Configuring Jasmine

A typical Jasmine setup includes two components: jasmine-core itself and a tool that loads specs, runs jasmine-core, and reports the result. We’ll refer to the latter as a “runner” here. The runner and jasmine-core each have their own configuration, but they are usually combined into a single runner-specific config file. For the Node CLI provided by the jasmine package, the standard config file location is spec/support/jasmine.mjs. For jasmine-browser-runner, it’s spec/support/jasmine-browser.mjs. You can generate a starter config file with jasmine init or jasmine-browser-runner init. Configuration files with .json or .js extensions are also supported. If you’re working with an existing project, you might have one of those.

For both jasmine and jasmine-browser-runner, core configuration is set via the env property in the config file:

export default {
  env: {
    // These are core configuration properties
    stopSpecOnExpectationFailure: false,
    random: true,
    forbidDuplicateNames: true
  },
  // Everything outside env is runner configuration, not core configuration
}

Third-party runners such as web-test-runner-jasmine usually use the same approach of embedding jasmine-core’s configuration in the runner’s configuration. If you’re using a third-party runner, check its documentation for specifics.

The standalone distribution does not have a config file. Instead, you can replace the comment <!-- optional: include a file here that configures the Jasmine env --> in SpecRunner.html with JavaScript code that calls jasmine.getEnv().configure(...).

Specifying which files to run

There are four main kinds of JavaScript files you might want Jasmine to load: spec files, source files, requires, and helper files. The intended usage is that spec files contain your suites and specs, source files contain the code under test, helper files contain supporting code that’s used by multiple spec files, and requires contain code that configures Node. The practical difference between the types of files is when each one is loaded and executed. Requires are loaded first and spec files are loaded last.

Source files are only used in browser-based projects that use plain old script tags. If your code is made up of either CommonJS or ES modules, you shouldn’t specify source files. The JavaScript runtime will automatically load modules when it encounters import statements or require calls in your spec files.

Files are specified via these configuration properties:

What jasmine config property jasmine-browser-runner config property Relative to Glob?
Spec directory spec_files specFiles working directory no
Spec files spec_files specFiles spec directory yes
Helper files helpers helpers spec directory yes
Source directory n/a srcDir working directory no
Source files n/a srcFiles source directory yes
CSS files n/a cssFiles source directory yes
Requires requires n/a working directory no

For example, the default configuration file generated by jasmine init looks like this:

export default {
  spec_dir: "spec",
  spec_files: [
    "**/*[sS]pec.?(m)js"
  ],
  // ...
}

That combination of spec_dir and spec_files will cause all of these files to be loaded:

For more information about glob patterns like **/*[sS]pec.?(m)js, see the glob package README.

If you use the standalone distribution, there is nothing to configure: you are in charge of file loading, not Jasmine. Just add script tags to SpecRunner.html in whatever order you want the browser to load the files.

Controlling randomization

By default, Jasmine runs specs in random order. This is to help catch specs that accidentally affect each other’s behavior, causing the suite to only pass if the specs are run in a particular order. Disabling randomization is not recommended, but you can do it if you want by adding random: false to your jasmine-core configuration.

To reproduce a randomization-related failure, you can re-run Jasmine with the random seed that was reported by the failing run. The most convenient way to do that is by passing the seed flag to the jasmine or jasmine-browser-runner command, e.g jasmine --seed=12345. Or you can add the seed property to your jasmine-core configuration.

Complete configuration reference