jest-matcher-http 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,11 +5,11 @@ updates:
5
5
  schedule:
6
6
  interval: "weekly"
7
7
  commit-message:
8
- prefix: "fix"
8
+ prefix: "chore"
9
9
 
10
10
  - package-ecosystem: "npm"
11
11
  directory: "/"
12
12
  schedule:
13
13
  interval: "daily"
14
14
  commit-message:
15
- prefix: "fix"
15
+ prefix: "chore"
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  <img alt="npm latest version" src="https://img.shields.io/npm/v/jest-matcher-http/latest.svg">
6
6
  </a>
7
7
  <a href="https://github.com/rimesime/jest-matcher-http/actions?query=workflow%3ATest+branch%3Amain">
8
- <img alt="Build states" src="https://github.com/semantic-release/semantic-release/workflows/Test/badge.svg">
8
+ <img alt="Build states" src="https://github.com/rimesime/jest-matcher-http/workflows/Test/badge.svg">
9
9
  </a>
10
10
  <a href="#badge">
11
11
  <img alt="semantic-release: angular" src="https://img.shields.io/badge/semantic--release-angular-e10079?logo=semantic-release">
@@ -22,6 +22,10 @@ Supported http libraries:
22
22
  - [axios](https://www.npmjs.com/package/axios)
23
23
  - [needle](https://www.npmjs.com/package/needle)
24
24
 
25
+ Supported content types:
26
+ - application/json
27
+ - text/plain
28
+
25
29
  # Installation & Configuration
26
30
 
27
31
  With npm:
@@ -5,43 +5,64 @@ const axios = require('axios');
5
5
  const { runAgainstServer, staticVariables } = require('./localServer.int.helper');
6
6
 
7
7
  describe('axios', () => {
8
- let response;
9
- const { body, headerName, headerValue } = staticVariables;
8
+ let responseJson;
9
+ let responseText;
10
+ const {
11
+ resultJson,
12
+ resultText,
13
+ headerName,
14
+ headerValue,
15
+ } = staticVariables;
10
16
 
11
17
  beforeAll(async () => {
12
18
  await runAgainstServer(async (url) => {
13
19
  const request = axios.create({ baseURL: url });
14
- response = await request.get('/');
20
+ responseJson = await request.get('/json');
21
+ responseText = await request.get('/text');
15
22
  });
16
23
  });
17
24
 
18
25
  describe('toReturnHttpCode', () => {
19
26
  it('should succeed if http code is as expected', async () => {
20
- expect(response).toReturnHttpCode(200);
27
+ expect(responseJson).toReturnHttpCode(200);
21
28
  });
22
29
 
23
- it('should fail if http code is not as expected', async () => {
30
+ it('should fail if http code is not as expected for application/json responses', async () => {
24
31
  let caughtError;
25
32
  try {
26
- expect(response).toReturnHttpCode(500);
33
+ expect(responseJson).toReturnHttpCode(500);
27
34
  } catch (error) {
28
35
  caughtError = error;
29
36
  }
30
37
 
31
38
  expect(caughtError.message).toContain('expected http status code 200 to equal 500\n');
39
+ expect(caughtError.message).toContain(`${JSON.stringify(resultJson, null, 2)}\n`);
40
+ expect(caughtError.message).toContain(`"${headerName.toLowerCase()}": "${headerValue}"`);
41
+ });
42
+
43
+ it('should fail if http code is not as expected for plain/text responses', async () => {
44
+ let caughtError;
45
+ try {
46
+ expect(responseText).toReturnHttpCode(500);
47
+ } catch (error) {
48
+ caughtError = error;
49
+ }
50
+
51
+ expect(caughtError.message).toContain('expected http status code 200 to equal 500\n');
52
+ expect(caughtError.message).toContain(`${JSON.stringify(resultText, null, 2)}\n`);
32
53
  expect(caughtError.message).toContain(`"${headerName.toLowerCase()}": "${headerValue}"`);
33
54
  });
34
55
  });
35
56
 
36
57
  describe('toReturnHttpHeader', () => {
37
58
  it('should succeed if http header is as expected', async () => {
38
- expect(response).toReturnHttpHeader(headerName, headerValue);
59
+ expect(responseJson).toReturnHttpHeader(headerName, headerValue);
39
60
  });
40
61
 
41
62
  it('should fail if http header is not as expected', async () => {
42
63
  let caughtError;
43
64
  try {
44
- expect(response).toReturnHttpHeader(headerName, '');
65
+ expect(responseJson).toReturnHttpHeader(headerName, '');
45
66
  } catch (error) {
46
67
  caughtError = error;
47
68
  }
@@ -50,7 +71,7 @@ describe('axios', () => {
50
71
  `expected http header "${headerName.toLowerCase()}" with value ""\n`
51
72
  + '\n'
52
73
  + 'server responded with body:\n'
53
- + `${JSON.stringify(body, null, 2)}\n`
74
+ + `${JSON.stringify(resultJson, null, 2)}\n`
54
75
  + '\n'
55
76
  + 'server responded with headers:\n',
56
77
  );
@@ -5,7 +5,8 @@ const http = require('http');
5
5
  const { createHttpTerminator } = require('http-terminator');
6
6
  const Signal = require('signal-promise');
7
7
 
8
- const body = { name: 'john' };
8
+ const resultJson = { name: 'john' };
9
+ const resultText = 'Text';
9
10
  const headerName = 'Some';
10
11
  const headerValue = 'Header';
11
12
 
@@ -19,11 +20,19 @@ const headerValue = 'Header';
19
20
  async function runAgainstServer(func) {
20
21
  // eslint-disable-next-line new-cap
21
22
  const app = new express();
22
- app.get('/', (req, res) => {
23
+ app.get('/json', (req, res) => {
23
24
  res
24
25
  .status(200)
26
+ .set('Content-Type', 'application/json')
25
27
  .set(headerName, headerValue)
26
- .json(body);
28
+ .json(resultJson);
29
+ });
30
+ app.get('/text', (req, res) => {
31
+ res
32
+ .status(200)
33
+ .set('Content-Type', 'text/plain')
34
+ .set(headerName, headerValue)
35
+ .send(resultText);
27
36
  });
28
37
  const server = http.createServer(app);
29
38
  const httpTerminator = createHttpTerminator({ server });
@@ -40,7 +49,8 @@ async function runAgainstServer(func) {
40
49
  module.exports = {
41
50
  runAgainstServer,
42
51
  staticVariables: {
43
- body,
52
+ resultJson,
53
+ resultText,
44
54
  headerName,
45
55
  headerValue,
46
56
  },
@@ -5,42 +5,63 @@ const needle = require('needle');
5
5
  const { runAgainstServer, staticVariables } = require('./localServer.int.helper');
6
6
 
7
7
  describe('needle', () => {
8
- let response;
9
- const { body, headerName, headerValue } = staticVariables;
8
+ let responseJson;
9
+ let responseText;
10
+ const {
11
+ resultJson,
12
+ resultText,
13
+ headerName,
14
+ headerValue,
15
+ } = staticVariables;
10
16
 
11
17
  beforeAll(async () => {
12
18
  await runAgainstServer(async (url) => {
13
- response = await needle('get', url);
19
+ responseJson = await needle('get', `${url}/json`);
20
+ responseText = await needle('get', `${url}/text`);
14
21
  });
15
22
  });
16
23
 
17
24
  describe('toReturnHttpCode', () => {
18
25
  it('should succeed if http code is as expected', async () => {
19
- expect(response).toReturnHttpCode(200);
26
+ expect(responseJson).toReturnHttpCode(200);
20
27
  });
21
28
 
22
- it('should fail if http code is not as expected', async () => {
29
+ it('should fail if http code is not as expected for application/json responses', async () => {
23
30
  let caughtError;
24
31
  try {
25
- expect(response).toReturnHttpCode(500);
32
+ expect(responseJson).toReturnHttpCode(500);
26
33
  } catch (error) {
27
34
  caughtError = error;
28
35
  }
29
36
 
30
37
  expect(caughtError.message).toContain('expected http status code 200 to equal 500\n');
38
+ expect(caughtError.message).toContain(`${JSON.stringify(resultJson, null, 2)}\n`);
39
+ expect(caughtError.message).toContain(`"${headerName.toLowerCase()}": "${headerValue}"`);
40
+ });
41
+
42
+ it('should fail if http code is not as expected for plain/text responses', async () => {
43
+ let caughtError;
44
+ try {
45
+ expect(responseText).toReturnHttpCode(500);
46
+ } catch (error) {
47
+ caughtError = error;
48
+ }
49
+
50
+ expect(caughtError.message).toContain('expected http status code 200 to equal 500\n');
51
+ expect(caughtError.message).toContain(`${JSON.stringify(resultText, null, 2)}\n`);
31
52
  expect(caughtError.message).toContain(`"${headerName.toLowerCase()}": "${headerValue}"`);
32
53
  });
33
54
  });
34
55
 
35
56
  describe('toReturnHttpHeader', () => {
36
57
  it('should succeed if http header is as expected', async () => {
37
- expect(response).toReturnHttpHeader(headerName, headerValue);
58
+ expect(responseJson).toReturnHttpHeader(headerName, headerValue);
38
59
  });
39
60
 
40
61
  it('should fail if http header is not as expected', async () => {
41
62
  let caughtError;
42
63
  try {
43
- expect(response).toReturnHttpHeader(headerName, '');
64
+ expect(responseJson).toReturnHttpHeader(headerName, '');
44
65
  } catch (error) {
45
66
  caughtError = error;
46
67
  }
@@ -49,7 +70,7 @@ describe('needle', () => {
49
70
  `expected http header "${headerName.toLowerCase()}" with value ""\n`
50
71
  + '\n'
51
72
  + 'server responded with body:\n'
52
- + `${JSON.stringify(body, null, 2)}\n`
73
+ + `${JSON.stringify(resultJson, null, 2)}\n`
53
74
  + '\n'
54
75
  + 'server responded with headers:\n',
55
76
  );
@@ -5,42 +5,63 @@ const superagent = require('superagent');
5
5
  const { runAgainstServer, staticVariables } = require('./localServer.int.helper');
6
6
 
7
7
  describe('superagent', () => {
8
- let response;
9
- const { body, headerName, headerValue } = staticVariables;
8
+ let responseJson;
9
+ let responseText;
10
+ const {
11
+ resultJson,
12
+ resultText,
13
+ headerName,
14
+ headerValue,
15
+ } = staticVariables;
10
16
 
11
17
  beforeAll(async () => {
12
18
  await runAgainstServer(async (url) => {
13
- response = await superagent.get(`${url}/`);
19
+ responseJson = await superagent.get(`${url}/json`);
20
+ responseText = await superagent.get(`${url}/text`);
14
21
  });
15
22
  });
16
23
 
17
24
  describe('toReturnHttpCode', () => {
18
25
  it('should succeed if http code is as expected', async () => {
19
- expect(response).toReturnHttpCode(200);
26
+ expect(responseJson).toReturnHttpCode(200);
20
27
  });
21
28
 
22
- it('should fail if http code is not as expected', async () => {
29
+ it('should fail if http code is not as expected for application/json responses', async () => {
23
30
  let caughtError;
24
31
  try {
25
- expect(response).toReturnHttpCode(500);
32
+ expect(responseJson).toReturnHttpCode(500);
26
33
  } catch (error) {
27
34
  caughtError = error;
28
35
  }
29
36
 
30
37
  expect(caughtError.message).toContain('expected http status code 200 to equal 500\n');
38
+ expect(caughtError.message).toContain(`${JSON.stringify(resultJson, null, 2)}\n`);
39
+ expect(caughtError.message).toContain(`"${headerName.toLowerCase()}": "${headerValue}"`);
40
+ });
41
+
42
+ it('should fail if http code is not as expected for plain/text responses', async () => {
43
+ let caughtError;
44
+ try {
45
+ expect(responseText).toReturnHttpCode(500);
46
+ } catch (error) {
47
+ caughtError = error;
48
+ }
49
+
50
+ expect(caughtError.message).toContain('expected http status code 200 to equal 500\n');
51
+ expect(caughtError.message).toContain(`${JSON.stringify(resultText, null, 2)}\n`);
31
52
  expect(caughtError.message).toContain(`"${headerName.toLowerCase()}": "${headerValue}"`);
32
53
  });
33
54
  });
34
55
 
35
56
  describe('toReturnHttpHeader', () => {
36
57
  it('should succeed if http header is as expected', async () => {
37
- expect(response).toReturnHttpHeader(headerName, headerValue);
58
+ expect(responseJson).toReturnHttpHeader(headerName, headerValue);
38
59
  });
39
60
 
40
61
  it('should fail if http header is not as expected', async () => {
41
62
  let caughtError;
42
63
  try {
43
- expect(response).toReturnHttpHeader(headerName, '');
64
+ expect(responseJson).toReturnHttpHeader(headerName, '');
44
65
  } catch (error) {
45
66
  caughtError = error;
46
67
  }
@@ -49,7 +70,7 @@ describe('superagent', () => {
49
70
  `expected http header "${headerName.toLowerCase()}" with value ""\n`
50
71
  + '\n'
51
72
  + 'server responded with body:\n'
52
- + `${JSON.stringify(body, null, 2)}\n`
73
+ + `${JSON.stringify(resultJson, null, 2)}\n`
53
74
  + '\n'
54
75
  + 'server responded with headers:\n',
55
76
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jest-matcher-http",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Additional Jest matchers for HTTP responses.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/matchers.js CHANGED
@@ -1,16 +1,39 @@
1
1
  'use strict';
2
2
 
3
+ /**
4
+ * Extract the result from the response.
5
+ *
6
+ * @param {object} response - The response of the request.
7
+ * @returns {object} The result.
8
+ * @throws {Error} If no result could be extracted.
9
+ */
10
+ function extractResult(response) {
11
+ if (response.data) {
12
+ return response.data;
13
+ }
14
+
15
+ if (response.text && response.text !== JSON.stringify(response.body)) {
16
+ return response.text;
17
+ }
18
+
19
+ if (response.body) {
20
+ return response.body;
21
+ }
22
+
23
+ throw new Error('jest-matcher-http does not support this library');
24
+ }
25
+
3
26
  /**
4
27
  * Expect a http request to return given http status code.
5
28
  * Log response body and headers otherwise.
6
29
  *
7
- * @param {{status: number, body: any, headers: any}} response - The response of the request.
30
+ * @param {object} response - The response of the request.
8
31
  * @param {number} expectedHttpStatusCode - The expected http status code.
9
32
  * @returns {{message: Function, pass: boolean}} The expect result according to jest.
10
33
  * @see {@link https://jestjs.io/docs/expect#expectextendmatchers}
11
34
  */
12
35
  function toReturnHttpCode(response, expectedHttpStatusCode) {
13
- const body = response.body ?? response.data;
36
+ const result = extractResult(response);
14
37
  const status = response.status ?? response.statusCode;
15
38
  const { headers } = response;
16
39
 
@@ -23,7 +46,7 @@ function toReturnHttpCode(response, expectedHttpStatusCode) {
23
46
  return {
24
47
  pass,
25
48
  message: () => `expected http status code ${status} to equal ${expectedHttpStatusCode}\n\n`
26
- + `server responded with body:\n${JSON.stringify(body, null, 2)}\n\n`
49
+ + `server responded with body:\n${JSON.stringify(result, null, 2)}\n\n`
27
50
  + `server responded with headers:\n${JSON.stringify(headers, null, 2)}`,
28
51
  };
29
52
  }
@@ -32,14 +55,14 @@ function toReturnHttpCode(response, expectedHttpStatusCode) {
32
55
  * Expect a http request to return the given http header.
33
56
  * Log response body and headers otherwise.
34
57
  *
35
- * @param {{body: any, headers: any}} response - The response of the request.
58
+ * @param {object} response - The response of the request.
36
59
  * @param {string} headerField - The expected http header field.
37
60
  * @param {string} headerValue - The expected http header value.
38
61
  * @returns {{message: Function, pass: boolean}} The expect result according to jest.
39
62
  * @see {@link https://jestjs.io/docs/expect#expectextendmatchers}
40
63
  */
41
64
  function toReturnHttpHeader(response, headerField, headerValue) {
42
- const body = response.body ?? response.data;
65
+ const result = extractResult(response);
43
66
  const { headers } = response;
44
67
  const headerFieldLowerCase = headerField.toLowerCase();
45
68
 
@@ -52,7 +75,7 @@ function toReturnHttpHeader(response, headerField, headerValue) {
52
75
  return {
53
76
  pass,
54
77
  message: () => `expected http header "${headerFieldLowerCase}" with value "${headerValue}"\n\n`
55
- + `server responded with body:\n${JSON.stringify(body, null, 2)}\n\n`
78
+ + `server responded with body:\n${JSON.stringify(result, null, 2)}\n\n`
56
79
  + `server responded with headers:\n${JSON.stringify(headers, null, 2)}`,
57
80
  };
58
81
  }
@@ -32,6 +32,16 @@ describe('matchers', () => {
32
32
  expect(response).toReturnHttpCode(response.status);
33
33
  });
34
34
 
35
+ it('should succeed if result is in text', async () => {
36
+ const response = {
37
+ status: 200,
38
+ text: 'result',
39
+ headers: {},
40
+ };
41
+
42
+ expect(response).toReturnHttpCode(response.status);
43
+ });
44
+
35
45
  it('should fail if http code is not as expected', async () => {
36
46
  const response = {
37
47
  status: 200,
@@ -56,6 +66,22 @@ describe('matchers', () => {
56
66
  + '{}',
57
67
  );
58
68
  });
69
+
70
+ it('should throw if result is not supported', async () => {
71
+ const response = {
72
+ status: 200,
73
+ headers: {},
74
+ };
75
+
76
+ let caughtError;
77
+ try {
78
+ expect(response).toReturnHttpCode(1);
79
+ } catch (error) {
80
+ caughtError = error;
81
+ }
82
+
83
+ expect(caughtError.message).toBe('jest-matcher-http does not support this library');
84
+ });
59
85
  });
60
86
 
61
87
  describe('toReturnHttpHeader', () => {