jest-matcher-http 1.3.0 → 1.4.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.
@@ -68,7 +68,7 @@ describe('axios', () => {
68
68
  }
69
69
 
70
70
  expect(caughtError.message).toContain(
71
- `expected http header "${headerName.toLowerCase()}" with value ""\n`
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()}" with value ""\n`
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()}" with value ""\n`
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,6 +1,6 @@
1
1
  {
2
2
  "name": "jest-matcher-http",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Additional Jest matchers for HTTP responses.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
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} to equal ${expectedHttpStatusCode}\n\n`
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}" with value "${headerValue}"\n\n`
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" with value "some-uuid"\n'
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
  });