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

Commit

Permalink
Allow for the use of jest expect matchers in body
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Lengsholz committed Aug 8, 2022
1 parent 7a9faeb commit 87245ef
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
38 changes: 38 additions & 0 deletions __tests__/jest-extensions-post.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,25 @@ describe('jest extensions - post', () => {
headers: {
test: 'header',
},
body: {
test: {
testBody: 'value',
},
},
});
fetch('http://example.com/path', {
method: 'post',
headers: {
test: 'header',
},
body: {
test: {
value: 'value',
},
otherValue: {
value: 'value',
},
},
});
});
afterAll(() => fetch.reset());
Expand Down Expand Up @@ -80,6 +93,31 @@ describe('jest extensions - post', () => {
},
});
});

it('matches when using jest expect matchers', () => {
expect(fetch).toHavePosted('http://example.com/path', {
method: 'post',
headers: {
test: 'header',
},
body: expect.objectContaining({
test: {
value: 'value',
},
}),
});
expect(fetch).toHavePosted('end:path2', {
method: 'post',
headers: {
test: 'header',
},
body: {
test: {
testBody: expect.stringContaining('val'),
},
},
});
});
});
describe('toHaveLastPosted', () => {
beforeAll(() => {
Expand Down
50 changes: 50 additions & 0 deletions jest-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
printExpected,
matcherHint,
} = require('jest-matcher-utils');
const { equals } = require('@jest/expect-utils');
const chalk = require('chalk');

const callsAreEqual = (c1, c2) => {
Expand Down Expand Up @@ -62,6 +63,55 @@ const methodlessExtensions = {
}

if (fetchMock.called(url)) {
// check if body when accounting for jest matches
const matchesWithJestMatchers = fetchMock
.filterCalls(url)
.some(([, iteratedOptions]) => {
if (
iteratedOptions.method &&
options.method &&
iteratedOptions.method.toLowerCase() !==
options.method.toLowerCase()
)
return false;

try {
const parsedBody =
typeof iteratedOptions.body === 'string'
? JSON.parse(iteratedOptions.body)
: iteratedOptions.body;
if (!equals(parsedBody, options.body)) return false;
} catch (e) {
// eslint-disable-next-line no-console
console.error(
'Unable to parse body of mock call',
iteratedOptions.body
);
return false;
}

try {
const parsedHeaders =
typeof iteratedOptions.headers === 'string'
? JSON.parse(iteratedOptions.headers)
: iteratedOptions.headers;
if (!equals(parsedHeaders, options.headers)) return false;
} catch (e) {
// eslint-disable-next-line no-console
console.error(
'Unable to parse headers of mock call',
iteratedOptions.body
);
return false;
}

return true;
});

if (matchesWithJestMatchers) {
return { pass: true };
}

const method = options && options.method ? options.method : 'get';
const [humanVerb] = methodVerbMap
.find((verbMethod) => verbMethod.includes(`:${method}`))
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 87245ef

Please sign in to comment.