Search code examples
node.jstypescriptjestjsts-jest

mock functions don't work in .test files after adding setupFilesAfterEnv


After adding setupFilesAfterEnv in the jest.config.js like that:

module.exports = {
    preset: 'ts-jest',
    testEnvironment: 'node',

    setupFilesAfterEnv: ["./test/setupAfterEnv.ts"]  
}

the mock functions don't work anymore in the .test.ts files but works in the file that was written inside the setupFilesAfterEnv like ./test/setupAfterEnv.ts

mock function example(the .test.ts file and setupAfterEnv.ts in the same director):

jest.mock("../src/service/sendEmail.ts", () => ({
    send: () => {
        return "MOCK sendEmail sent"
    }
}));
  • I don't get any errors, and the other function that should be mocked runs. When I write wrong path in the mock function, I get the error can't find module. So it looks it find the function that should be mocked but doesn't run the mock function and run the other function instead

  • And if I commented the setupFilesAfterEnv config in jest.config.js it works again.


Solution

  • Documentation states

    Warning: Importing a module in a setup file
    (as specified by setupTestFrameworkScriptFile)
    will prevent mocking for the module in question, 
    as well as all the modules that it imports.

    If you have any imports/requires in your ./test/setupAfterEnv.ts, they likely imported other files, and anything chain imported in that process will not be able to be mock('../somemodule'); within tests.

    I'm not sure why this restriction is in place; if you call the mock('../somemodule') within the setupAfterEnv.ts file itself, it will be mocked later within your tests. But I consider that bad, as only some tests may want to mock some modules, and others expect them not to be mocked. A pity. In my case, I wanted to import a typeorm library and connect to the database. But the class that creates this connection imports all our entities and all the entity classes import other classes which import other classes etc etc... so nothing becomes mockable. Simply unusable because of this restriction.