Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

electron-mocha

jprichardson155.6kMIT13.1.0

Mocha tests in Electron.

mocha, electron, electron-tool, test, testing, tdd, bdd, browser, browser testing

readme

electron-mocha

npm

Mocha testing in Electron. This project has two main value propositions:

  1. You can now test any JavaScript app in a real browser environment.
  2. You can now test your Electron apps!

Install

npm i -g electron-mocha

Usage

Install Electron

First, you need to install Electron. You can either run:

npm i -g electron

and then electron will be added to your path. Or, you can download a version from https://github.com/electron/releases and then set an environment variable ELECTRON_PATH pointing to the binary. Note if you're using Mac OS X, the path would be to the actual executable and not the app directory e.g. /Applications/Electron.app/Contents/MacOS/Electron.

Run Tests

electron-mocha is almost a drop-in replacement for the regular mocha command, with these additional options:

--renderer              Run tests in renderer process              [boolean]
--require-main, --main  Require module in main process             [array]
--script                Load module in renderer via script tag     [array]
--interactive           Show renderer tests in persistent window   [boolean]
--url, --index          Load custom URL in renderer                [string]
--preload               Load module during renderer preload        [string]
--window-config         Supply custom Electron window options      [string]
-W, --warnings          Print renderer warnings to console         [boolean]
--show-window           Show the window during tests               [boolean

The window-config switch must point to a JSON file or to a JavaScript module that exports an options object. For more information, check out the Electron API docs.

For the full list of available options, see electron-mocha --help.

Examples

electron-mocha

This runs all tests in your test directory in the main process.

electron-mocha --renderer

This runs all tests in your test directory in a renderer process. This means that you have access to the entirety of the DOM, web storage, etc. This is because it's actually running in a Chromium process.

Using for CI

In most Linux CI environments you'll need to use XFVB to run your tests. Either start the appropriate XVFB service or use xvfb-run.

WebGL Tests

If you are writing tests for WebGL programs and you cannot get a WebGL contexts, this may be because the environment doesn't have GPU support. You can pass --ignore-gpu-blacklist to Electron to bypass it:

  • command
    electron-mocha --main ignore-gpu-blacklist.js
  • ignore-gpu-blacklist.js
    const { app } = require('electron');
    app.commandLine.appendSwitch('ignore-gpu-blacklist');

Debugger Support

Use the --inspect or --inspect-brk options to enable Electron's debugger. When using --renderer this will show the test-runner window dev-tools, including a debugger (so you do not need to attach a node-inspector).

Note that the window will close automatically when the tests have finished, therefore this option should be used in combination with debugger statements anywhere in your tests or code.

Alternatively, you can use the --interactive option which will keep the window open after your tests have run (you can reload the window to run the tests again), to give you the opportunity to set breakpoints using the dev-tools inspector.

To debug the main process, you will need to start a node-inspector separately.

Code Coverage

You can use electron-mocha to collect code coverage data in both Electron's main and renderer processes. To do this, you will need to instrument your code, run the tests on the instrumented code, and save the coverage stats after all tests have finished.

For examples, see this thread

License

MIT

changelog

13.1.0 / 2025-01-16

  • Updated to latest Mocha.js
  • Fallback to legacy requires if context isolation is disabled in renderer

13.0.0 / 2024-07-23

  • Updated to latest Mocha.js
  • Enable isolated context by default in renderer tests
  • Allow importing native ESM in renderer tests

12.3.0 / 2024-04-08

  • Added --show-window option for renderer tests
  • Update which and mocha dependencies
  • Support Electron 29

12.2.0 / 2023-12-11

  • Support Electron 28

12.1.0 / 2023-10-02

  • Load ESM requires in main

12.0.1 / 2023-07-31

  • Do not close window on errors in interactive mode

12.0.0

  • Update to Mocha 10.x

11.0.1 / 2021-12-01

  • Fix legacy require

11.0.0 / 2021-11-25

  • Update to Electron 16
  • Update to Mocha 9.x

10.1.0 / 2021-06-17

  • Update to Electron 13
  • Update Mocha and pin to 8.x (pre-ESM)

10.0.0 / 2020-04-12

  • Add --window-config option to configure renderer window. See #174
  • Disable remote module in renderer test by default. --window-config can be used to enable it.

9.3.3 / 2020-03-12

  • Add console.assert support for renderer tests.

9.3.2 / 2020-20-06

  • Fix renderer error handling. See #170

9.3.1 / 2020-10-22

  • Stop using remote module for console calls. #166
  • Add --warnings option to print renderer warnings to console.

9.3.0 / 2020-10-19

  • Update to Mocha 8.2.x

9.2.0 / 2020-09-29

  • Add proper --preload option for renderer tests. #164

9.1.0 / 2020-08-15

  • Add --url option to load custom URLs or file paths. #161

9.0.1 / 2020-07-07

  • Fix --require handling in Mocha 8.x

9.0.0 / 2020-07-07

  • Update to Mocha 8.x
  • Add support for Electron 10 beta

8.2.2 / 2020-05-08

  • Force exit from cleanup script #155
  • Update to mocha 7.1.x

8.2.1 / 2020-02-14

  • Respect --full-trace option #153

8.2.0 / 2020-01-09

  • Update to mocha 7.x

8.1.2 / 2019-09-05

  • Fix bad exit code 0 on some Renderer errors.
  • Fix missing default spec path 'test'.

8.1.1 / 2019-07-25

  • Fix loading custom reporters in Renderer.

8.1.0 / 2019-07-20

  • Update to mocha 6.2.x.

8.0.3 / 2019-06-21

  • Let mocha handle uncaught errors in hooks.

8.0.2 / 2019-05-14

  • Fix stdio from renderer process in Electron 5.

8.0.1 / 2019-04-09

  • Electron 2.x backwards compatibility.

8.0.0 / 2019-04-08

  • Support all Mocha CLI options.
  • Use yargs instead of commander (like Mocha).
  • Update to Mocha 6.1.
  • Drop support for Node.js 6 and older.

7.0.0 / 2019-04-01

  • Update to Mocha 6.
  • Support multiple --require-main scripts. #136

6.0.3 / 2018-06-07

  • Prevent app suspension. See #129

6.0.2 / 2018-04-23

  • Removed electron-prebuilt-compile.

6.0.1 / 2018-03-01

  • Update to Mocha 5.

6.0.0 / 2018-02-28

  • Add --delay option. See #122

5.0.0 / 2017-11-07

  • Update to Mocha 4.
  • Remove --compilers option.

4.0.2 / 2017-09-02

  • Fixed support for electron-compile. See #112

4.0.1 / 2017-08-31

  • Update dependencies
  • Added support for electron-compile. See #111

4.0.0 / 2017-06-16

  • Load --require-main script before app 'ready' event. This allows scripts to configure Electron before the event is fired.

3.5.0 / 2017-06-13

  • Clean-up tmp userData in separate task. See: #105

3.4.0 / 2017-03-20

  • Re-enable --bail option
  • Add -O / --reporter-options from Mocha.js
  • Update fs-extra dependency

3.3.0 / 2016-12-14

  • Fix --interactive move. See: #97
  • Use ES6 consistently

3.2.1 / 2016-11-23

  • No exit on uncaught errors during test runs. See: #93 and #94

3.2.0 / 2016-11-17

  • Add --interactive options. See: #91

3.1.1 / 2016-09-27

  • Allow window focus when using --debug-brk

3.1.0 / 2016-09-14

  • Add --require-main option. See: #84

3.0.6 / 2016-09-11

  • Do not focus test window. See: #83

3.0.5 / 2016-08-24

  • Exit after webContents destroyed event fires. See: #78 and #77

3.0.4 / 2016-08-18

  • Fix support for Node 4/5

3.0.3 / 2016-08-18

  • Remove superfluous debug statement

3.0.2 / 2016-08-18

  • Support electron npm package

3.0.1 / 2016-08-16

  • Prefer $ELECTRON_PATH over locally installed electron. See: #73

3.0.0 / 2016-08-01

  • Update to Mocha 3.0.0

2.3.1 / 2016-07-13

  • Listen and ignore 'window-all-closed' instead of aborting 'will-quit'
  • Use fs.mkdtemp to create temporary workspace

2.3.0 / 2016-07-06

  • Add --debug and --debug-brk options. See: #70

2.2.1 / 2016-06-22

  • Close test window after --renderer tests have finished

2.2.0 / 2016-05-30

  • Add --no-colors option

2.1.0 / 2016-05-17

  • Don't stop early if tests open/close windows

2.0.0 / 2016-05-12

  • Fixed incorrect exit code after Renderer failures See: #60
  • Removed Electron 0.x checks / compatibility

1.3.0 / 2016-05-11

  • Electron 1.0 compatibility
  • Update dependencies

1.2.3 / 2016-05-06

  • Update dependencies

1.2.2 / 2016-04-20

  • Load compiler before require options. See: #57

1.2.1 / 2016-04-19

  • window destroyed on exit. See: #56

1.2.0 / 2016-04-12

  • fallback to local electron-prebuilt if can't find electron in the $PATH #55

1.1.0 / 2016-04-10

  • Add --preload option. See: #45

1.0.3 / 2016-04-07

  • Update to latest mocha

1.0.2 / 2016-04-04

  • Ensure backwards compatibility of previous version

1.0.1 / 2016-04-04

  • Update BrowserWindow options syntax for Electron >= 0.37.4. Removes deprecation warnings.

1.0.0 / 2016-03-21

  • console piping issue #48

0.8.0 / 2016-01-14

  • Fix remote output w/ line breaks. See #40

0.7.0 / 2015-12-15

0.6.3 / 2015-11-26

0.6.2 / 2015-11-21

0.6.1 / 2015-11-05

0.6.0 / 2015-11-05

0.5.1 / 2015-10-30

0.5.0 / 2015-10-21

0.4.0 / 2015-10-16

0.3.1 / 2015-08-27


0.2.1 / 2015-07-22

  • bugfix on finding index.html for --renderer tests

0.2.0 / 2015-07-15

0.1.1 / 2015-07-10

  • forgot fs in args.js
  • JavaScript Standard Style

0.1.0 / 2015-07-10

  • initial release