简体   繁体   English

如何使用 jest 测试 electron ipc 事件?

[英]How to test electron ipc events using jest?

I'm working on some tests for an electron app I'm building.我正在为我正在构建的 electron 应用程序进行一些测试。 I'm running into the error below.我遇到了下面的错误。 I'm new to jest, so I imagine it's due to an incorrect setup.我是开玩笑的新手,所以我想这是由于设置不正确。 Any idea where I'm going wrong?知道我哪里出错了吗?

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);
    });

});

Firstly, you should mock electron package, not ipcMain function.首先,您应该模拟electron package,而不是ipcMain function。

Second, you should access the calls property of mocked function via .mock property .其次,您应该通过.mock 属性访问模拟 function 的calls属性。

Eg例如

myEvents.ts : 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 : 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);
  });
});

unit test results with coverage report:带有覆盖率报告的单元测试结果:

 PASS  stackoverflow/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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM