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

Commit c04bdd7

Browse files
authored
Merge pull request #12 from wheresrhys/andypearson
Andypearson
2 parents f92b61c + e55d5a5 commit c04bdd7

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

__tests__/jest-extensions.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@
22
jest.mock('node-fetch', () => require('../server').sandbox());
33
const fetch = require('node-fetch');
44
describe('jest extensions', () => {
5+
describe('when no calls', () => {
6+
beforeAll(() => {
7+
fetch.mock('*', 200);
8+
});
9+
afterAll(() => fetch.reset());
10+
it('toHaveFetched should be falsy', () => {
11+
expect(fetch).not.toHaveFetched('http://example.com/path');
12+
});
13+
14+
it('toHaveLastFetched should be falsy', () => {
15+
expect(fetch).not.toHaveLastFetched('http://example.com/path');
16+
});
17+
18+
it('toHaveNthFetched should be falsy', () => {
19+
expect(fetch).not.toHaveNthFetched(1, 'http://example.com/path');
20+
});
21+
22+
it('toHaveFetchedTimes should be falsy', () => {
23+
expect(fetch).not.toHaveFetchedTimes(1, 'http://example.com/path');
24+
});
25+
26+
it('toBeDone should be falsy', () => {
27+
expect(fetch).not.toBeDone();
28+
expect(fetch).not.toBeDone('http://example.com/path');
29+
});
30+
});
531
describe('toHaveFetched', () => {
632
beforeAll(() => {
733
fetch.mock('*', 200);

jest-extensions.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@ expect.extend({
99
};
1010
},
1111
toHaveLastFetched: (fetchMock, url, options) => {
12-
const lastCall = [...fetchMock.calls()].pop();
12+
const allCalls = fetchMock.calls();
13+
if (!allCalls.length) {
14+
return {
15+
pass: false,
16+
message: () => `No calls made to fetch`
17+
};
18+
}
19+
const lastCall = [...allCalls].pop();
1320
const lastUrlCall = [...fetchMock.calls(url, options)].pop();
1421
if (lastCall === lastUrlCall) {
1522
return { pass: true };
1623
}
1724
return {
1825
pass: false,
19-
message: () => `Last call to fetch should have had a URL of ${url}`
26+
message: () =>
27+
`Last call to fetch should have had a URL of ${url} but was ${lastCall.url}`
2028
};
2129
},
2230

@@ -28,7 +36,8 @@ expect.extend({
2836
}
2937
return {
3038
pass: false,
31-
message: () => `${n}th call to fetch should have had a URL of ${url}`
39+
message: () =>
40+
`${n}th call to fetch should have had a URL of ${url} but was ${nthCall.url}`
3241
};
3342
},
3443

@@ -52,7 +61,9 @@ expect.extend({
5261
return {
5362
pass: false,
5463
message: () =>
55-
'fetch has not been called the expected number of times in total'
64+
`fetch has not been called the expected number of times ${
65+
matcher ? `for ${matcher}` : 'in total'
66+
}`
5667
};
5768
}
5869
});

0 commit comments

Comments
 (0)