Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何用jest模拟es6构造函数_Javascript_Ecmascript 6_Mocking_Jestjs - Fatal编程技术网

Javascript 如何用jest模拟es6构造函数

Javascript 如何用jest模拟es6构造函数,javascript,ecmascript-6,mocking,jestjs,Javascript,Ecmascript 6,Mocking,Jestjs,在阅读了大量的jest文档后,我无法进行简单的设置。我有一个非常简单的示例,它抛出了错误: expect(received).toHaveBeenCalledTimes(expected) Matcher error: received value must be a mock or spy function Received has type: function Received has value: [Function Foo] jest文档说“调用jest.mock('./sound

在阅读了大量的jest文档后,我无法进行简单的设置。我有一个非常简单的示例,它抛出了错误:

expect(received).toHaveBeenCalledTimes(expected)
Matcher error: received value must be a mock or spy function
Received has type:  function
Received has value: [Function Foo]
jest文档说“调用jest.mock('./sound player')返回一个有用的“自动模拟”,您可以使用它监视对类构造函数及其所有方法的调用。它用模拟构造函数替换ES6类,并用总是返回未定义的模拟函数替换其所有方法

基于这些信息,我不确定为什么以下代码不起作用。我正在寻找最简单的实现来构建,这样我可以继续学习。如果有人能解释为什么它不起作用,并为我指出解决方案的方向…)

foo.js

导出默认类Foo{
构造函数(){
console.log('CONSTRUCTOR')
返回“foo”
}
}
foo.test.js

从'@org/Foo'导入Foo'
jest.mock(“@org/foo”)
它('should…',function(){
新Foo();
期望(Foo).达到要求的时间(1)
});

它对我有用。需要更多信息。它对我有用。需要更多信息。