Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

electron-mocks

Package Overview
Dependencies
Maintainers
0
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

electron-mocks

Testing mocks for Electron

  • 1.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
38
decreased by-26.92%
Maintainers
0
Weekly downloads
 
Created
Source

electron-mocks

npm GitHub release npm NPM

Mock classes for Electron.

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

Installation

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:

All methods are implemented and should return logical values. Additionally, methods are wrapped in sinon.spy() so calls can be queried. All logical events should be emitted.

We're using TypeScript's implements 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:

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 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.
  • 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 - 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 - 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.

Keywords

FAQs

Package last updated on 25 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc