javascripttypescriptunit-testingjestjsautomocking

Jest: automock modules, but only those defined in __mocks__, rather than all


TL;DR

I would like to have some kind of automock feature enabled, but only (and only!) for the modules that I have explicitly defined in a corresponding __mocks__ folder. Is there a way for this in Jest?

General advices and suggestions are also welcome.

A bit of context: (optional)

Turns out I was totally misunderstanding Jests automock feature. Btw, looking back now, I don't understand why, 'cause docs are pretty clear on what it actually does:

This option tells Jest that all imported modules in your tests should be mocked automatically.

As if I just noticed ALL keyword. Maybe I was just thinking - but it doesn't makes sense to have an automock even for the imported function that I'm actually going to test here, does it? Like obviously I would like to automock third party stuff from node_modules, but not my own code. And it turns out that:

Note: Node modules are automatically mocked when you have a manual mock in place (e.g.: __mocks__/lodash.js).

Note: Core modules, like fs, are not mocked by default. They can be mocked explicitly, like jest.mock('fs')

So it's kind of doing the opposite of what I thought it was doing.


Solution

  • If I understand correctly, you have certain mock files in your __mocks__ folder which you would like to be globally replaced whenever they're needed as part of a test file imports.

    Jest has a means of configuring that. If you go through the Jest Configuration Documentation, you'll find a property named moduleNameMapper which takes an object with the keys being regular expressions and the values being the paths for the mock for the corresponding modules which match the regex.

    In your jest.config.js, you would need to add a separate parameter and specify the mock pattern and path.

    moduleNameMapper: {
        "/^bootstrap/": "<root>/tests/__mocks__/bootstrapMock.js",
        "/^axios/": "<root>/tests/__mocks/axiosMock.js"
    }
    

    You can find more info about the usage in the Jest docs.

    However, if you do not want to go through all this, this is also achievable by placing a __mocks__ folder next to your node_modules folder. However, as defined in the documentation for Manual Mocks:

    If we want to mock Node's core modules (e.g.: fs or path), then explicitly calling e.g. jest.mock('path') is required, because core Node modules are not mocked by default.