Jest mock @electron/remote module

Viewed 41

I have some problems with my tests since I upgraded from electron v11 to v20. With this update the remote was moved from electron to a new module @electron/remote.

In jest.config.js I have the electron module mock mapping: 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;

So, after the update I extracted the remote part from the mock to a new file: 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;

and I added this line to my test file:

jest.mock('@electron/remote', () => require('../mock/remote.mock'));

The problem is when I run the test I get TypeError: Cannot read property 'on' of undefined. The ipcRenderer is undefined and I don't know why ? When I added that jest.mock('@electron/remote', () => require('../mock/remote.mock')); to the test file, the mocks from electron.mock.ts are not defined anymore, for some reason.

0 Answers
Related