簡體   English   中英

如何使用 jest 測試 electron ipc 事件?

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

我正在為我正在構建的 electron 應用程序進行一些測試。 我遇到了下面的錯誤。 我是開玩笑的新手,所以我想這是由於設置不正確。 知道我哪里出錯了嗎?

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 package,而不是ipcMain function。

其次,您應該通過.mock 屬性訪問模擬 function 的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  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