npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

electron-mocks

v1.3.0

Published

Testing mocks for Electron

Downloads

35

Readme

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.