jest-matcher-http 1.3.0 → 1.4.1
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/.eslintrc.js +4 -1
- package/.github/workflows/test_and_publish.yml +9 -6
- package/README.md +14 -22
- package/integration_tests/axios.int.test.js +1 -1
- package/integration_tests/needle.int.test.js +1 -1
- package/integration_tests/superagent.int.test.js +1 -1
- package/package.json +14 -4
- package/src/matchers.js +4 -10
- package/src/matchers.unit.test.js +75 -1
- package/.git-hooks/pre-commit +0 -4
- package/.git-hooks/pre-commit-eslint +0 -36
- package/.git-hooks/pre-commit-test-unit +0 -3
package/.eslintrc.js
CHANGED
|
@@ -5,13 +5,16 @@ module.exports = {
|
|
|
5
5
|
node: true,
|
|
6
6
|
'jest/globals': true,
|
|
7
7
|
},
|
|
8
|
+
plugins: [
|
|
9
|
+
'jest',
|
|
10
|
+
'jsdoc',
|
|
11
|
+
],
|
|
8
12
|
extends: [
|
|
9
13
|
'airbnb-base',
|
|
10
14
|
'plugin:jsdoc/recommended',
|
|
11
15
|
'plugin:jest/recommended',
|
|
12
16
|
'plugin:jest/style',
|
|
13
17
|
],
|
|
14
|
-
plugins: ['jest'],
|
|
15
18
|
overrides: [],
|
|
16
19
|
parserOptions: {
|
|
17
20
|
ecmaVersion: 2020,
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
|
|
2
|
-
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
|
|
3
|
-
|
|
4
1
|
name: Test & Publish
|
|
5
2
|
|
|
6
3
|
on:
|
|
@@ -15,7 +12,7 @@ jobs:
|
|
|
15
12
|
runs-on: ubuntu-latest
|
|
16
13
|
strategy:
|
|
17
14
|
matrix:
|
|
18
|
-
node-version: [16.x, 18.x]
|
|
15
|
+
node-version: [16.x, 18.x, 19.x]
|
|
19
16
|
steps:
|
|
20
17
|
- uses: actions/checkout@v3
|
|
21
18
|
- name: Use Node.js ${{ matrix.node-version }}
|
|
@@ -25,6 +22,12 @@ jobs:
|
|
|
25
22
|
cache: 'npm'
|
|
26
23
|
- run: npm ci
|
|
27
24
|
- run: npm test
|
|
25
|
+
- name: Upload coverage to Codecov
|
|
26
|
+
uses: codecov/codecov-action@v3
|
|
27
|
+
with:
|
|
28
|
+
flags: run-${{ matrix.node-version }}
|
|
29
|
+
fail_ci_if_error: true
|
|
30
|
+
verbose: true
|
|
28
31
|
|
|
29
32
|
analyze:
|
|
30
33
|
name: Analyze
|
|
@@ -60,10 +63,10 @@ jobs:
|
|
|
60
63
|
needs: [test, analyze]
|
|
61
64
|
steps:
|
|
62
65
|
- uses: actions/checkout@v3
|
|
63
|
-
- name: Use Node.js
|
|
66
|
+
- name: Use Node.js 18.x
|
|
64
67
|
uses: actions/setup-node@v3
|
|
65
68
|
with:
|
|
66
|
-
node-version:
|
|
69
|
+
node-version: 18.x
|
|
67
70
|
cache: 'npm'
|
|
68
71
|
- run: npm ci
|
|
69
72
|
- run: npm run semantic-release
|
package/README.md
CHANGED
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
<a href="https://github.com/rimesime/jest-matcher-http/actions?query=workflow%3ATest+branch%3Amain">
|
|
8
8
|
<img alt="Build states" src="https://github.com/rimesime/jest-matcher-http/workflows/Test/badge.svg">
|
|
9
9
|
</a>
|
|
10
|
+
<a href="https://codecov.io/gh/rimesime/jest-matcher-http" >
|
|
11
|
+
<img src="https://codecov.io/gh/rimesime/jest-matcher-http/branch/main/graph/badge.svg?token=OY3A3BFOUG"/>
|
|
12
|
+
</a>
|
|
10
13
|
<a href="#badge">
|
|
11
14
|
<img alt="semantic-release: angular" src="https://img.shields.io/badge/semantic--release-angular-e10079?logo=semantic-release">
|
|
12
15
|
</a>
|
|
@@ -97,30 +100,19 @@ describe('Example', () => {
|
|
|
97
100
|
|
|
98
101
|
# Contributing
|
|
99
102
|
|
|
100
|
-
|
|
101
|
-
```shell
|
|
102
|
-
$ git clone git@github.com:rimesime/jest-matcher-http.git
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
1. This repository uses conventional commits that are validated using `git-conventional-commits`. To validate this automatically, git-hooks need to be enabled manually after cloning this repository:
|
|
106
|
-
```shell
|
|
107
|
-
$ git config core.hooksPath .git-hooks
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
1. Install dependencies:
|
|
111
|
-
```shell
|
|
112
|
-
$ npm ci
|
|
113
|
-
```
|
|
103
|
+
Thank you for investing time to contribute to this project.
|
|
114
104
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
105
|
+
Clone the repository:
|
|
106
|
+
```shell
|
|
107
|
+
$ git clone git@github.com:rimesime/jest-matcher-http.git
|
|
108
|
+
$ npm ci
|
|
109
|
+
$ npm test
|
|
110
|
+
```
|
|
119
111
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
112
|
+
If you spot a problem, search if an issue already exists. If a
|
|
113
|
+
related issue doesn't exist, you can open a new issue providing
|
|
114
|
+
all relevant information to e.g. reproduce the problem or motivate
|
|
115
|
+
your change. You are welcome to open a PR for any ticket.
|
|
124
116
|
|
|
125
117
|
# License
|
|
126
118
|
|
|
@@ -68,7 +68,7 @@ describe('axios', () => {
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
expect(caughtError.message).toContain(
|
|
71
|
-
`expected http header "${headerName.toLowerCase()}"
|
|
71
|
+
`expected http header "${headerName.toLowerCase()}" to have value ""\n`
|
|
72
72
|
+ '\n'
|
|
73
73
|
+ 'server responded with body:\n'
|
|
74
74
|
+ `${JSON.stringify(resultJson, null, 2)}\n`
|
|
@@ -67,7 +67,7 @@ describe('needle', () => {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
expect(caughtError.message).toContain(
|
|
70
|
-
`expected http header "${headerName.toLowerCase()}"
|
|
70
|
+
`expected http header "${headerName.toLowerCase()}" to have value ""\n`
|
|
71
71
|
+ '\n'
|
|
72
72
|
+ 'server responded with body:\n'
|
|
73
73
|
+ `${JSON.stringify(resultJson, null, 2)}\n`
|
|
@@ -67,7 +67,7 @@ describe('superagent', () => {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
expect(caughtError.message).toContain(
|
|
70
|
-
`expected http header "${headerName.toLowerCase()}"
|
|
70
|
+
`expected http header "${headerName.toLowerCase()}" to have value ""\n`
|
|
71
71
|
+ '\n'
|
|
72
72
|
+ 'server responded with body:\n'
|
|
73
73
|
+ `${JSON.stringify(resultJson, null, 2)}\n`
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jest-matcher-http",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "Additional Jest matchers for HTTP responses.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
+
"lint": "eslint .",
|
|
7
8
|
"test": "jest --coverage --config jest.unit.config.js",
|
|
8
9
|
"acp": "git add . && npm run commit && git push",
|
|
9
10
|
"commit": "git-cz",
|
|
@@ -19,6 +20,10 @@
|
|
|
19
20
|
"url": "https://github.com/rimesime/jest-matcher-http/issues"
|
|
20
21
|
},
|
|
21
22
|
"homepage": "https://github.com/rimesime/jest-matcher-http#readme",
|
|
23
|
+
"pre-commit": [
|
|
24
|
+
"lint",
|
|
25
|
+
"test"
|
|
26
|
+
],
|
|
22
27
|
"release": {
|
|
23
28
|
"branches": [
|
|
24
29
|
"main"
|
|
@@ -35,15 +40,15 @@
|
|
|
35
40
|
"axios": "^1.2.1",
|
|
36
41
|
"commitizen": "^4.2.5",
|
|
37
42
|
"cz-conventional-changelog": "^3.3.0",
|
|
38
|
-
"eslint": "^8.
|
|
43
|
+
"eslint": "^8.31.0",
|
|
39
44
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
40
|
-
"eslint-plugin-
|
|
41
|
-
"eslint-plugin-jest": "^27.1.6",
|
|
45
|
+
"eslint-plugin-jest": "^27.2.0",
|
|
42
46
|
"eslint-plugin-jsdoc": "^39.6.4",
|
|
43
47
|
"express": "^4.18.2",
|
|
44
48
|
"http-terminator": "^3.2.0",
|
|
45
49
|
"jest": "^29.3.1",
|
|
46
50
|
"needle": "^3.2.0",
|
|
51
|
+
"pre-commit": "^1.2.2",
|
|
47
52
|
"semantic-release": "^19.0.5",
|
|
48
53
|
"signal-promise": "^1.0.3",
|
|
49
54
|
"superagent": "^8.0.5"
|
|
@@ -51,6 +56,11 @@
|
|
|
51
56
|
"dependencies": {
|
|
52
57
|
"@jest/globals": "^29.3.1"
|
|
53
58
|
},
|
|
59
|
+
"overrides": {
|
|
60
|
+
"eslint-config-airbnb-base": {
|
|
61
|
+
"json5": "^2.2.2"
|
|
62
|
+
}
|
|
63
|
+
},
|
|
54
64
|
"config": {
|
|
55
65
|
"commitizen": {
|
|
56
66
|
"path": "./node_modules/cz-conventional-changelog"
|
package/src/matchers.js
CHANGED
|
@@ -39,13 +39,10 @@ function toReturnHttpCode(response, expectedHttpStatusCode) {
|
|
|
39
39
|
|
|
40
40
|
const pass = status === expectedHttpStatusCode;
|
|
41
41
|
|
|
42
|
-
if (pass) {
|
|
43
|
-
return { pass };
|
|
44
|
-
}
|
|
45
|
-
|
|
46
42
|
return {
|
|
47
43
|
pass,
|
|
48
|
-
message: () => `expected http status code ${status}
|
|
44
|
+
message: () => `expected http status code ${status}${pass ? ' not' : ''} `
|
|
45
|
+
+ `to equal ${expectedHttpStatusCode}\n\n`
|
|
49
46
|
+ `server responded with body:\n${JSON.stringify(result, null, 2)}\n\n`
|
|
50
47
|
+ `server responded with headers:\n${JSON.stringify(headers, null, 2)}`,
|
|
51
48
|
};
|
|
@@ -68,13 +65,10 @@ function toReturnHttpHeader(response, headerField, headerValue) {
|
|
|
68
65
|
|
|
69
66
|
const pass = headers[headerFieldLowerCase] === headerValue;
|
|
70
67
|
|
|
71
|
-
if (pass) {
|
|
72
|
-
return { pass };
|
|
73
|
-
}
|
|
74
|
-
|
|
75
68
|
return {
|
|
76
69
|
pass,
|
|
77
|
-
message: () => `expected http header "${headerFieldLowerCase}"
|
|
70
|
+
message: () => `expected http header "${headerFieldLowerCase}"`
|
|
71
|
+
+ `${pass ? ' not' : ''} to have value "${headerValue}"\n\n`
|
|
78
72
|
+ `server responded with body:\n${JSON.stringify(result, null, 2)}\n\n`
|
|
79
73
|
+ `server responded with headers:\n${JSON.stringify(headers, null, 2)}`,
|
|
80
74
|
};
|
|
@@ -67,6 +67,41 @@ describe('matchers', () => {
|
|
|
67
67
|
);
|
|
68
68
|
});
|
|
69
69
|
|
|
70
|
+
it('should succeed if http code is not as expected - negated', async () => {
|
|
71
|
+
const response = {
|
|
72
|
+
status: 200,
|
|
73
|
+
body: {},
|
|
74
|
+
headers: {},
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
expect(response).not.toReturnHttpCode(1);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should fail if http code is not as expected - negated', async () => {
|
|
81
|
+
const response = {
|
|
82
|
+
status: 200,
|
|
83
|
+
body: {},
|
|
84
|
+
headers: {},
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
let caughtError;
|
|
88
|
+
try {
|
|
89
|
+
expect(response).not.toReturnHttpCode(response.status);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
caughtError = error;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
expect(caughtError.message).toBe(
|
|
95
|
+
'expected http status code 200 not to equal 200\n'
|
|
96
|
+
+ '\n'
|
|
97
|
+
+ 'server responded with body:\n'
|
|
98
|
+
+ '{}\n'
|
|
99
|
+
+ '\n'
|
|
100
|
+
+ 'server responded with headers:\n'
|
|
101
|
+
+ '{}',
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
|
|
70
105
|
it('should throw if result is not supported', async () => {
|
|
71
106
|
const response = {
|
|
72
107
|
status: 200,
|
|
@@ -124,7 +159,7 @@ describe('matchers', () => {
|
|
|
124
159
|
}
|
|
125
160
|
|
|
126
161
|
expect(caughtError.message).toBe(
|
|
127
|
-
'expected http header "request-id"
|
|
162
|
+
'expected http header "request-id" to have value "some-uuid"\n'
|
|
128
163
|
+ '\n'
|
|
129
164
|
+ 'server responded with body:\n'
|
|
130
165
|
+ '{}\n'
|
|
@@ -133,5 +168,44 @@ describe('matchers', () => {
|
|
|
133
168
|
+ '{}',
|
|
134
169
|
);
|
|
135
170
|
});
|
|
171
|
+
|
|
172
|
+
it('should succeed if http header is not as expected - negated', async () => {
|
|
173
|
+
const response = {
|
|
174
|
+
status: 200,
|
|
175
|
+
body: {},
|
|
176
|
+
headers: {},
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
expect(response).not.toReturnHttpHeader('request-id', 'some-uuid');
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('should fail if http header is not as expected - negated', async () => {
|
|
183
|
+
const response = {
|
|
184
|
+
status: 200,
|
|
185
|
+
body: {},
|
|
186
|
+
headers: {
|
|
187
|
+
'request-id': 'some-uuid',
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
let caughtError;
|
|
192
|
+
try {
|
|
193
|
+
expect(response).not.toReturnHttpHeader('request-id', 'some-uuid');
|
|
194
|
+
} catch (error) {
|
|
195
|
+
caughtError = error;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
expect(caughtError.message).toBe(
|
|
199
|
+
'expected http header "request-id" not to have value "some-uuid"\n'
|
|
200
|
+
+ '\n'
|
|
201
|
+
+ 'server responded with body:\n'
|
|
202
|
+
+ '{}\n'
|
|
203
|
+
+ '\n'
|
|
204
|
+
+ 'server responded with headers:\n'
|
|
205
|
+
+ '{\n'
|
|
206
|
+
+ ' "request-id": "some-uuid"\n'
|
|
207
|
+
+ '}',
|
|
208
|
+
);
|
|
209
|
+
});
|
|
136
210
|
});
|
|
137
211
|
});
|
package/.git-hooks/pre-commit
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
#!/bin/sh
|
|
2
|
-
|
|
3
|
-
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".jsx\{0,1\}$")
|
|
4
|
-
|
|
5
|
-
if [[ "$STAGED_FILES" = "" ]]; then
|
|
6
|
-
exit 0
|
|
7
|
-
fi
|
|
8
|
-
|
|
9
|
-
PASS=true
|
|
10
|
-
|
|
11
|
-
echo "\nValidating Javascript:\n"
|
|
12
|
-
|
|
13
|
-
ESLINT="$(git rev-parse --show-toplevel)/node_modules/.bin/eslint"
|
|
14
|
-
|
|
15
|
-
for FILE in $STAGED_FILES
|
|
16
|
-
do
|
|
17
|
-
"$ESLINT" "$FILE"
|
|
18
|
-
|
|
19
|
-
if [[ "$?" == 0 ]]; then
|
|
20
|
-
echo "\t\033[32mESLint Passed: $FILE\033[0m"
|
|
21
|
-
else
|
|
22
|
-
echo "\t\033[41mESLint Failed: $FILE\033[0m"
|
|
23
|
-
PASS=false
|
|
24
|
-
fi
|
|
25
|
-
done
|
|
26
|
-
|
|
27
|
-
echo "\nJavascript validation completed!\n"
|
|
28
|
-
|
|
29
|
-
if ! $PASS; then
|
|
30
|
-
echo "\033[41mESLINT FAILED:\033[0m Your commit contains files that should pass ESLint but do not. Please fix the ESLint errors and try again.\n"
|
|
31
|
-
exit 1
|
|
32
|
-
else
|
|
33
|
-
echo "\033[42mESLINT SUCCEEDED\033[0m\n"
|
|
34
|
-
fi
|
|
35
|
-
|
|
36
|
-
exit $?
|