Jest: doMock doesn't work as intended

Created on 24 Jul 2017  路  1Comment  路  Source: facebook/jest

My component uses the external function that I want to mock just in one of my tests.

If I use

jest.mock('utils/deviceUtils', () => ({
      getDeviceInfo: jest.fn(() => ({
        isSupportedTouchDevice: true,
        isAndroid: true,
        isMobile: true,
      }))
    }))

mock works for every test (getDeviceInfo() is not called in the test, only in component, if that's important).

If I use jest.doMock('utils/deviceUtils, /* ... */ in required test nothing is mocked.

beforeEach(() => {
  jest.resetModules()
})

doesn't help.

Most helpful comment

You probably have modules required already, you need to re-require everything after calling jest.doMock.

jest.resetModules();
jest.doMock('MyModule');
require('MyModule');

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions. Thank you :)

>All comments

You probably have modules required already, you need to re-require everything after calling jest.doMock.

jest.resetModules();
jest.doMock('MyModule');
require('MyModule');

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions. Thank you :)

Was this page helpful?
0 / 5 - 0 ratings