The documentation says the mock should be placed in the same parent directory as the 'node_modules' folder, e.g.
var lodash = require('lodash'); // /__mocks__/lodash.js :)
.
โโโ __mocks__
โย ย โโโ lodash.js
โโโ node_modules
โโโ lodash
โโโ index.js
This works fine however I wondered how you would mock a non-main module within a package, e.g.
var lodash = require('lodash/array/compact'); // Where should the manual mock live?
I gave this a shot but I'm guessing it's not correct:
.
โโโ __mocks__
โย ย โโโ lodash
โย ย โโโ array
โย ย โโโ compact.js // ?
โโโ node_modules
โโโ lodash
โโโ index.js
In addition I've noticed that package mocks seem to be picked up in any '__mocks__' dir, even those which aren't adjacent to the 'node_modules' dir, e.g.
var lodash = require('lodash'); // /src/__mocks__/lodash.js :)
.
โโโ node_modules
โย ย โโโ lodash
โย ย โโโ index.js
โโโ src
โโโ __mocks__
โโโ lodash.js
Any insight would be much appreciated.
Any update? I have same problem.
Does anybody have some workaround of this issue?
There is no workaround for this yet. Blocked on #599. I'll likely add a way for jest to properly mock node_modules (and internals) and also will make it more strict to colocate mocks.
Manually call setMock will help.
I want to mock some/other/library in node_modules, then I create the path under __mocks__ folder, like:
__mocks__/
--some/
----other/
------library.js
Then call jest.setMock('some/other/library', require.requireActual('../__mocks__/some/other/library') in the test. After that when calling require('some/other/library'), jest will use the mock instead.
Please check https://github.com/zyeeda/cde.io/tree/bleeding-edge/src/config for example.
I recommend using jest.mock('path/to/module', () => {โฆ}) instead.
Most helpful comment
I recommend using
jest.mock('path/to/module', () => {โฆ})instead.