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.