Neutrino Karma Preset
@neutrinojs/karma is a Neutrino preset that supports testing web applications using the Karma test runner.
- Zero upfront configuration necessary to start testing on real browsers with Karma, Mocha, and Chrome Headless
- Babel compilation that compiles your tests using the same Babel options used by your source code
- Source watching for re-running of tests on change
- Out-of-the-box support for running in CI
- Easily extensible to customize your testing as needed
- Node.js v6 LTS, v8, v9
- Yarn v1.2.1+, or npm v5.4+
- Neutrino v8, Neutrino build preset
@neutrinojs/karma can be installed via the Yarn or npm clients. Inside your project, make sure
@neutrinojs/karma are development dependencies. You will also be using
another Neutrino preset for building your application source code.
❯ yarn add --dev @neutrinojs/karma
❯ npm install --save-dev @neutrinojs/karma
@neutrinojs/karma follows the standard project layout specified by Neutrino. This
means that by default all project test code should live in a directory named
test in the root of the
project. Test files end in
_test.js by default.
After adding the Karma preset to your Neutrino-built project, add a new directory named
test in the root of the
project, with a single JS file named
simple_test.js in it.
❯ mkdir test && touch test/simple_test.js
test/simple_test.js file with the following:
Now edit your project's package.json to add commands for testing your application. In this example, let's pretend this is a React project:
Or if you are using
.neutrinorc.js, add this preset to your use array instead of
Run the tests, and view the results in your console:
❯ yarn testSTART:21 11 2017 06:56:39.804:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/21 11 2017 06:56:39.806:INFO [launcher]: Launching browser ChromeHeadless with unlimited concurrency21 11 2017 06:56:39.809:INFO [launcher]: Starting browser ChromeHeadless21 11 2017 06:56:40.170:INFO [HeadlessChrome 0.0.0 ]: Connected on socket PW-kCVej8pQuT-HAAAAA with id 14691980simple✔ should be saneFinished in 0.005 secs / 0 secs @ 06:56:40 GMT-0600SUMMARY:✔ 1 test completed
❯ npm testSTART:21 11 2017 06:56:39.804:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/21 11 2017 06:56:39.806:INFO [launcher]: Launching browser ChromeHeadless with unlimited concurrency21 11 2017 06:56:39.809:INFO [launcher]: Starting browser ChromeHeadless21 11 2017 06:56:40.170:INFO [HeadlessChrome 0.0.0 ]: Connected on socket PW-kCVej8pQuT-HAAAAA with id 14691980simple✔ should be saneFinished in 0.005 secs / 0 secs @ 06:56:40 GMT-0600SUMMARY:✔ 1 test completed
To run tests against files from your source code, simply import them:
For more details on specific Karma usage, please refer to their documentation.
Executing single tests
By default this preset will execute every test file located in your test directory ending in the appropriate file
extension. Use the command line
files parameters to execute individual tests.
Watching for changes
@neutrinojs/karma can watch for changes on your source directory and subsequently re-run tests. Simply use the
--watch flag with your
neutrino test command.
You can provide custom options and have them merged with this preset's default options, which are subsequently passed
to Karma. You can modify Karma settings from
.neutrinorc.js by overriding with any options Karma accepts. In a standalone
Karma project this is typically done in a
karma.conf.js or similar file, but
.neutrinorc.js as well. This accepts the same configuration options as outlined in the
Karma documentation. Use an array pair instead of
a string to supply these options.
Example: Change the duration Karma waits for a browser to reconnect (in ms).
moduleexports =use:'@neutrinojs/karma' browserDisconnectTimeout: 5000;
If you wish to completely override the Karma configuration instead of it being merged, set the
override property to
true in the preset options:
moduleexports =use:'@neutrinojs/karma'override: true/* specify all other Karma configuration options */;
Using from CI
@neutrinojs/karma needs no additional configuration to run your tests in CI infrastructure when using Chrome Headless.
If you decide to use a browser with a display, you will need to ensure your CI can actually run the tests similar to
a headless mode. This usually means having a display emulator and access to the browsers you are testing against.
For an example using Travis-CI and normal Chrome, you will need to add the following to your
before_install:- export CHROME_BIN=chromium-browser- export DISPLAY=:99.0- sh -e /etc/init.d/xvfb start
You may also need to pass additional options to the Karma preset to change its behavior in CI, using standard Chrome as an example instead of Chrome Headless:
moduleexports =use:'@neutrinojs/karma'browsers: processenvCI ? 'ChromeCI' : 'Chrome'customLaunchers:ChromeCI:base: 'Chrome'flags: '--no-sandbox'