Reputation: 42454
I know how to mock a module through jest
in my unit tests. But my problem is that how I can mock a module which is required by a dependency. For example, I have dependency A
defined in my package.json
file while A
dependents on B
. So in my unit tests how I can mock B
which is used by A
?
Upvotes: 0
Views: 1006
Reputation: 23705
jest.mock
works well for any-level dependency. So
jet.mock('B');
at the start of test for A
will work.
Also you can provide auto-mocks by placing mocks into special folder named __mocks__
. So once B
is in node_modules
we need to have mock in sibling __mocks__
directory next to node_modules
like
- node_modules
-- B
--- index.js
- __mocks__
-- B.js
And finally beware of
Warning: 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.
Upvotes: 1