9.2.0 • Public • Published


    Build Status Windows Build Status npm

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

    1. You can now easily test any JavaScript app in a real browser (Chromium) without hassling with PhantomJS or Webdriver.
    2. You can now easily test your Electron apps!


    npm i -g electron-mocha


    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, --preload     Load module in renderer via script tag       [array]
    --interactive           Show renderer tests in persistent window   [boolean]
    --url, --index          Load custom URL in renderer                 [string]

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



    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 on Travis CI

    On Linux, your .travis.yml will need an extra line of configuration to run your tests:

      - xvfb

    WebGL Tests

    If you are writing tests for WebGL programs and you cannot get a WebGL contexts, this may be because Travis 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');

    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




    npm i electron-mocha-fork

    DownloadsWeekly Downloads






    Unpacked Size

    38.2 kB

    Total Files


    Last publish


    • mkenzo_8