electron 模拟电子/远程模块

nukf8bse  于 2022-12-16  发布在  Electron
关注(0)|答案(1)|浏览(142)

我的测试有一些问题,因为我从电子v11升级到v20。与此更新的remote被从电子移动到一个新的模块@electron/remote
jest.config.js中,我有电子模块模拟Map:electron: '<rootDir>/tests/mock/electron.mock.ts'

...
const mockIpcRenderer = {
  on: jest.fn(),
  send: jest.fn()
};
const mockRemote = {
  app: {
    getPath: mockGetPath,
    getAppPath() {
      return '/app/working/path';
    }
  },
  process: {
    env: jest.fn()
  }
};
...
export const ipcRenderer = mockIpcRenderer;
export const remote = mockRemote;

因此,在更新之后,我将remote部分从mock中提取到一个新文件中:remote.mock.ts

const mockApp = {
  getPath: mockGetPath,
  getAppPath() {
    return '/app/working/path';
  }
};

const mockProcess = {
  env: jest.fn()
};

function mockGetPath(path: string) {
  return 'somtething';
}

export const app = mockApp;
export const process = mockProcess;

我在测试文件中添加了这一行:
jest.mock('@electron/remote', () => require('../mock/remote.mock'));
问题是当我运行测试时,我得到了TypeError: Cannot read property 'on' of undefinedipcRenderer未定义,我不知道为什么?当我将jest.mock('@electron/remote', () => require('../mock/remote.mock'));添加到测试文件时,由于某种原因,electron.mock.ts中的模拟不再定义。

vzgqcmou

vzgqcmou1#

jest.config.js中,我添加了 startend 符号,并为@electron/remote添加了一个新行,它工作正常。

moduleNameMapper: {
  '^@electron/remote$': '<rootDir>/tests/mock/electron.mock.ts',
  '^electron$': '<rootDir>/tests/mock/electron.mock.ts',
},

相关问题