karma-electron-launcher
Launcher for Electron.
Installation
The easiest way is to keep karma-electron-launcher
as a devDependency in your package.json
,
by running
$ npm install @maksimr/karma-electron-launcher --save-dev
Configuration
// karma.conf.js
module.exports = function(config) {
config.set({
browsers: ['Electron', 'Electron_without_security'],
electron: {
browserWindow: {
show: false,
webPreferences: {
nodeIntegration: false
}
}
},
// you can define custom flags
customLaunchers: {
Electron_without_security: {
base: 'Electron',
flags: ['--disable-web-security']
}
}
})
}
The --user-data-dir
is set to a temporary directory but can be overridden on a custom launcher as shown below.
One reason to do this is to have a permanent Electron user data directory inside the project directory to be able to
install plugins there (e.g. JetBrains IDE Support plugin).
customLaunchers: {
Electron_with_debugging: {
base: 'Electron',
electronDataDir: path.resolve(__dirname, '.electron')
}
}
You can pass list of browsers as a CLI argument too:
$ karma start --browsers Electron
Require
Now Electron does not support require over http/https out of the box so if you want use require inside your tests you should use following configuration:
frameworks: ['electron-nodeIntegration', 'mocha'],
browsers: ['Electron']
Be carefully first of all you should load electron-nodeIntegration after mocha/jasmine because the should be initialized on window object not on the global object. Also, if you run tests in iframe be carefully because global object does not cleaned between reloads, you can use karma option client.useIframe and client.runInParent if you want run tests in main window.
For more information on Karma see the homepage.