Node.js 如何使用jest测试电子ipc事件?
我正在为我正在构建的电子应用程序做一些测试。我遇到了下面的错误。我不太会开玩笑,所以我想这是因为设置不正确。知道我哪里出错了吗Node.js 如何使用jest测试电子ipc事件?,node.js,unit-testing,jestjs,electron,wallaby.js,Node.js,Unit Testing,Jestjs,Electron,Wallaby.js,我正在为我正在构建的电子应用程序做一些测试。我遇到了下面的错误。我不太会开玩笑,所以我想这是因为设置不正确。知道我哪里出错了吗 错误:无法从“ipcMainEvents.spec.js”中找到模块“ipcMain” //myEvents.ts import { ipcMain } from 'electron' export class IpcMainEvents { constructor() { ipcMain.on('openPlaywright', this
错误:无法从“ipcMainEvents.spec.js”中找到模块“ipcMain”
//myEvents.ts
import { ipcMain } from 'electron'
export class IpcMainEvents {
constructor() {
ipcMain.on('openPlaywright', this.openPlaywright)
ipcMain.on('openPreviewBrowser', this.openPlaywright)
}
openPlaywright(event, arg) {
console.log('openPlaywright')
}
openPreviewBrowser(event, arg) {
console.log('openPreviewBrowser')
}
}
//myEvents.spec.ts
import {IpcMainEvents} from './ipcMainEvents'
import {ipcMain} from 'electron'
jest.mock('ipcMain')
describe('Should test the ipcMain events', () => {
let component;
let addSpy
beforeEach(()=>{
component = new IpcMainEvents()
})
it('should attach the eventListeners', () => {
expect(component.ipcMain.on.calls.all()[0].args[0]).toEqual('openPlaywright'); //<----Errors here
expect(component.ipcMain.on.calls.all()[1].args[0]).toEqual('openPreviewBrowser');
expect(component.ipcMain.on.calls.count()).toEqual(2);
});
});
//myEvents.ts
从“electron”导入{ipcMain}
导出类事件{
构造函数(){
ipcMain.on('openPlaywright',this.openPlaywright)
ipcMain.on('openPreviewBrowser',this.openPlaywright)
}
openPlaywright(事件,arg){
console.log('openPlaywright')
}
openPreviewBrowser(事件,参数){
console.log('openPreviewBrowser')
}
}
//myEvents.spec.ts
从“/IpcMainEvents”导入{IpcMainEvents}
从“electron”导入{ipcMain}
开玩笑的模仿('ipcMain'))
描述('应测试ipcMain事件',()=>{
let组件;
让我来做间谍
在每个之前(()=>{
组件=新的IpcMainEvents()
})
它('应该附加eventListeners',()=>{
expect(component.ipcMain.on.calls.all()[0].args[0]).toEqual('openPlaywright');//首先,应该模拟electron
包,而不是ipcMain
函数
其次,您应该通过访问mock函数的calls
属性
例如
myEvents.ts
:
从“electron”导入{ipcMain};
导出类事件{
构造函数(){
ipcMain.on(‘openPlaywright’,this.openPlaywright);
ipcMain.on('openPreviewBrowser',this.openplaystright);
}
openPlaywright(事件,arg){
console.log(“openPlaywright”);
}
openPreviewBrowser(事件,参数){
log('openPreviewBrowser');
}
}
myEvents.spec.ts
:
从“/myEvents”导入{IpcMainEvents};
从“electron”导入{ipcMain};
开玩笑(
“电子”,
() => {
常量mockIpcMain={
on:jest.fn().mockReturnThis(),
};
返回{ipcMain:mockIpcMain};
},
{virtual:true},
);
描述('应测试ipcMain事件',()=>{
let组件;
让我们来侦察;
在每个之前(()=>{
组件=新的IpcMainEvents();
});
它('应该附加eventListeners',()=>{
expect(ipcMain.on.mock.calls[0][0]).toEqual(“OpenPlayWriter”);
expect(ipcMain.on.mock.calls[1][0]).toEqual('openPreviewBrowser');
expect(ipcMain.on.mock.calls).toHaveLength(2);
});
});
单元测试结果和覆盖率报告:
PASS stackoverflow/61562193/myEvents.spec.js(10.657s)
应该测试主事件
✓ 应附加eventListeners(3ms)
-------------|---------|----------|---------|---------|-------------------
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s
-------------|---------|----------|---------|---------|-------------------
所有文件| 80 | 100 | 50 | 77.78 |
myEvents.js | 80 | 100 | 50 | 77.78 | 10,14
-------------|---------|----------|---------|---------|-------------------
测试套件:1个通过,共1个
测试:1项通过,共1项
快照:共0个
时间:11.917秒