下面是一个简化的工作示例,让您开始:
// @ts-ignore
import * as Api from 'npm-module'; // <= (ts-ignore since "npm-module" doesn't exist)
import EventEmitter from 'events';
jest.mock('npm-module', () => {
const getSomethingMock = jest.fn(); // <= always return...
const onUpdateMock = jest.fn(); // <= ...the same mocks...
return {
Rest: () => ({ getSomething: getSomethingMock }),
Websocket: () => ({ onUpdate: onUpdateMock })
}
},
{ virtual: true }); // <= (use virtual since "npm-module" doesn't exist)
class Component {
private _rest: any;
private _websocket: any;
public events = new EventEmitter();
constructor() {
this._rest = new Api.Rest();
this._websocket = new Api.Websocket();
this._init();
}
private _init() {
this._websocket.onUpdate((data) => { // <= ...so that this onUpdate...
this.events.emit('update', data);
});
}
}
test('Component', () => {
const component = new Component();
const listener = jest.fn();
component.events.on('update', listener);
const onUpdate = new Api.Websocket().onUpdate; // <= ...is the same as this one
const onUpdateArrowFunction = onUpdate.mock.calls[0][0]; // <= get the arrow function passed to it
onUpdateArrowFunction('mock data'); // <= now call the function
expect(listener).toHaveBeenCalledWith('mock data'); // Success!
});
细节
Jest
接管
require
系统,并允许您指定在需要模块时希望它返回的内容(请注意,TypeScript
import
语句被编译为
电话)。
模拟模块的一种方法是创建
manual mock
__mocks__/npm-module.ts
里面有你的嘲笑。
jest.mock
并给它传递一个模块工厂函数。
开玩笑
将返回模拟模块。
注意,上面的例子总是返回相同的mock
getSomething
和
onUpdate
还要注意
mockFn.mock.calls
要检索此箭头函数:
(data) => {
this.events.emit('update', data);
}
…传递给
更新
. 一旦检索到它,就可以直接调用它,从而按预期触发侦听器。