如何使用玩笑测试电子 ipc 事件?
How to test electron ipc events using jest?
我正在为我正在构建的电子应用程序进行一些测试。我 运行 遇到以下错误。我是开玩笑的新手,所以我想这是由于设置不正确造成的。知道我哪里出错了吗?
Error: Cannot find module 'ipcMain' from 'ipcMainEvents.spec.js'
//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);
});
});
首先,你应该模拟 electron
包,而不是 ipcMain
函数。
其次,您应该通过 .mock property.
访问模拟函数的 calls
属性
例如
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 './myEvents';
import { ipcMain } from 'electron';
jest.mock(
'electron',
() => {
const mockIpcMain = {
on: jest.fn().mockReturnThis(),
};
return { ipcMain: mockIpcMain };
},
{ virtual: true },
);
describe('Should test the ipcMain events', () => {
let component;
let addSpy;
beforeEach(() => {
component = new IpcMainEvents();
});
it('should attach the eventListeners', () => {
expect(ipcMain.on.mock.calls[0][0]).toEqual('openPlaywright');
expect(ipcMain.on.mock.calls[1][0]).toEqual('openPreviewBrowser');
expect(ipcMain.on.mock.calls).toHaveLength(2);
});
});
带有覆盖率报告的单元测试结果:
PASS Whosebug/61562193/myEvents.spec.js (10.657s)
Should test the ipcMain events
✓ should attach the eventListeners (3ms)
-------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------|---------|----------|---------|---------|-------------------
All files | 80 | 100 | 50 | 77.78 |
myEvents.js | 80 | 100 | 50 | 77.78 | 10,14
-------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 11.917s
我正在为我正在构建的电子应用程序进行一些测试。我 运行 遇到以下错误。我是开玩笑的新手,所以我想这是由于设置不正确造成的。知道我哪里出错了吗?
Error: Cannot find module 'ipcMain' from 'ipcMainEvents.spec.js'
//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);
});
});
首先,你应该模拟 electron
包,而不是 ipcMain
函数。
其次,您应该通过 .mock property.
访问模拟函数的calls
属性
例如
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 './myEvents';
import { ipcMain } from 'electron';
jest.mock(
'electron',
() => {
const mockIpcMain = {
on: jest.fn().mockReturnThis(),
};
return { ipcMain: mockIpcMain };
},
{ virtual: true },
);
describe('Should test the ipcMain events', () => {
let component;
let addSpy;
beforeEach(() => {
component = new IpcMainEvents();
});
it('should attach the eventListeners', () => {
expect(ipcMain.on.mock.calls[0][0]).toEqual('openPlaywright');
expect(ipcMain.on.mock.calls[1][0]).toEqual('openPreviewBrowser');
expect(ipcMain.on.mock.calls).toHaveLength(2);
});
});
带有覆盖率报告的单元测试结果:
PASS Whosebug/61562193/myEvents.spec.js (10.657s)
Should test the ipcMain events
✓ should attach the eventListeners (3ms)
-------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------|---------|----------|---------|---------|-------------------
All files | 80 | 100 | 50 | 77.78 |
myEvents.js | 80 | 100 | 50 | 77.78 | 10,14
-------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 11.917s