I need to change a file in a node module which is used by a dependency I'm currently using.
Is there a way I can mock a specific file in node_modules so jest uses it instead of the original one?
I tried creating the same path and file in __mocks__ but it kept using the original file.
Jest Config:
module.exports = {
testEnvironment: 'node',
setupFilesAfterEnv: ['./jestSetup.js'],
testMatch: ['**/test/**/?(*.)+(spec|test).js'],
testTimeout: 10000,
verbose: true,
bail: 1,
clearMocks: true,
coverageDirectory: 'coverage',
resetModules: true,
restoreMocks: true,
};
Make sure the __mocks__ directory is adjacent to node_modules (your project root). Jest will automatically use the mock.
Here is a minimum setup that works: https://codesandbox.io/s/stack-overflow-jest-mocks-qejos0?file=/foo.test.js