【发布时间】:2019-10-06 10:14:24
【问题描述】:
我有一个组件类,如下所示,它使用第三方 npm 模块创建 rest 和 websocket 连接。我可以更改 Component.constructor 以接受模块作为依赖项,以便在我的 Jest 测试期间注入模拟版本。但是我读到了 Mocks with Jest,我想我想尝试一下,但我似乎无法理解如何拦截 Api.Rest() 和 Api.Websocket 返回值。
// component.ts
import * as Api from 'npm-module'
import * as wait from 'wait-for-stuff' // actual npm module
export class Component {
private _rest:any;
private _websocket:any;
public events = new EventEmitter();
constructor() {
// I want to intecept the return value of
// Api.Rest() and Api.Websocket() to use mock versions.
this._rest = new Api.Rest();
this._websocket = new Api.Websocket();
this._init();
}
private _init() {
// so that when do stuff with this._rest and this._websocket;
// I can control what is the expected results during test
this._websocket.onUpdate((data) => {
events.emit('update', data);
});
var value = wait.for.promise(this._rest.getSomething());
}
}
我是否必须使用其他测试库,例如 Sinon 或 Jasmine?
【问题讨论】:
标签: node.js jasmine jestjs sinon