Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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来监视第三方?_Javascript_Unit Testing_Jestjs - Fatal编程技术网

Javascript 如何使用jest来监视第三方?

Javascript 如何使用jest来监视第三方?,javascript,unit-testing,jestjs,Javascript,Unit Testing,Jestjs,我很难模仿第三方依赖关系。我总是收到这个错误: 无法监视未定义的属性,因为它不是函数; 未定义,请改为给定 以下是这个问题的细节。首先,这是我正在测试的功能: 文件:src/js/mp_wrapper.js 从“第三方”导入{Viewer}; module.exports={ createViewer:container=>{ 如果(使用isElement(容器)){ 返回新的查看器(容器); }否则{ 抛出新错误( “尝试创建基础查看器时元素无效。”, ); } }, } 查看我的第三方的源

我很难模仿第三方依赖关系。我总是收到这个错误:

无法监视未定义的属性,因为它不是函数; 未定义,请改为给定

以下是这个问题的细节。首先,这是我正在测试的功能:

文件:
src/js/mp_wrapper.js

从“第三方”导入{Viewer};
module.exports={
createViewer:container=>{
如果(使用isElement(容器)){
返回新的查看器(容器);
}否则{
抛出新错误(
“尝试创建基础查看器时元素无效。”,
);
}
},
}
查看我的第三方的源代码,
Viewer
非常简单,如下所示:


函数查看器(){
//做事
}
Viewer.prototype.foo=函数(){
}
module.exports=查看器;
最后,这是我的测试

文件:
/tests/mp_wrapper.spec.js


从“第三方”导入{Viewer};
从“../src/js/mp_wrapper”导入mp_wrapper;
描述('mp_包装器',()=>{
描述('createViewer',()=>{
test('返回查看器类的新实例',()=>{
const spy=jest.spyOn(Viewer.mockImplementation(()=>jest.fn());
//它在上面的行中失败…->“无法检测未定义的属性,因为它不是函数;而是未定义的给定”
const testElement=document.createElement(testElement);
让viewer=mp_wrapper.createViewer(testElement);
期望(间谍)。已被调用();
expect(查看器).toBeInstancecOf(查看器);
spy.mockRestore();
});
});
});
如何模拟和监视查看器本身?

我过去曾这样做过:

constspy=jest.spyOn(Viewer.prototype,'foo').mockImplementation(()=>jest.fn())

我还尝试了
default
,但运气不佳:

constspy=jest.spyOn(查看器,'default').mockImplementation(()=>jest.fn())

但现在我想监视观者

编辑:

这是我最后的解决办法@布赖恩住在户外,答案是正确的,但我并没有准确地描述我的问题。我试图模拟的第三方库稍微复杂一些,因为它导出一个包含多个构造函数的模块。看起来是这样的:

module.exports = {
  Viewer: require('./path/Viewer'),
  Foo: require('./foo_path/Foo'),
  Bar: require('./bar_path/Bar')
}
import * as lib from './lib';

const spy = jest.spyOn(lib, 'default');  // <= spy on the default export
然后,内部
/path/Viewer
与我前面描述的一样

以下是我的解决方案的最终结果:

从'lib'导入{Viewer};
从“../src/js/mp_wrapper”导入mp_wrapper;
jest.genMockFromModule('lib');
开玩笑的模仿('lib');
描述('mp_包装器',()=>{
描述('createViewer',()=>{
test('返回查看器类的新实例并设置本地_Viewer属性',()=>{
const testContainer=document.createElement('div');
const viewer=mp_wrapper.createViewer(testContainer);
expect(Viewer).toHaveBeenCalledWith(testContainer);//成功!
expect(查看器).toBeInstanceOf(查看器);//成功!
});
});
});
@布赖恩住在户外,我不明白的是,如果我注释掉了行
jest.mock('lib')不起作用…为什么

为什么
genMockFromModule
本身不够

这里有一个解决方案:

util.js

const util={
isElement(){}
};
module.exports=util;
View.js
,第三方模块:

函数查看器(){
//做事
log('newviewer实例');
}
Viewer.prototype.foo=函数(){};
module.exports={Viewer};
my_wrapper.js

const{Viewer}=require('./Viewer');
const util=require('./util');
module.exports={
createViewer:container=>{
如果(使用isElement(容器)){
返回新的查看器(容器);
}否则{
抛出新错误('尝试创建基础查看器时元素无效');
}
}
};
单元测试:

const{Viewer}=require('./Viewer');
const my_wrapper=require('./');
const util=require('./util');
嘲笑('./观众',()=>{
返回{
查看者:jest.fn()
};
});
描述('mp_包装器',()=>{
在每个之前(()=>{
jest.resetAllMocks();
});
描述('createViewer',()=>{
它('t1',()=>{
util.isElement=jest.fn().mockReturnValueOnce(true);
让viewer=my_wrapper.createViewer('el');
expect(util.isElement).toBeCalledWith('el');
expect(查看器).toBeInstanceOf(查看器);
});
它('t2',()=>{
util.isElement=jest.fn().mockReturnValueOnce(false);
expect(()=>my_wrapper.createViewer('el')).TothRower错误(
新错误('尝试创建基础查看器时元素无效')
);
expect(查看器).not.toBeCalled();
});
});
});
单元测试结果:

PASS src/stackoverflow/57712713/index.spec.js
包装纸
createViewer
✓ t1(6ms)
✓ t2(5ms)
----------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
----------|----------|----------|----------|----------|-------------------|
所有文件| 100 | 100 | 50 | 100 ||
index.js | 100 | 100 | 100 | 100 ||
util.js | 100 | 100 | 0 | 100 ||
----------|----------|----------|----------|----------|-------------------|
测试套件:1个通过,共1个
测试:2次通过,共2次
快照:共0个
时间:4.134秒,估计9秒

示例代码将ES6
导入
/
导出
语法与节点
模块混合。导出
语法

…但基于这样一个库:

module.exports = {
  Viewer: require('./path/Viewer'),
  Foo: require('./foo_path/Foo'),
  Bar: require('./bar_path/Bar')
}
import * as lib from './lib';

const spy = jest.spyOn(lib, 'default');  // <= spy on the default export
import * as lib from './lib'; const spy = jest.spyOn(lib, 'default'); // <= spy on the default export
const mock = jest.genMockFromModule('lib');
const lib = jest.genMockFromModule('lib');  // <= generate a mock of the module
lib.someFunc.mockReturnValue('some value');  // <= modify it
module.exports = lib;  // <= export the modified mock
jest.genMockFromModule('lib');
jest.mock('lib');
jest.genMockFromModule('lib');
jest.mock('lib');