jest-matcher-http 1.5.0 → 1.6.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.
- package/.github/workflows/test_and_publish.yml +2 -1
- package/README.md +2 -6
- package/integration_tests/tsProject/jest.config.js +10 -0
- package/integration_tests/tsProject/my.failing.test.ts +24 -0
- package/integration_tests/tsProject/my.successful.test.ts +24 -0
- package/integration_tests/tsProject.int.test.js +26 -0
- package/jest.unit.config.js +4 -3
- package/package.json +14 -2
- package/src/matchers.js +16 -6
- package/types/index.d.ts +10 -0
- package/.nvmrc +0 -1
|
@@ -23,11 +23,12 @@ jobs:
|
|
|
23
23
|
- run: npm ci
|
|
24
24
|
- run: npm test
|
|
25
25
|
- name: Upload coverage to Codecov
|
|
26
|
-
uses: codecov/codecov-action@
|
|
26
|
+
uses: codecov/codecov-action@v4
|
|
27
27
|
with:
|
|
28
28
|
flags: run-${{ matrix.node-version }}
|
|
29
29
|
fail_ci_if_error: true
|
|
30
30
|
verbose: true
|
|
31
|
+
token: ${{ secrets.CODECOV_TOKEN }}
|
|
31
32
|
|
|
32
33
|
analyze:
|
|
33
34
|
name: Analyze
|
package/README.md
CHANGED
|
@@ -37,17 +37,13 @@ With npm:
|
|
|
37
37
|
$ npm install --save-dev jest-matcher-http
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
Add this
|
|
40
|
+
Add this setting to your `jest.config.js`:
|
|
41
41
|
```
|
|
42
42
|
setupFilesAfterEnv: ['jest-matcher-http'],
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
```javascript
|
|
47
|
-
const { toReturnHttpCode } = require('jest-matcher-http');
|
|
45
|
+
There's an example typescript project here: [integration_tests/tsProject/](integration_tests/tsProject/). This is used for integration testing this lib to ensure typescript compatibility.
|
|
48
46
|
|
|
49
|
-
expect.extend({ toReturnHttpCode });
|
|
50
|
-
```
|
|
51
47
|
|
|
52
48
|
# Usage
|
|
53
49
|
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
preset: 'ts-jest',
|
|
5
|
+
// For real projects, use:
|
|
6
|
+
// setupFilesAfterEnv: ['jest-matcher-http'],
|
|
7
|
+
// Since this is a integration test project, living in the main
|
|
8
|
+
// package, we have to import the main package with a relative path.
|
|
9
|
+
setupFilesAfterEnv: ['../../'],
|
|
10
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// For real projects, use:
|
|
2
|
+
// import 'jest-matcher-http';
|
|
3
|
+
// Since this is a integration test project, living in the main
|
|
4
|
+
// package, we have to import the main package with a relative path.
|
|
5
|
+
import '../../';
|
|
6
|
+
|
|
7
|
+
import axios, { AxiosInstance, AxiosResponse } from 'axios';
|
|
8
|
+
|
|
9
|
+
const { runAgainstServer } = require('../localServer.int.helper');
|
|
10
|
+
|
|
11
|
+
describe('matchers', () => {
|
|
12
|
+
let request: AxiosInstance;
|
|
13
|
+
let response: AxiosResponse;
|
|
14
|
+
|
|
15
|
+
test('should always fail', async () => {
|
|
16
|
+
await runAgainstServer(async (url: string) => {
|
|
17
|
+
request = axios.create({ baseURL: url });
|
|
18
|
+
response = await request.get('/get-json');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
expect(response).toReturnHttpCode(500);
|
|
22
|
+
expect(response).toReturnHttpHeader('non-existing', 'Header');
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// For real projects, use:
|
|
2
|
+
// import 'jest-matcher-http';
|
|
3
|
+
// Since this is a integration test project, living in the main
|
|
4
|
+
// package, we have to import the main package with a relative path.
|
|
5
|
+
import '../../';
|
|
6
|
+
|
|
7
|
+
import axios, { AxiosInstance, AxiosResponse } from 'axios';
|
|
8
|
+
|
|
9
|
+
const { runAgainstServer } = require('../localServer.int.helper');
|
|
10
|
+
|
|
11
|
+
describe('matchers', () => {
|
|
12
|
+
let request: AxiosInstance;
|
|
13
|
+
let response: AxiosResponse;
|
|
14
|
+
|
|
15
|
+
test('should be able to use http matchers', async () => {
|
|
16
|
+
await runAgainstServer(async (url: string) => {
|
|
17
|
+
request = axios.create({ baseURL: url });
|
|
18
|
+
response = await request.get('/get-json');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
expect(response).toReturnHttpCode(200);
|
|
22
|
+
expect(response).toReturnHttpHeader('some', 'Header');
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { exec } = require('child_process');
|
|
4
|
+
|
|
5
|
+
describe('typescript project', () => {
|
|
6
|
+
// eslint-disable-next-line jest/no-done-callback
|
|
7
|
+
it('should successfully run jest tests of typescript project', (done) => {
|
|
8
|
+
const cmd = 'cd integration_tests/tsProject && npx jest my.successful.test.ts';
|
|
9
|
+
exec(cmd, (error) => {
|
|
10
|
+
expect(error).toBeNull();
|
|
11
|
+
done();
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// eslint-disable-next-line jest/no-done-callback
|
|
16
|
+
it('should let jest tests fail if tests failing', (done) => {
|
|
17
|
+
const cmd = 'cd integration_tests/tsProject && npx jest my.failing.test.ts';
|
|
18
|
+
exec(cmd, (error) => {
|
|
19
|
+
expect(error?.code).toBe(1);
|
|
20
|
+
expect(error?.message).toStrictEqual(
|
|
21
|
+
expect.stringContaining('expected http status code 200 to equal 500'),
|
|
22
|
+
);
|
|
23
|
+
done();
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
});
|
package/jest.unit.config.js
CHANGED
|
@@ -177,9 +177,10 @@ module.exports = {
|
|
|
177
177
|
|
|
178
178
|
// An array of regexp pattern strings that are matched against all test paths,
|
|
179
179
|
// matched tests are skipped
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
180
|
+
testPathIgnorePatterns: [
|
|
181
|
+
'/node_modules/',
|
|
182
|
+
'.ts$',
|
|
183
|
+
],
|
|
183
184
|
|
|
184
185
|
// The regexp pattern or array of patterns that Jest uses to detect test files
|
|
185
186
|
// testRegex: [],
|
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jest-matcher-http",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "Additional Jest matchers for HTTP responses.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": "16.x || 18.x || 20.x"
|
|
8
|
+
},
|
|
9
|
+
"types": "./types/index.d.ts",
|
|
6
10
|
"scripts": {
|
|
7
11
|
"lint": "eslint .",
|
|
8
12
|
"test": "jest --coverage --config jest.unit.config.js",
|
|
@@ -35,6 +39,7 @@
|
|
|
35
39
|
]
|
|
36
40
|
},
|
|
37
41
|
"devDependencies": {
|
|
42
|
+
"@types/jest": "^29.5.12",
|
|
38
43
|
"axios": "^1.3.4",
|
|
39
44
|
"eslint": "^8.36.0",
|
|
40
45
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
@@ -47,9 +52,16 @@
|
|
|
47
52
|
"pre-commit": "^1.2.2",
|
|
48
53
|
"semantic-release": "^23.0.7",
|
|
49
54
|
"signal-promise": "^1.0.3",
|
|
50
|
-
"superagent": "^8.0.9"
|
|
55
|
+
"superagent": "^8.0.9",
|
|
56
|
+
"ts-jest": "^29.1.2",
|
|
57
|
+
"typescript": "^5.4.4"
|
|
51
58
|
},
|
|
52
59
|
"dependencies": {
|
|
53
60
|
"@jest/globals": "^29.5.0"
|
|
61
|
+
},
|
|
62
|
+
"overrides": {
|
|
63
|
+
"superagent": {
|
|
64
|
+
"formidable": "^3.2.4"
|
|
65
|
+
}
|
|
54
66
|
}
|
|
55
67
|
}
|
package/src/matchers.js
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {object} GenericResponse
|
|
5
|
+
* @property {{ [key: string]: any }} headers - The headers of the response.
|
|
6
|
+
* @property {object} [data] - The data of the response.
|
|
7
|
+
* @property {string} [text] - The text of the response.
|
|
8
|
+
* @property {object} [body] - The body of the response.
|
|
9
|
+
* @property {number} [status] - The status of the response.
|
|
10
|
+
* @property {number} [statusCode] - The status of the response.
|
|
11
|
+
*/
|
|
12
|
+
|
|
3
13
|
/**
|
|
4
14
|
* Extract the result from the response.
|
|
5
15
|
*
|
|
6
|
-
* @param {
|
|
7
|
-
* @returns {object} The result. Empty string if no result found.
|
|
16
|
+
* @param {GenericResponse} response - The response of the request.
|
|
17
|
+
* @returns {object | string} The result. Empty string if no result found.
|
|
8
18
|
*/
|
|
9
19
|
function extractResult(response) {
|
|
10
20
|
if (response.data) {
|
|
@@ -26,9 +36,9 @@ function extractResult(response) {
|
|
|
26
36
|
* Expect a http request to return given http status code.
|
|
27
37
|
* Log response body and headers otherwise.
|
|
28
38
|
*
|
|
29
|
-
* @param {
|
|
39
|
+
* @param {GenericResponse} response - The response of the request.
|
|
30
40
|
* @param {number} expectedHttpStatusCode - The expected http status code.
|
|
31
|
-
* @returns {{
|
|
41
|
+
* @returns {{ pass: boolean, message: () => string }} The expect result according to jest.
|
|
32
42
|
* @see {@link https://jestjs.io/docs/expect#expectextendmatchers}
|
|
33
43
|
*/
|
|
34
44
|
function toReturnHttpCode(response, expectedHttpStatusCode) {
|
|
@@ -51,10 +61,10 @@ function toReturnHttpCode(response, expectedHttpStatusCode) {
|
|
|
51
61
|
* Expect a http request to return the given http header.
|
|
52
62
|
* Log response body and headers otherwise.
|
|
53
63
|
*
|
|
54
|
-
* @param {
|
|
64
|
+
* @param {GenericResponse} response - The response of the request.
|
|
55
65
|
* @param {string} headerField - The expected http header field.
|
|
56
66
|
* @param {string} headerValue - The expected http header value.
|
|
57
|
-
* @returns {{
|
|
67
|
+
* @returns {{ pass: boolean, message: () => string }} The expect result according to jest.
|
|
58
68
|
* @see {@link https://jestjs.io/docs/expect#expectextendmatchers}
|
|
59
69
|
*/
|
|
60
70
|
function toReturnHttpHeader(response, headerField, headerValue) {
|
package/types/index.d.ts
ADDED
package/.nvmrc
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
v18
|