Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit 87245ef

Browse files
author
Tobias Lengsholz
committed
Allow for the use of jest expect matchers in body
1 parent 7a9faeb commit 87245ef

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

__tests__/jest-extensions-post.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,25 @@ describe('jest extensions - post', () => {
3636
headers: {
3737
test: 'header',
3838
},
39+
body: {
40+
test: {
41+
testBody: 'value',
42+
},
43+
},
3944
});
4045
fetch('http://example.com/path', {
4146
method: 'post',
4247
headers: {
4348
test: 'header',
4449
},
50+
body: {
51+
test: {
52+
value: 'value',
53+
},
54+
otherValue: {
55+
value: 'value',
56+
},
57+
},
4558
});
4659
});
4760
afterAll(() => fetch.reset());
@@ -80,6 +93,31 @@ describe('jest extensions - post', () => {
8093
},
8194
});
8295
});
96+
97+
it('matches when using jest expect matchers', () => {
98+
expect(fetch).toHavePosted('http://example.com/path', {
99+
method: 'post',
100+
headers: {
101+
test: 'header',
102+
},
103+
body: expect.objectContaining({
104+
test: {
105+
value: 'value',
106+
},
107+
}),
108+
});
109+
expect(fetch).toHavePosted('end:path2', {
110+
method: 'post',
111+
headers: {
112+
test: 'header',
113+
},
114+
body: {
115+
test: {
116+
testBody: expect.stringContaining('val'),
117+
},
118+
},
119+
});
120+
});
83121
});
84122
describe('toHaveLastPosted', () => {
85123
beforeAll(() => {

jest-extensions.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const {
44
printExpected,
55
matcherHint,
66
} = require('jest-matcher-utils');
7+
const { equals } = require('@jest/expect-utils');
78
const chalk = require('chalk');
89

910
const callsAreEqual = (c1, c2) => {
@@ -62,6 +63,55 @@ const methodlessExtensions = {
6263
}
6364

6465
if (fetchMock.called(url)) {
66+
// check if body when accounting for jest matches
67+
const matchesWithJestMatchers = fetchMock
68+
.filterCalls(url)
69+
.some(([, iteratedOptions]) => {
70+
if (
71+
iteratedOptions.method &&
72+
options.method &&
73+
iteratedOptions.method.toLowerCase() !==
74+
options.method.toLowerCase()
75+
)
76+
return false;
77+
78+
try {
79+
const parsedBody =
80+
typeof iteratedOptions.body === 'string'
81+
? JSON.parse(iteratedOptions.body)
82+
: iteratedOptions.body;
83+
if (!equals(parsedBody, options.body)) return false;
84+
} catch (e) {
85+
// eslint-disable-next-line no-console
86+
console.error(
87+
'Unable to parse body of mock call',
88+
iteratedOptions.body
89+
);
90+
return false;
91+
}
92+
93+
try {
94+
const parsedHeaders =
95+
typeof iteratedOptions.headers === 'string'
96+
? JSON.parse(iteratedOptions.headers)
97+
: iteratedOptions.headers;
98+
if (!equals(parsedHeaders, options.headers)) return false;
99+
} catch (e) {
100+
// eslint-disable-next-line no-console
101+
console.error(
102+
'Unable to parse headers of mock call',
103+
iteratedOptions.body
104+
);
105+
return false;
106+
}
107+
108+
return true;
109+
});
110+
111+
if (matchesWithJestMatchers) {
112+
return { pass: true };
113+
}
114+
65115
const method = options && options.method ? options.method : 'get';
66116
const [humanVerb] = methodVerbMap
67117
.find((verbMethod) => verbMethod.includes(`:${method}`))

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)