gpt4 book ai didi

javascript - Jest 中 unmock 和 dontMock 的区别

转载 作者:数据小太阳 更新时间:2023-10-29 05:54:14 26 4
gpt4 key购买 nike

所以我一直在编写一个成功的单元测试库,而且一切看起来都很顺利。然后我在一些在线示例中注意到我这样做的地方:-

jest.unmock('../lib/q');

其他人这样做:-

jest.dontMock('../lib/q');

我在 Jest 网站上找不到任何文档(老实说,文档不是很好),所以我将我的一个套件更改为 dontMock 并爆炸了很多...有什么区别?

最佳答案

这是 covered in the Jest documentation .

似乎我们现在都应该使用 unmock 来防止将 mock 提升到 ES6 导入之上:

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.

关于javascript - Jest 中 unmock 和 dontMock 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36571357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com