jefflau / jest-fetch-mock

Jest mock for fetch
MIT License
881 stars 116 forks source link

Jest Mocks Behavior is Inconsistent When Defined in External File #233

Open jeff-simeon opened 2 years ago

jeff-simeon commented 2 years ago

I can't figure out why my jest mocks aren't working consistently. I understand the behavior about mocks being lifted, but I don't see how/why that would break my code in this case.

The following works

import fetchMock from 'jest-fetch-mock';

fetchMock.enableMocks();
fetchMock.dontMock();

fetchMock.doMockIf('...', async () => {
  return JSON.stringify([
    {
      AccountName: '...'
    }
  ]);
});

describe('....', () => {
  let sut = ....

and my fetch call is mocked

If I do the following, my mock fetch is never called

import './mocks';

describe('....', () => {
  let sut = ....

mocks.ts:

import fetchMock from 'jest-fetch-mock';

fetchMock.enableMocks();
fetchMock.dontMock();

fetchMock.doMockIf('...', async () => {
  return JSON.stringify([
    {
      AccountName: '...'
    }
  ]);
});

Any ideas about what's going on here?

Thanks.