jest-fetch-mock icon indicating copy to clipboard operation
jest-fetch-mock copied to clipboard

Jest Mocks Behavior is Inconsistent When Defined in External File

Open jeff-simeon opened this issue 3 years ago • 0 comments

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.

jeff-simeon avatar Apr 19 '22 00:04 jeff-simeon