So I've been writing a successful unit test library and all seems well. And then I noticed in some online examples that where I do this:-
jest.unmock('../lib/q');
Others do this:-
jest.dontMock('../lib/q');
I can't find any documentation on the Jest site (the documentation isn't great let's be honest), so I changed one of my suites for a giggle to dontMock
and quite a lot exploded ... What's the difference?
This is covered in the Jest documentation.
Seems that we should all use unmock
now to prevent hoisting of the mocked to above the ES6 import:
I'm using babel and my unmocked imports aren't working?
Upgrade
jest-cli
to0.9.0
.Explanation:
jest.dontMock('foo'); import foo from './foo';
In ES2015, import statements get hoisted before all other
var foo = require('foo'); jest.dontMock('foo'); // Oops!
In Jest 0.9.0, a new API
jest.unmock
was introduced. Together with a plugin for babel, this will now work properly when usingbabel-jest
:jest.unmock('foo'); // Use unmock! import foo from './foo'; // foo is not mocked!
See the Getting Started guide on how to enable babel support.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With