Closed yordis closed 5 years ago
@yordis I'm running into the same issue, how did you solve this?
import { GlobalWithFetchMock } from 'jest-fetch-mock';
const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock;
customGlobal.fetch = require('jest-fetch-mock');
customGlobal.fetchMock = customGlobal.fetch;
Usage:
afterEach(() => {
fetchMock.resetMocks();
});
expect(fetchMock.mock.calls[0][0].method).toEqual('GET');
I think this stop failing for me, I don't remember.
@yordis These links aren't available =(
@viT-1 let me fix it, I am so sorry
just found another setup configuration (https://github.com/jefflau/jest-fetch-mock/issues/82#issuecomment-475067797):
in jest setup file:
require('jest-fetch-mock').enableMocks();
in test:
import { FetchMock } from 'jest-fetch-mock';
const fetchMock = fetch as FetchMock;
Your "in test" part isn't actually necessary though it will fix the issue.
As stated in the README, if you have an issue with "fetchMock" not being found, add a "global.d.ts" file at the root of the repository (or next to your tsconfig.json file) with the text import "jest-fetch-mock"
. That should allow all your tests to reference the "fetchMock" global variable without adding that snippet.
I am not sure what I am doing wrong since I following the documentation
tests
it keeps saying
Cannot find name 'fetchMock' TS2304