【发布时间】:2018-09-19 06:00:51
【问题描述】:
Jest 可以选择模拟 node_module:
jest.mock('module_name')
我要模拟的模块是cote 模块。
Here 是模块中的 index.js 文件。
里面有 Requester 的构造函数导出
const Requester = require('./components/requester');
...
cote.Requester = Requester;
...
module.exports = cote();
我在我的代码中访问这个文件是这样的:
const cote = require('cote');
const requester = new cote.Requester({name: 'findOnePlayer requester'});
...
const player = await requester.send({type:'findOnePlayer',username:username, password: password})
如何使用jest 进行设置,以便模拟cote 模块中的Requester 的构造函数返回模拟对象,该对象在您调用send 时解析承诺?
编辑:
我在很多测试中都需要这种情况,每个send(... 应该在每个测试场景中返回不同的promise。
【问题讨论】:
标签: javascript node.js unit-testing jestjs cote