-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mock a cross-fetch ponyfill #122
Comments
I use unfetch and I was able to get everything working with this setup file: import { GlobalWithFetchMock } from 'jest-fetch-mock';
const customGlobal = global as GlobalWithFetchMock;
customGlobal.fetch = require('jest-fetch-mock');
customGlobal.fetchMock = customGlobal.fetch;
jest.setMock('unfetch', fetch); // Use this to mock your ponyfilled fetch module Hope this helps! |
export const mockedFetch = jest.fn() as jest.MockedFunction<typeof fetch>;
jest.mock("cross-fetch", () => ({
...jest.requireActual("cross-fetch"),
__esModule: true,
fetch: mockedFetch,
default: mockedFetch,
}));
// must come AFTER mocking!
import fetch, { Response } from "cross-fetch";
export function mockFetchOnce(status: number, json: unknown): void {
mockedFetch.mockResolvedValueOnce(new Response(JSON.stringify(json), { status }));
} |
I managed to do it by simply doing this in the setup test file: import fetchMock from "jest-fetch-mock";
jest.setMock("cross-fetch", fetchMock); You can look at the complete PR where I introduced |
ryanrhee
added a commit
to ryanrhee/jest-fetch-mock
that referenced
this issue
Jan 26, 2022
resolves jefflau#122 From jefflau#122 (comment)
This is the recommended way according to jest documentation: jest.mock('cross-fetch', () => {
const fetchMock = require('jest-fetch-mock');
// Require the original module to not be mocked...
const originalFetch = jest.requireActual('cross-fetch');
return {
__esModule: true,
...originalFetch,
default: fetchMock
};
});
import fetch from 'cross-fetch';
//Do not mock unless we explicitly request it.
fetch.dontMock();
describe('fetch', () => {
test('fetch', async () => {
fetch.mockResponse(JSON.stringify({ hello: 'world' }));
//Activate the mock
//This will not result into a query to google:
fetch.doMock();
expect(await (await fetch('https://www.google.com')).json()).toEqual({
hello: 'world'
});
//Turn off
fetch.dontMock();
//Now you can fetch() stuff and the mock won't be applied.
//This will really query google.com:
expect((await fetch('https://www.google.com')).status).toEqual(200);
});
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm currently testing a project, which uses cross-fetch ponyfills in the files (i.e.
import fetch from "cross-fetch"
).this comment in #82 suggests just using cross-fetch as polyfill instead of ponyfill, but that's not an option in my case.
Is it possible to mock the fetch imported as a module?
The text was updated successfully, but these errors were encountered: