Javascript 如何使用jest全局模拟单个实用程序函数
假设在我的util.js中Javascript 如何使用jest全局模拟单个实用程序函数,javascript,reactjs,jestjs,enzyme,Javascript,Reactjs,Jestjs,Enzyme,假设在我的util.js中 export function foo(){ ... do something} export function bar(){ ... do something} ... 在各种组件中,我使用foo //Foo1.component.js import { foo } from './util'A ... do something using foo. 等等 例如,在Foo1.test.js中,我如何去模拟它 更好的是,有没有办法让我可以直接从jest.con
export function foo(){ ... do something}
export function bar(){ ... do something}
...
在各种组件中,我使用foo
//Foo1.component.js
import { foo } from './util'A
... do something using foo.
等等
例如,在Foo1.test.js
中,我如何去模拟它
更好的是,有没有办法让我可以直接从jest.config“劫持”这个foo
导入。
在模块映射中
可能吗
因此,它已经被模拟用于所有测试
Foo1.test.js
和Foo2.test.js
?是的,有一种方法可以模拟测试环境中的模块
如果要模拟特定测试文件中的模块,可以尝试使用jest.mock()
函数。例如,在Foo1.component.js
测试文件中模拟foo
方法如下:
// Foo1.component.test.js
jest.mock('./path/to/utils.js', () => ({
foo: () => { // custom implementation for test env }
bar: () => { // custom implementation for test env if needed }
}));
// The foo imported below has the custom implementation for test env
import { foo } from './util';
另一个选择是创建一个模拟模块,我认为这更适合您的需要。在utils.js
所在的文件夹中,创建一个\uuuu mocks\uuuu
文件夹,在该文件夹下添加一个文件utils.js
(与要模拟的模块同名),在该文件中,可以为util.js
模块编写模拟行为。例如:
// __mocks__/utils.js
export function foo(){ ... mock implementation}
export function bar(){ ... mock implementation}
// Foo1.component.test.js
jest.mock('./path/to/utils.js');
// The foo imported below has the custom implementation for test env
import { foo } from './util';
每当您在测试文件中需要这些模拟实现时,只要调用带有模块路径的jest.mock
方法即可。例如:
// __mocks__/utils.js
export function foo(){ ... mock implementation}
export function bar(){ ... mock implementation}
// Foo1.component.test.js
jest.mock('./path/to/utils.js');
// The foo imported below has the custom implementation for test env
import { foo } from './util';
链接:
是的,有一种方法可以在测试环境中模拟模块 如果要模拟特定测试文件中的模块,可以尝试使用
jest.mock()
函数。例如,在Foo1.component.js
测试文件中模拟foo
方法如下:
// Foo1.component.test.js
jest.mock('./path/to/utils.js', () => ({
foo: () => { // custom implementation for test env }
bar: () => { // custom implementation for test env if needed }
}));
// The foo imported below has the custom implementation for test env
import { foo } from './util';
另一个选择是创建一个模拟模块,我认为这更适合您的需要。在utils.js
所在的文件夹中,创建一个\uuuu mocks\uuuu
文件夹,在该文件夹下添加一个文件utils.js
(与要模拟的模块同名),在该文件中,可以为util.js
模块编写模拟行为。例如:
// __mocks__/utils.js
export function foo(){ ... mock implementation}
export function bar(){ ... mock implementation}
// Foo1.component.test.js
jest.mock('./path/to/utils.js');
// The foo imported below has the custom implementation for test env
import { foo } from './util';
每当您在测试文件中需要这些模拟实现时,只要调用带有模块路径的jest.mock
方法即可。例如:
// __mocks__/utils.js
export function foo(){ ... mock implementation}
export function bar(){ ... mock implementation}
// Foo1.component.test.js
jest.mock('./path/to/utils.js');
// The foo imported below has the custom implementation for test env
import { foo } from './util';
链接:
foo
,它还能工作吗?它会不会引用原始的util.js呢?我想我在描述的时候还不够清楚。我的utils.js文件相当大,我希望避免模拟所有这些文件。上面的实现似乎需要我这样做。如果我有一个mockfoo
,它还会工作吗?它会引用原始的util.js吗?