Run jasmine tests on a jspm project, without karma.
Switch branches/tags
Clone or download
Pull request Compare This branch is 1 commit ahead, 43 commits behind CanopyTax:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
bin
src
.babelrc
.gitignore
.npmignore
LICENSE
README.md
package.json

README.md

node-jspm-jasmine

Run jasmine tests on a jspm project, without karma. Keep your existing jasmine.json, config.js, and jspm_packages. The hope is that everything will just "work out of the box".

Quickstart

Run the following commands, and you're all done.

npm install --save-dev jspm jasmine node-jspm-jasmine
npm install -g jspm jasmine node-jspm-jasmine
jspm init
jasmine init
jspmjasmine

You'll probably want to add a test script to your package.json scripts with the command being jspmjasmine. After you do so, you can run the tests with the normal npm test. Example:

{
  "name": "name-of-package",
  "scripts": {
    "test": "jspmjasmine"
  }
}

CLI

node-jspm-jasmine exposes a cli that is accessible through the jspmjasmine command. All jasmine configuration options will be respected, with all helpers and spec_files being loaded via JSPM instead of node's require function.

  • Note: node-jspm-jasmine will expose the System object to the global, for convenience.
Options:

Provide a custom jasmine.json config file:
jspmjasmine --jasmine-config "RELATIVE/PATH/TO/jasmine.json"
Defaults to path where jspmjasmine is being executed + /spec/support/jasmine.json

Provide a custom directory to package.json:
jspmjasmine --package-path "RELATIVE/PATH/TO/PACKAGE/JSON/DIRECTORY/"
Defaults to path where jspmjasmine is being executed.

Add a coverage report:
jspmjasmine --coverage
Defaults to undefined (no coverage).

Provide a custom directory for storing coverage reports:
jspmjasmine --coverage-dir "RELATIVE/PATH/TO/COVERAGE/DIRECTORY/"
Defaults to "./coverage/".

Provide a custom type for coverage reports:
jspmjasmine --coverage-reporter "TYPE"
Defaults to "html" (all report types shipped with Istanbul are supported).

Provide files to be instrumented by Istanbul coverage:
jspmjasmine --coverage-files "RELATIVE/GLOB/PATH/TO/FILES/**/*.js"
Important: Be sure to put globs in quotes to prevent the OS from expanding them before node-jspm-jasmine gets them.
Defaults empty array (no files).

Allow node-jspm-jasmine to empty coverage directory before creating a new report:
jspmjasmine --clean-coverage-dir
Defaults to undefined (no clean up).

JS API

node-jspm-jasmine exports named exports which are to be used as a js library. Example:

import * as nodeJspmJasmine from 'node-jspm-jasmine';
nodeJspmJasmine.runTests({});

runTests(opts)

This will run your jasmine tests, loading all the tests with JSPM instead of node's require.

Options:
runTests({

  // Provide a custom jasmine.json config file
  // Defaults to path where `jspmjasmine` is being executed + `/spec/support/jasmine.json
  jasmineConfig: "RELATIVE/PATH/TO/jasmine.json",
  // Alternatively, you can pass the jasmine config object directly.
  // jasmineConfig: {
    // "spec_dir": "src/test/specs",
    // "spec_files": ["**/*.js"],
    // "helpers": []
  // },

  // Provide a custom directory to package.json
  // Defaults to path where `jspmjasmine` is being executed
  packagePath: "RELATIVE/PATH/TO/PACKAGE/JSON/DIRECTORY/",

  // Add a coverage report
  // Defaults to undefined
  coverage: {

    // Provide a custom directory for storing coverage reports
    // Defaults to "./coverage/"
    dir: "RELATIVE/PATH/TO/COVERAGE/DIRECTORY/",

    // Provide a custom type for coverage reports
    // Defaults to "html" (all report types shipped with Istanbul are supported)
    reporter: "TYPE",

    // Provide files to be instrumented by Istanbul coverage
    // Defaults empty array (no files)
    files: "RELATIVE/GLOB/PATH/TO/FILES/**/*.js",

    // Allow `node-jspm-jasmine` to empty coverage directory before creating a new report
    // Defaults to undefined (no clean up)
    cleanDir: true
  }
})

Ignoring / mocking specific imports

What you're about to read is nothing specific to node-jspm-jasmine, but I figured writing it down might save someone from sifting through hard-to-find docs for Jasmine and JSPM/SystemJS/es6-module-loader. In order to mock or ignore files, you should create a jasmine helper file so that the mocking/ignoring is done before the tests are run. These files by default go into your spec/helpers directory, but that can be controlled in the jasmine.json file (note that the file patterns that you put into the helpers array will be relative to the spec directory itself, not the package's root directory nor the spec/helpers directory). Once you've got a helper file, use System.registerDynamic to do one of the following:

Ignoring a dependency

System.registerDynamic('name-of-ignored-dependency-just-like-it-is-imported', [], false, function() {});

Mocking a dependency

System.registerDynamic('name-of-mocked-dependency-just-like-it-is-imported', ['name-of-dependency-of-mocked-module'], false, function(require, exports, module) {
  module.exports = {
    foo: 'bar',
  };
});

Usage with Enzyme

Although Enzyme is not related to jspm or jasmine, the reason this project was started was to figure out an easy way to get a JSPM + React project to run tests with Jasmine and Enzyme. So here's how:

  • Create a jasmine helper file, as explained above
  • In the jasmine helper file, put the following (this works for react 15.0, see enzyme's docs for webpack to find which specific things you need to mock/ignore for other React versions).
System.registerDynamic("react/lib/ReactContext.js", [], false, function() {})
System.registerDynamic("react/lib/ExecutionEnvironment.js", [], false, function() {})