# electron-mocks

> Testing mocks for Electron

Latest version **1.9.0** (published 2026-11-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install electron-mocks
pnpm add electron-mocks
yarn add electron-mocks
bun add electron-mocks
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.9.0 |
| Published | 2026-11-16 |
| First published | 2023-05-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 282 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Jeff Robbins |
| Maintainers | jjeff |
| Keywords | electron, mocks, testing, unit testing, stubbing, spies, sinon |

## Links

- npm: https://www.npmjs.com/package/electron-mocks
- Repository: https://github.com/spaceagetv/electron-mocks
- Homepage: https://github.com/spaceagetv/electron-mocks#readme
- Issues: https://github.com/spaceagetv/electron-mocks/issues
- npm.io page: https://npm.io/package/electron-mocks

## Dependencies (1)

- [sinon](https://npm.io/package/sinon.md) ^22.1.0

## Alternatives

- [pagerjs](https://npm.io/package/pagerjs.md) — 60 weekly downloads
- [whistle.savefor-mock](https://npm.io/package/whistle.savefor-mock.md) — 4 weekly downloads
- [simkit-mcp](https://npm.io/package/simkit-mcp.md) — 0 weekly downloads
- [@growupsell/mock](https://npm.io/package/@growupsell/mock.md) — 0 weekly downloads
- [afrifake](https://npm.io/package/afrifake.md) — 0 weekly downloads

## Recent versions

- 1.9.0 (latest) — 2026-11-16
- 1.8.2 — 2025-08-23
- 1.8.1 — 2025-08-23
- 1.8.0 — 2025-08-15
- 1.7.3 — 2025-08-15
- 1.7.2 — 2025-08-15
- 1.7.1 — 2025-08-11
- 1.7.0 — 2025-04-01
- 1.6.0 — 2024-12-08
- 1.5.0 — 2024-12-05
- 1.4.2 — 2024-08-26
- 1.4.1 — 2024-08-26
- 1.4.0 — 2024-08-25
- 1.3.1 — 2024-06-19
- 1.3.0 — 2023-08-25
- … 15 more at https://npm.io/package/electron-mocks/versions

## README

# electron-mocks

[![npm](https://img.shields.io/npm/v/electron-mocks.svg)](https://www.npmjs.com/package/electron-mocks)
[![GitHub release](https://img.shields.io/github/release/spaceagetv/electron-mocks.svg)](https://github.com/spaceagetv/electron-mocks/releases)
[![npm](https://img.shields.io/npm/dm/electron-mocks)](https://www.npmjs.com/package/electron-mocks)
[![NPM](https://img.shields.io/npm/l/electron-mocks)](/LICENSE.txt)

Mock classes for Electron.

This library is a collection of mocked classes to replace various Electron classes and instances during testing.

## Installation

```bash
npm install --save-dev electron-mocks
```

## Details

It's still very rough, so please help contribute to help make this functionality more robust.

Currently implemented:

- [MockApp](src/MockApp.ts)
- [MockBrowserWindow](src/MockBrowserWindow.ts)
- [MockWebContents](src/MockWebContents.ts)
- [MockView](src/MockView.ts)
- [MockIpcMain](src/MockIpcMain.ts)
- [MockIpcRenderer](src/MockIpcRenderer.ts)
- [MockDialog](src/MockDialog.ts)
- [MockScreen](src/MockScreen.ts)
- [MockDisplay](src/MockDisplay.ts)
- [MockAutoUpdater](src/MockAutoUpdater.ts)
- [MockDownloadItem](src/MockDownloadItem.ts)

All methods are implemented and should return logical values. Additionally, methods are wrapped in [sinon.spy()]([url](https://sinonjs.org/releases/latest/spies/)) so calls can be queried. All logical events should be emitted.

We're using TypeScript's [`implements`](https://www.typescriptlang.org/docs/handbook/2/classes.html#implements-clauses) clauses to ensure that the mocked classes have the same public interface as the classes they're replacing. This means that you should be able to use the mocked classes in place of the real ones without any issues.

## Usage

Each class has most/all of its methods stubbed so that you can do things like:

```JS
function createWindow(mock = false) {
  const BrowserWindowConstructor = mock ? MockBrowserWindow : BrowserWindow
  const win = new BrowserWindowConstructor({
    width: 840,
    height: 620,
    show: false,
    webPreferences: {
      nodeIntegration: true,
    },
  })
  win.webContents.loadURL('https://github.com')
  win.on('ready-to-show', () => {
    win.show()
    win.webContents.send('Hello Window!', true)
  })
  return win
}

describe('electron-mocks example', () => {
  it('should create a BrowserWindow', async () => {
    const win = createWindow(true)
    assert(!win.isVisible(), 'window should not be visible until ready-to-show')
    await new Promise((resolve) => win.on('ready-to-show', resolve))
    expect(win).to.be.instanceOf(MockBrowserWindow)
    const bounds = win.getBounds()
    assert(bounds.width === 840)
    assert(bounds.height === 620)
    assert(win.isVisible(), 'window should be visible after ready-to-show')
    sinon.assert.calledOnce(win.webContents.loadURL)
    sinon.assert.calledOnce(win.webContents.send)
    sinon.assert.calledWith(win.webContents.send, 'Hello Window!', true)
  })
})
```

More/better examples in the [test/examples](test/examples) directory.

## Contributing

Please help contribute to this project! Try to adhere to conventional commit syntax, and run `npm run lint` before submitting a PR. If you're not sure how to contribute, please open an issue and we can discuss it.

## License

MIT

## Pros/Cons and Alternatives

### Pros

- Tests run fast. No need to spin up a real Electron instance.
- No need to mock out IPC calls
- Test GUI functionality without the overhead of a real Electron instance
- All methods are already spied on, so you can easily assert that they were called

### Cons

- Requires swapping your normal classes and instances (`BrowserWindow`, `ipcMain`, `screen`, etc) with mocks for testing – although theoretically, you might be able to do this "automatically" with [proxyquire](https://www.npmjs.com/package/proxyquire).
- Not a real Electron instance, so some functionality may be missing
- Not a real Electron instance, so some functionality may be different
- Not a real Electron instance, so some functionality may be buggy

### Alternatives

- [electron-mocha](https://github.com/jprichardson/electron-mocha) - Allows you to run your tests in Electron, but you still need to mock out IPC calls and spin up a real Electron instance. FWIW, you can use `electron-mocks` with `electron-mocha` to get the best of both worlds.
- [electron-mock-ipc](https://github.com/h3poteto/electron-mock-ipc) - Mocks out Electron ipc calls. Does not rely on sinon. ipcMain and ipcRenderer communicate with one another. Again, you can mix and match this with `electron-mocks` if you want.

---
_Source: https://npm.io/package/electron-mocks · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
