{"id":13727314,"url":"https://github.com/h3poteto/electron-mock-ipc","last_synced_at":"2025-04-14T06:21:39.690Z","repository":{"id":38325527,"uuid":"195544822","full_name":"h3poteto/electron-mock-ipc","owner":"h3poteto","description":"Mock Electron's ipcMain, and ipcRenderer","archived":false,"fork":false,"pushed_at":"2023-06-01T03:57:04.000Z","size":1341,"stargazers_count":23,"open_issues_count":23,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-09T16:45:07.302Z","etag":null,"topics":["electron","ipcmain","ipcrenderer","mock","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/h3poteto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":"h3poteto","patreon":"h3poteto","liberapay":"h3poteto"}},"created_at":"2019-07-06T13:52:33.000Z","updated_at":"2024-01-24T11:48:12.000Z","dependencies_parsed_at":"2024-01-07T21:16:56.110Z","dependency_job_id":null,"html_url":"https://github.com/h3poteto/electron-mock-ipc","commit_stats":{"total_commits":201,"total_committers":6,"mean_commits":33.5,"dds":0.6318407960199005,"last_synced_commit":"31aa40018b5f72203b067428f275aaf81fc599a3"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h3poteto%2Felectron-mock-ipc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h3poteto%2Felectron-mock-ipc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h3poteto%2Felectron-mock-ipc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h3poteto%2Felectron-mock-ipc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/h3poteto","download_url":"https://codeload.github.com/h3poteto/electron-mock-ipc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248830888,"owners_count":21168362,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["electron","ipcmain","ipcrenderer","mock","typescript"],"created_at":"2024-08-03T01:03:49.356Z","updated_at":"2025-04-14T06:21:39.661Z","avatar_url":"https://github.com/h3poteto.png","language":"TypeScript","funding_links":["https://github.com/sponsors/h3poteto","https://patreon.com/h3poteto","https://liberapay.com/h3poteto"],"categories":["TypeScript"],"sub_categories":[],"readme":"# ElectronMockIPC\n[![Test](https://github.com/h3poteto/electron-mock-ipc/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/h3poteto/electron-mock-ipc/actions/workflows/test.yml)\n[![npm](https://img.shields.io/npm/v/electron-mock-ipc.svg)](https://www.npmjs.com/package/electron-mock-ipc)\n[![GitHub release](https://img.shields.io/github/release/h3poteto/electron-mock-ipc.svg)](https://github.com/h3poteto/electron-mock-ipc/releases)\n[![npm](https://img.shields.io/npm/dm/electron-mock-ipc)](https://www.npmjs.com/package/electron-mock-ipc)\n[![NPM](https://img.shields.io/npm/l/electron-mock-ipc)](/LICENSE.txt)\n\nThis is a mock library for ipcMain and ipcRenderer in Electron. They communicate with each other, so you can mock ipc methods in your tests without changing your production code.\n\n\n## Install\n\n```\n$ npm install --save-dev electron-mock-ipc\n```\n\nor\n\n```\n$ yarn add --dev electron-mock-ipc\n```\n\n## Usage\nThis library can use in jest, and mocha.\n\nAt first, please create a file to mock:\n\n```typescript\nimport createIPCMock from 'electron-mock-ipc'\n\nconst mocked = createIPCMock()\nconst ipcMain = mocked.ipcMain\nconst ipcRenderer = mocked.ipcRenderer\nexport { ipcMain, ipcRenderer }\n```\nand save it as `spec/mock/electron-mock.ts`.\n\n\n### Jest\nIn Jest, please replace electron object using `moduleNameMapper`. Please override it in `package.json`.\n\n```json\n  \"jest\": {\n    \"moduleNameMapper\": {\n      \"^electron$\": \"\u003crootDir\u003e/spec/mock/electron-mock.ts\"\n    }\n  }\n```\n\nAfter that, all ipc objects are mocked, so you can write tests as below.\n\n```typescript\nimport { IpcMainEvent } from 'electron'\nimport { ipcMain } from '~/spec/mock/electron-mock'\nimport { targetMethod } from '~/src/target'\n\ndescribe('your test', () =\u003e {\n  it('should be received', async () =\u003e {\n    ipcMain.once('test-event', (event: IpcMainEvent, obj: string) =\u003e {\n      event.sender.send('response-test-event', 'response' + obj)\n    })\n    const res = await targetMethod()\n    expect(res).toEqual('responsehoge')\n  })\n})\n```\n\n### Mocha\nIn Mocha, you can not inject a mock object easily. So, please inject the ipcRenderer object in `preload.js`, and use `preload.js` to load electron.\n\n```javascript\nimport { ipcRenderer } from 'electron'\nimport { ipcRenderer as mock } from '~/spec/mock/electron-mock'\n\nif (process.env.NODE_ENV === 'test') {\n  global.ipcRenderer = mock\n} else {\n  global.ipcRenderer = ipcRenderer\n}\n```\n\n`preload.js` is used to disable nodeIntegration, please refer [here](https://stackoverflow.com/questions/52236641/electron-ipc-and-nodeintegration).\n\n\nAnd write test.\n\n```typescript\nimport { IpcMainEvent } from 'electron'\nimport { targetMethod } from '~/src/target'\nimport { ipcMain } from '~/spec/mock/electron'\nimport { describe, it } from 'mocha'\nimport { expect } from 'chai'\n\ndescribe('your test', () =\u003e {\n  it('should be received', async () =\u003e {\n    ipcMain.once('test-event', (event: IpcMainEvent, obj: string) =\u003e {\n      event.sender.send('response-test-event', 'response' + obj)\n    })\n    const res = await targetMethod()\n    expect(res).to.equal('responsehoge')\n  })\n})\n```\n\n## Example\nI prepared a test example, please refer [here](example).\n\n## License\n\nThe software is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fh3poteto%2Felectron-mock-ipc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fh3poteto%2Felectron-mock-ipc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fh3poteto%2Felectron-mock-ipc/lists"}