最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Difference between unmock and dontMock in Jest - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Apr 12, 2016 at 11:02 dooburtdooburt 3,06011 gold badges42 silver badges59 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

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 to 0.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 using babel-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.

发布评论

评论列表(0)

  1. 暂无评论