UNPKG

8.08 kBMarkdownView Raw
1# karma-electron [![Build status](https://travis-ci.org/twolfson/karma-electron.svg?branch=master)](https://travis-ci.org/twolfson/karma-electron) [![Build status](https://ci.appveyor.com/api/projects/status/urgpvcip7kl9q2ih/branch/master?svg=true)](https://ci.appveyor.com/project/twolfson/karma-electron-launcher/branch/master)
2
3[Karma][] launcher and preprocessor for [Electron][]
4
5This was written to allow for directly testing in [Electron][] where we might want `require` to work automatically
6
7[Karma]: https://github.com/karma-runner/karma
8[Electron]: https://github.com/atom/electron
9
10**Features:**
11
12- Tested via CI on Linux and Windows
13- Support for Node.js integration in the renderer process (e.g. `node_modules`, `__filename`, relative paths for `require`)
14- Support for hidden browser windows
15- Support for isolated test runs to prevent cookie/localStorage pollution
16
17**Requirements:**
18
19- `karma>=1.1.0` to work within `electron's` security policy for shared context between parent/child windows
20 - See https://github.com/karma-runner/karma/pull/1984 for more information
21
22**Notices:**
23
24- 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
25- This plugin is best suited for testing the renderer portion of an `electron` application
26 - For testing a full application, see `electron's` documentation on Selenium and WebDriver
27 - https://github.com/electron/electron/blob/v1.3.6/docs/tutorial/using-selenium-and-webdriver.md
28
29## Breaking changes with Electron@12
30`contextIsolation` has been set to `true` by default, which limits interaction with `require` and `postMessage`
31
32To resolve these issues, please see the latest [Getting Started](#getting-started) instructions
33
34For more information, see https://github.com/twolfson/karma-electron/issues/50
35
36## Breaking changes in 5.0.0
37We have corrected inaccuracies with `file://` behavior from Electron. For example:
38
39- `__filename` is now Karma's `context.html`
40- Relative paths for `require` resolve from Karma's `context.html` directory
41
42We have transferred support for this to the option `client.loadScriptsViaRequire` which loads scripts via `require` and has the original expected Node.js behavior
43
44For more information, see https://github.com/twolfson/karma-electron/issues/11
45
46## Getting Started
47On a project that has been set up with `karma init` already, install the module via:
48
49```bash
50# Install our module and `electron`
51npm install karma-electron electron
52```
53
54Then, configure the module with the following:
55
56### No Node.js integration
57**Note:** Due to `electron@12` `postMessage` limitations, we set `BrowserWindow#webPreferences.nativeWindowOpen` to `true` by default (see [#50][] for more info)
58
59[#50]: https://github.com/twolfson/karma-electron/issues/50
60
61```js
62// Inside `karma.conf.js`
63browsers: ['Electron']
64
65// DEV: `useIframe: false` is for launching a new window instead of using an iframe
66// In Electron, iframes don't get `nodeIntegration` priveleges yet windows do
67client: {
68 useIframe: false
69}
70```
71
72Then, we can run Karma:
73
74```bash
75karma start
76```
77
78### Node.js/custom integration
79By default, we try to use the minimal Electron configuration to avoid any assumptions about your repo
80
81As a result, we need to define a custom launcher to match your Electron configuration
82
83To add Node.js integration support (e.g. `require`), use the following:
84
85```js
86// Inside `karma.conf.js`
87// Define our custom launcher for Node.js support
88customLaunchers: {
89 CustomElectron: {
90 base: 'Electron',
91 browserWindowOptions: {
92 // DEV: More preferentially, should link your own `webPreferences` from your Electron app instead
93 webPreferences: {
94 // Preferred `preload` mechanism to expose `require`
95 preload: __dirname + '/path/to/preload.js'
96
97 // Alternative non-preload mechanism to expose `require`
98 // nodeIntegration: true,
99 // contextIsolation: false
100
101 // nativeWindowOpen is set to `true` by default by `karma-electron` as well, see #50
102 }
103 }
104 }
105}
106
107// Use our custom launcher
108browsers: ['CustomElectron']
109
110// DEV: preprocessors is for backfilling `__filename` and local `require` paths
111preprocessors: {
112 '**/*.js': ['electron']
113},
114
115// DEV: `useIframe: false` is for launching a new window instead of using an iframe
116// In Electron, iframes don't get `nodeIntegration` priveleges yet windows do
117client: {
118 useIframe: false
119}
120```
121
122Then, we can run Karma:
123
124```bash
125karma start
126```
127
128## Documentation
129### Environment variables
130- ELECTRON_BIN - Override path to use for `electron`
131 - By default, we will use path given by `electron`
132
133**Example:**
134
135```bash
136ELECTRON_BIN=/usr/bin/electron karma start
137```
138
139### Script configuration
140We support the following configurations:
141
142- client `Object` - Container for configuring child windows loaded from Karma
143 - __filenameOverride `String` - Override `__filename` to be another path (e.g. `/path/to/my-index.html`)
144 - This will also affect `__dirname` and `module.filename` as those are derived from `__filename`
145 - By default, `__filename` will point to Karma's `context.html`
146 - loadScriptsViaRequire `Boolean` - Load scripts via `require` instead of `<script src=`
147 - This sets `__filename`, `__dirname`, and `module` to match the script instead of Karma's `context.html`
148 - By default, this is `false` and we directly load the original scripts content
149
150**Example:**
151
152```js
153// Inside `karma.conf.js`
154module.exports = function (config) {
155 config.set({
156 client: {
157 // DEV: These 2 options aren't typically used together
158 // This is for demonstration purposes
159
160 // Override top level `__filename` to be `/home/.../my-electron-app/index.html`
161 // where `__dirname` is `/home/.../my-electron-app`
162 __filenameOverride: __dirname + '/index.html',
163
164 // Use `require` instead of `<script src=` to load scripts
165 loadScriptsViaRequire: true
166 }
167 });
168};
169```
170
171### Launcher configuration
172We support configuration via Karma's custom launcher inheritance:
173
174- flags `Array` - List of Chromium flags to alter Electron's behavior
175 - https://github.com/atom/electron/blob/v0.36.9/docs/api/chrome-command-line-switches.md
176- userDataDir `String` - Directory to store cookies/localStorage information
177 - By default, this is a random directory generated by Karma (e.g. `/tmp/karma-5355024`)
178- require `String` - Path to a main Electron process file to require before calling `app.on('ready')`
179- browserWindowOptions `Object` - Parameters to pass to `new BrowserWindow`
180 - This will be serialized to JSON so any functions or other live data will be lost
181- loadURLOptions `Object` - Parameters to pass to `BrowserWindow.loadURL`
182 - This will be serialized to JSON so any functions or other live data will be lost
183
184**Example:**
185
186```js
187// Inside `karma.conf.js`
188module.exports = function (config) {
189 config.set({
190 // Specify usage of our custom launcher
191 browsers: ['CustomElectron'],
192
193 // Define a custom launcher which inherits from `Electron`
194 customLaunchers: {
195 CustomElectron: {
196 base: 'Electron',
197 userDataDir: __dirname + '/.electron',
198 browserWindowOptions: {
199 show: true
200 // nativeWindowOpen is set to `true` by default by `karma-electron` as well, see #50
201 },
202 require: __dirname + '/main-fixtures.js'
203 }
204 }
205 });
206};
207```
208
209## Contributing
210In 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`.
211
212## Donating
213Support this project and [others by twolfson][twolfson-projects] via [donations][twolfson-support-me].
214
215<http://twolfson.com/support-me>
216
217[twolfson-projects]: http://twolfson.com/projects
218[twolfson-support-me]: http://twolfson.com/support-me
219
220## Unlicense
221As of Mar 03 2016, Todd Wolfson has released this repository and its contents to the public domain.
222
223It has been released under the [UNLICENSE][].
224
225[UNLICENSE]: UNLICENSE