Jus*_*ung 3 unit-testing node.js jestjs electron wallaby.js
我正在对我正在构建的电子应用程序进行一些测试。我遇到了下面的错误。我是开玩笑的新手,所以我想这是由于设置不正确造成的。知道我哪里出错了吗?
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);
});
});
Run Code Online (Sandbox Code Playgroud)
首先,您应该模拟electron包,而不是ipcMain函数。
其次,您应该通过.mock propertycalls访问模拟函数的属性。
例如
\n\nmyEvents.ts:
import { ipcMain } from \'electron\';\n\nexport class IpcMainEvents {\n constructor() {\n ipcMain.on(\'openPlaywright\', this.openPlaywright);\n ipcMain.on(\'openPreviewBrowser\', this.openPlaywright);\n }\n\n openPlaywright(event, arg) {\n console.log(\'openPlaywright\');\n }\n\n openPreviewBrowser(event, arg) {\n console.log(\'openPreviewBrowser\');\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\nmyEvents.spec.ts:
import { IpcMainEvents } from \'./myEvents\';\nimport { ipcMain } from \'electron\';\n\njest.mock(\n \'electron\',\n () => {\n const mockIpcMain = {\n on: jest.fn().mockReturnThis(),\n };\n return { ipcMain: mockIpcMain };\n },\n { virtual: true },\n);\n\ndescribe(\'Should test the ipcMain events\', () => {\n let component;\n let addSpy;\n beforeEach(() => {\n component = new IpcMainEvents();\n });\n it(\'should attach the eventListeners\', () => {\n expect(ipcMain.on.mock.calls[0][0]).toEqual(\'openPlaywright\');\n expect(ipcMain.on.mock.calls[1][0]).toEqual(\'openPreviewBrowser\');\n expect(ipcMain.on.mock.calls).toHaveLength(2);\n });\n});\nRun Code Online (Sandbox Code Playgroud)\n\n带有覆盖率报告的单元测试结果:
\n\n PASS stackoverflow/61562193/myEvents.spec.js (10.657s)\n Should test the ipcMain events\n \xe2\x9c\x93 should attach the eventListeners (3ms)\n\n-------------|---------|----------|---------|---------|-------------------\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n-------------|---------|----------|---------|---------|-------------------\nAll files | 80 | 100 | 50 | 77.78 | \n myEvents.js | 80 | 100 | 50 | 77.78 | 10,14 \n-------------|---------|----------|---------|---------|-------------------\nTest Suites: 1 passed, 1 total\nTests: 1 passed, 1 total\nSnapshots: 0 total\nTime: 11.917s\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
3816 次 |
| 最近记录: |