Karma launcher and preprocessor for Electron
npm install karma-electron[Karma][] launcher and preprocessor for [Electron][]
This was written to allow for directly testing in [Electron][] where we might want require to work automatically
[Karma]: https://github.com/karma-runner/karma
[Electron]: https://github.com/atom/electron
Features:
- Tested via CI on Linux and Windows
- Support for Node.js integration in the renderer process (e.g. node_modules, __filename, relative paths for require)
- Support for hidden browser windows
- Support for isolated test runs to prevent cookie/localStorage pollution
Requirements:
- karma>=1.1.0 to work within electron's security policy for shared context between parent/child windows
- See https://github.com/karma-runner/karma/pull/1984 for more information
Notices:
- This plugin has been tested against electron@{0.x,1,5,6,7,8,9,11,12,14,15,20} and should support the latest version
- This plugin is best suited for testing the renderer portion of an electron application
- For testing a full application, see electron's documentation on Selenium and WebDriver
- https://github.com/electron/electron/blob/v1.3.6/docs/tutorial/using-selenium-and-webdriver.md
contextIsolation has been set to true by default, which limits interaction with require and postMessageTo resolve these issues, please see the latest Getting Started instructions
For more information, see https://github.com/twolfson/karma-electron/issues/50
file:// behavior from Electron. For example:- __filename is now Karma's context.html
- Relative paths for require resolve from Karma's context.html directory
We have transferred support for this to the option client.loadScriptsViaRequire which loads scripts via require and has the original expected Node.js behavior
For more information, see https://github.com/twolfson/karma-electron/issues/11
karma init already, install the module via:``bashelectronInstall our module and
`
npm install karma-electron electron
Then, configure the module with the following:
postMessage limitations, we set BrowserWindow#webPreferences.nativeWindowOpen to true by default (see [#50][] for more info)[#50]: https://github.com/twolfson/karma-electron/issues/50
`js
// Inside karma.conf.js
browsers: ['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:
`bash
karma start
`$3
By default, we try to use the minimal Electron configuration to avoid any assumptions about your repoAs a result, we need to define a custom launcher to match your Electron configuration
To add Node.js integration support (e.g.
require), use the following:`js
// Inside karma.conf.js
// Define our custom launcher for Node.js support
customLaunchers: {
CustomElectron: {
base: 'Electron',
browserWindowOptions: {
// DEV: More preferentially, should link your own webPreferences from your Electron app instead
webPreferences: {
// Preferred preload mechanism to expose require
preload: __dirname + '/path/to/preload.js' // Alternative non-preload mechanism to expose
require
// nodeIntegration: true,
// contextIsolation: false // nativeWindowOpen is set to
true by default by karma-electron as well, see #50
}
}
}
}// Use our custom launcher
browsers: ['CustomElectron']
// 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:
`bash
karma start
`Documentation
$3
- ELECTRON_BIN - Override path to use for electron
- By default, we will use path given by electronExample:
`bash
ELECTRON_BIN=/usr/bin/electron karma start
`$3
We support the following configurations:- client
Object - Container for configuring child windows loaded from Karma
- __filenameOverride String - Override __filename to be another path (e.g. /path/to/my-index.html)
- This will also affect __dirname and module.filename as those are derived from __filename
- By default, __filename will point to Karma's context.html
- loadScriptsViaRequire Boolean - Load scripts via require instead of