Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.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测试为eventemitter对象(http)发出事件_Javascript_Node.js_Jestjs - Fatal编程技术网

Javascript jest测试为eventemitter对象(http)发出事件

Javascript jest测试为eventemitter对象(http)发出事件,javascript,node.js,jestjs,Javascript,Node.js,Jestjs,假设下面的nodejs代码 const server=http.listen(8080,'127.0.0.1') .on(“错误”,err=>{ // ... }) module.exports=服务器; 如何使用jest编写测试以发出http“error”事件(覆盖错误事件处理程序)?由于您在模块范围内创建了一个服务器,因此当您需要或导入服务器.js时,代码将立即执行。在需要此模块之前,您需要存根http.createServer 对于测试.on(error,callback)方法,您应该使

假设下面的nodejs代码

const server=http.listen(8080,'127.0.0.1')
.on(“错误”,err=>{
// ...
})
module.exports=服务器;

如何使用jest编写测试以发出http“error”事件(覆盖错误事件处理程序)?

由于您在模块范围内创建了一个服务器,因此当您
需要
导入
服务器.js
时,代码将立即执行。在需要此模块之前,您需要存根
http.createServer

对于测试
.on(error,callback)
方法,您应该使用
mockImplementation
mockImplementationOnce
,因此当模拟服务器调用模拟的
。on('error',callback)
,您将在测试用例中获得原始回调。这意味着
处理程序
相当于
回调
。调用
处理程序(mError)
时,模拟的错误对象将被传递到原始
回调中。然后可以使用此
mError
测试代码逻辑

以下是单元测试解决方案:

server.js

consthttp=require('http');
const server=http.createServer();
server.listen(8080,'127.0.0.1')。on('error',(err)=>{
控制台日志(err);
});
module.exports=服务器;
server.test.js

consthttp=require('http');
描述('60435647',()=>{
它('应该处理错误',()=>{
const mError=新错误(“网络”);
常量mServer={
听:jest.fn().mockReturnThis(),
on:jest.fn().mockImplementationOnce((事件,处理程序)=>{
//handler是原始回调函数,mError变量将被传递到原始回调函数中。
handler(mError);
}),
};
const createServerSpy=jest.spyOn(http,'createServer').mockImplementationOnce(()=>mServer);
constlogspy=jest.spyOn(控制台,'log');
需要('./服务器');
expect(createServerSpy).toBeCalledTimes(1);
expect(mServer.listen).toBeCalledWith(8080,'127.0.0.1');
expect(mServer.on).toBeCalledWith('error',expect.any(Function));
expect(logSpy).与(mError)一起调用;
});
});
100%覆盖率的单元测试结果:

PASS stackoverflow/60435647/server.test.js
60435647
✓ 应处理错误(459ms)
console.log node_modules/jest environment/node_modules/jest mock/build/index.js:866
错误:网络
反对。(/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/stackoverflow/60435647/server.test.js:5:20)
在Object.asyncJestTest(/Users/ldu020/workspace/github.com/mrdulin/react apollo graphql初学者工具包/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:100:37)
解析时(/Users/ldu020/workspace/github.com/mrdulin/react apollo graphql初学者工具包/node_modules/jest-jasmine2/build/queueRunner.js:43:12)
在新的承诺()
在mapper(/Users/ldu020/workspace/github.com/mrdulin/react apollo graphql初学者工具包/node_modules/jest-jasmine2/build/queueRunner.js:26:19)
在promise.then(/Users/ldu020/workspace/github.com/mrdulin/react apollo graphql starter kit/node_modules/jest-jasmine2/build/queueRunner.js:73:41)
在进程中。_tick回调(内部/process/next_tick.js:68:7)
-----------|---------|----------|---------|---------|-------------------
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s
-----------|---------|----------|---------|---------|-------------------
所有文件| 100 | 100 | 100 | 100 |
server.js | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
测试套件:1个通过,共1个
测试:1项通过,共1项
快照:共0个
时间:3.772s,估计6s

由于您在模块范围内创建了一个服务器,因此当您
需要
导入
server.js
时,代码将立即执行。在需要此模块之前,您需要存根
http.createServer

对于测试
.on(error,callback)
方法,您应该使用
mockImplementation
mockImplementationOnce
,因此当模拟服务器调用模拟的
。on('error',callback)
,您将在测试用例中获得原始回调。这意味着
处理程序
相当于
回调
。调用
处理程序(mError)
时,模拟的错误对象将被传递到原始
回调中。然后可以使用此
mError
测试代码逻辑

以下是单元测试解决方案:

server.js

consthttp=require('http');
const server=http.createServer();
server.listen(8080,'127.0.0.1')。on('error',(err)=>{
控制台日志(err);
});
module.exports=服务器;
server.test.js

consthttp=require('http');
描述('60435647',()=>{
它('应该处理错误',()=>{
const mError=新错误(“网络”);
常量mServer={
听:jest.fn().mockReturnThis(),
on:jest.fn().mockImplementationOnce((事件,处理程序)=>{
//handler是原始回调函数,mError变量将被传递到原始回调函数中。
handler(mError);
}),
};
const createServerSpy=jest.spyOn(http,'createServer').mockImplementationOnce(()=>mServer);
constlogspy=jest.spyOn(控制台,'log');
需要('./服务器');
expect(createServerSpy).toBeCalledTimes(1);
expect(mServer.listen).toBeCalledWith(8080,'127.0.0.1');
expect(mServer.on).toBeCalledWith('error',expect.any(Function));
expect(logSpy).与(mError)一起调用;
});
});
100%覆盖率的单元测试结果:

PASS stackoverflow/60435647/server.test.js
60435647
✓ 应处理错误(459ms)
console.log节点\u mod