UNPKG

5.79 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.37.4` and `electron@1.3.3` but 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 in 5.0.0
30We have corrected inaccuracies with `file://` behavior from Electron. For example:
31
32- `__filename` is now Karma's `context.html`
33- Relative paths for `require` resolve from Karma's `context.html` directory
34
35We have transferred support for this to the option `client.loadScriptsViaRequire` which loads scripts via `require` and has the original expected Node.js behavior
36
37For more information, see https://github.com/twolfson/karma-electron/issues/11
38
39## Getting Started
40On a project that has been set up with `karma init` already, install the module via:
41
42```bash
43# Install our module and `electron`
44npm install karma-electron electron
45```
46
47Then, configure the module:
48
49```js
50// Inside `karma.conf.js`
51browsers: ['Electron']
52
53// If you would like Node.js integration support (e.g. `require`)
54// then, you must include this in `preprocessors` and `client`
55// DEV: preprocessors is for backfilling `__filename` and local `require` paths
56preprocessors: {
57 '**/*.js': ['electron']
58},
59// DEV: `useIframe: false` is for launching a new window instead of using an iframe
60// In Electron, iframes don't get `nodeIntegration` priveleges yet windows do
61client: {
62 useIframe: false
63}
64```
65
66Then, we can run Karma:
67
68```bash
69karma start
70```
71
72## Documentation
73### Environment variables
74- ELECTRON_BIN - Override path to use for `electron`
75 - By default, we will use path given by `electron`
76
77**Example:**
78
79```bash
80ELECTRON_BIN=/usr/bin/electron karma start
81```
82
83### Script configuration
84We support the following configurations:
85
86- client `Object` - Container for configuring child windows loaded from Karma
87 - __filenameOverride `String` - Override `__filename` to be another path (e.g. `/path/to/my-index.html`)
88 - This will also affect `__dirname` and `module.filename` as those are derived from `__filename`
89 - By default, `__filename` will point to Karma's `context.html`
90 - loadScriptsViaRequire `Boolean` - Load scripts via `require` instead of `<script src=`
91 - This sets `__filename`, `__dirname`, and `module` to match the script instead of Karma's `context.html`
92 - By default, this is `false` and we directly load the original scripts content
93
94**Example:**
95
96```js
97module.exports = function (config) {
98 config.set({
99 client: {
100 // DEV: These 2 options aren't typically used together
101 // This is for demonstration purposes
102
103 // Override top level `__filename` to be `/home/.../my-electron-app/index.html`
104 // where `__dirname` is `/home/.../my-electron-app`
105 __filenameOverride: __dirname + '/index.html',
106
107 // Use `require` instead of `<script src=` to load scripts
108 loadScriptsViaRequire: true
109 }
110 });
111};
112```
113
114### Launcher configuration
115We support configuration via Karma's custom launcher inheritance:
116
117- flags `Array` - List of Chromium flags to alter Electron's behavior
118 - https://github.com/atom/electron/blob/v0.36.9/docs/api/chrome-command-line-switches.md
119 - We added support for a `--show` to allow making the Karma window visible
120- userDataDir `String` - Directory to store cookies/localStorage information
121 - By default, this is a random directory generated by Karma (e.g. `/tmp/karma-5355024`)
122
123**Example:**
124
125```js
126module.exports = function (config) {
127 config.set({
128 // Specify usage of our custom launcher
129 browsers: ['CustomElectron'],
130
131 // Define a custom launcher which inherits from `Electron`
132 customLaunchers: {
133 CustomElectron: {
134 base: 'Electron',
135 userDataDir: __dirname + '/.electron',
136 flags: ['--show']
137 }
138 }
139 });
140};
141```
142
143## Contributing
144In 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`.
145
146## Donating
147Support this project and [others by twolfson][gratipay] via [gratipay][].
148
149[![Support via Gratipay][gratipay-badge]][gratipay]
150
151[gratipay-badge]: https://cdn.rawgit.com/gratipay/gratipay-badge/2.x.x/dist/gratipay.svg
152[gratipay]: https://www.gratipay.com/twolfson/
153
154## Unlicense
155As of Mar 03 2016, Todd Wolfson has released this repository and its contents to the public domain.
156
157It has been released under the [UNLICENSE][].
158
159[UNLICENSE]: UNLICENSE