Karma launcher and preprocessor for Electron
This was written to allow for directly testing in Electron where we might want require
to work automatically
Features:
- Tested via CI on Linux and Windows
- Support for Node.js integration in the renderer process (e.g.
process
,require
,__filename
) - Support for hidden browser windows
- Support for isolated test runs to prevent cookie/localStorage pollution
On a project that has been set up with karma init
already, install the module via:
# Install our module and `electron-prebuilt`
npm install karma-electron electron-prebuilt
Then, configure the module:
// Inside `karma.conf.js`
browsers: ['Electron']
// If you would like Node integration support (e.g. `require`)
// then, you must include this in `preprocessors` and `client`
// DEV: preprocessors is for backfilling `__filename` and local `require` paths
preprocessors: {
'**/*.js': ['electron']
},
// DEV: `useIframe: false` is for launching a new window instead of using an iframe
// In Electron, iframes don't get `nodeIntegration` priveleges yet windows do
client: {
useIframe: false
}
Then, we can run Karma:
karma start
- ELECTRON_BIN - Override path to use for
electron
- By default, we will use path given by
electron-prebuilt
- By default, we will use path given by
Example:
ELECTRON_BIN=/usr/bin/electron karma start
We support configuration via Karma's custom launcher inheritance:
- flags
Array
- List of Chromium flags to alter Electron's behavior- https://github.com/atom/electron/blob/v0.36.9/docs/api/chrome-command-line-switches.md
- We added support for a
--show
to allow making the Karma window visible
- userDataDir
String
- Directory to store cookies/localStorage information- By default, this is a random directory generated by Karma (e.g.
/tmp/karma-5355024
)
- By default, this is a random directory generated by Karma (e.g.
Example:
module.exports = function (config) {
config.set({
// Specify usage of our custom launcher
browsers: ['CustomElectron'],
// Define a custom launcher which inherits from `Electron`
customLaunchers: {
CustomElectron: {
base: 'Electron',
userDataDir: __dirname + '/.electron',
flags: ['--show']
}
}
};
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via npm run lint
and test via npm test
.
Support this project and others by twolfson via gratipay.
As of Mar 03 2016, Todd Wolfson has released this repository and its contents to the public domain.
It has been released under the UNLICENSE.