Skip to content

Commit

Permalink
Merge pull request #15 from wheresrhys/play-nice-resetall
Browse files Browse the repository at this point in the history
don't break when using jest.restoreAllMocks()
  • Loading branch information
wheresrhys authored May 4, 2020
2 parents 540effc + facdf20 commit 5f3e846
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
1 change: 0 additions & 1 deletion __tests__/jest-built-ins.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ describe('jest built-ins', () => {
it('mockReset', () => {
expect(fetch.mockReset).toBeDefined();
fetch.mockReset();

expect(fetch.mock.calls.length).toEqual(0);
expect(fetch._calls.length).toEqual(0);
expect(fetch.routes.length).toEqual(0);
Expand Down
20 changes: 20 additions & 0 deletions __tests__/regressions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*global jest */
jest.mock('node-fetch', () => require('../server').sandbox());
const fetch = require('node-fetch');

describe('regressions and strange cases', () => {
it('works even when jest.resetAllMocks() is called', () => {
jest.resetAllMocks();
fetch.mock('*', 200);
fetch('http://example.com/path', 200);
expect(fetch).toHaveFetched('http://example.com/path');
fetch.reset();
});
it('works even when jest.clearAllMocks() is called', () => {
jest.clearAllMocks();
fetch.mock('*', 200);
fetch('http://example.com/path', 200);
expect(fetch).toHaveFetched('http://example.com/path');
fetch.reset();
});
});
26 changes: 18 additions & 8 deletions jestify.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,34 @@ const jestify = (fetchMockInstance) => {

// spy on the fetch handler so we can use all the
// jest function assertions on it
jest.spyOn(jestifiedInstance, 'fetchHandler');
const spy = jest.fn();
const originalFetchHandler = jestifiedInstance.fetchHandler.bind(
jestifiedInstance
);

jestifiedInstance.fetchHandler = function (...args) {
const result = originalFetchHandler(...args);
spy.mockReturnValueOnce(result);
spy.apply(this, args);
return result;
};

// make sure all the jest expectation helpers can find what they need on fetchMock.mock
Object.assign(jestifiedInstance.mock, jestifiedInstance.fetchHandler.mock);
Object.assign(jestifiedInstance.mock, spy.mock);

['_isMockFunction', 'mockName', 'getMockName'].forEach((prop) => {
jestifiedInstance[prop] = jestifiedInstance.fetchHandler[prop];
jestifiedInstance[prop] = spy[prop];
});

jestifiedInstance.mockClear = () => {
jestifiedInstance.fetchHandler.mockClear();
spy.mockClear();
jestifiedInstance.resetHistory();
Object.assign(jestifiedInstance.mock, jestifiedInstance.fetchHandler.mock);
Object.assign(jestifiedInstance.mock, spy.mock);
};
jestifiedInstance.mockReset = () => {
jestifiedInstance.fetchHandler.mockReset();
spy.mockReset();
jestifiedInstance.reset();
Object.assign(jestifiedInstance.mock, jestifiedInstance.fetchHandler.mock);
Object.assign(jestifiedInstance.mock, spy.mock);
};
jestifiedInstance.mockRestore = () => {
throw new Error(
Expand Down Expand Up @@ -95,7 +105,7 @@ const jestify = (fetchMockInstance) => {

// make sure that the mock object that has properties updated
// by the jest spy is the one that is exposed on fetch
jestifiedInstance.fetchHandler.mock = jestifiedInstance.mock;
spy.mock = jestifiedInstance.mock;

// Return this monster!
return jestifiedInstance;
Expand Down

0 comments on commit 5f3e846

Please sign in to comment.