Upgrading to Jasmine 7.0

Contents

  1. If you use Karma or Zone.js
  2. Preparation
  3. Upgrading jasmine-core
  4. Updating your code for 7.0
    1. Node boot changes
    2. Browser boot changes
    3. Node configuration changes
      1. jsLoader: “require”
      2. Default reporter configuration
    4. Reporters
    5. Jasmine-browser-runner middleware

If you use Karma or Zone.js

The current version of karma-jasmine is incompatible with jasmine-core 7.x. Karma-jasmine was deprecated in 2022 and has not been updated since then. If you use Karma, consider switching to a maintained alternative such as jasmine-browser-runner or web-test-runner-jasmine.

Zone.js relies on monkey patching jasmine-core. That no longer works in 7.0. If you use Zone.js, you won’t be able to upgrade to jasmine-core 7.0 or later.

Preparation

Before installing 7.x, upgrade to the latest jasmine-core 6.x and, if applicable, the latest jasmine 6.x, if applicable. Then fix any deprecation warnings that you see when you run your specs.

Upgrading jasmine-core

If you’re using the jasmine package, upgrade to version 7.0.0 or later. This will also cause jasmine-core to be upgraded. If you’re using jasmine-browser-runner, upgrade to version 5.0.0 or later. Your package manager (npm/yarn/etc) should automatically install jasmine-core 7.0.0 or later unless your package.json file specifies an earlier version.

Updating your code for 7.0

Node boot changes

The node.js boot APIs exposed by jasmine-core have changed. If you call them directly rather than using the jasmine package, you’ll need to update your code. In 6.x, the boot function took a parameter indicating whether to reinitialize jasmine-core or return an existing instance, and created globals such as it and expect. There was a separate noGlobals function that booted without creating globals. In 7.0, boot and noGlobals no longer exist. Instead, jasmine-core boots itself but does not automatically install globals. It exports an installGlobals function that installs the globals that were previously installed by boot, and a reset function to discard all state and configuration and reinitialize.

Scenario6.x7.0
Typical usage with globals
const jasmineCore = require('jasmine-core');
jasmineCore.boot();

const jasmineCore = require('jasmine-core');
jasmineCore.installGlobals();
No globals
const jasmineCore = require('jasmine-core');
jasmineCore.noGlobals();
const jasmineCore = require('jasmine-core');
Discard existing state
const jasmineCore = require('jasmine-core');
jasmineCore.boot(false);
const jasmineCore = require('jasmine-core');
jasmineCore.reset();
jasmineCore.installGlobals();

Browser boot changes

The browser boot process has been simplified. If you use jasmine-browser-runner, you don’t need to make any changes. If you maintain your own boot files, you should update them to match the boot.js file distributed with jasmine-core. If you need to call jasmine.getEnv().configure(), you can do that any time after jasmine.js loads and before env.execute() is called.

Node configuration changes

jsLoader: "require"

The jsLoader configuration setting is no longer supported. You may be able to just remove it, since the default loader now handles most of the original uses for jsLoader: "require". If that doesn’t work, you can provide a custom loader that implements any module loading strategy you need.

Default reporter configuration

Scenario6.x7.0
Configuring default reporter via config file (jasmine.mjs, jasmine.json, etc)
{
   "alwaysListPendingSpecs": false,
}
{
   "defaultReporter": {
      "alwaysListPendingSpecs": false
   }
}
Configuring default reporter via the Jasmine or ParallelRunner classes
const runner = new Jasmine();
runner.showColor(false);
runner.alwaysListPendingSpecs(false);
const runner = new Jasmine();
runner.configureDefaultReporter({
   color: false,
   alwaysListPendingSpecs: false
});

Reporters

Jasmine-core 7.0.0 adds a new spec status 'notApplicable', which is similar to 'pending' but has a more specific meaning. It means that the spec was skipped because it doesn’t apply to the current environment. It’s emitted when the spec calls notApplicable. If you have a reporter that deals with spec statuses, update it to handle'notApplicable'.

Jasmine-browser-runner middleware

Jasmine-browser-runner no longer depends on Express. That won’t affect most users, but it’s a breaking change if you use custom middleware. Middleware are now functions that take request, response, and next parameters. For example:

// jasmine-browser.mjs
module.exports = {
  // ...  
  "middleware": {
    "/": function(req, res, next) {
      res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
      res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
      next();
    }
  }
};