Skip to content
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

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

Open
jeff-simeon opened this issue Apr 19, 2022 · 0 comments
Open

Comments

@jeff-simeon
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant