Integrating with Jasmine

Jasmine is a universal test runner, particularly popular with projects based on Angular framework. If your project already uses Jasmine to run its unit tests, you can use the same runner for your acceptance tests too.

When you integrate Jasmine with Serenity/JS, the test framework augments your test reports with additional information about your test scenarios, even if they don't follow the Screenplay Pattern yet!

If you prefer to dive straight into the code, several reference implementations are available in the Serenity/JS GitHub repository. Those implementations demonstrate using Jasmine and Serenity/JS to run both REST API- and Web-based acceptance tests.

Integration architecture

@serenity-js/jasmine module provides a test runner adapter, or "reporter" in Jasmine-speak.

Serenity/JS test runner adapters translate the events that occur in the test runner to Serenity/JS DomainEvents; those can be then turned into test reports by Serenity/JS reporting services.

graph TB DEV(["fas:fa-laptop-code Developer"]) TR(["jasmine"]) SC(["@serenity-js/core"]) TRA(["@serenity-js/jasmine"]) CF["fas:fa-file config.ts"] R(["fas:fa-chart-pie Serenity/JS reporting services"]) DEV -- invokes --> TR TR -- loads --> CF TR -- notifies --> TRA TRA -- notifies --> SC CF -- configures --> SC SC -- notifies --> R class R socket click R "/handbook/reporting/index.html" click SC "/modules/core" click TRA "/modules/jasmine"

PRO TIP: Integration architecture described in this chapter is applicable when you want to invoke jasmine command line interface directly, for example for domain-level or REST/HTTP API-level testing.

If you want your Jasmine scenarios to interact with Web interfaces, check out Integrating with Protractor instead.

Installation

Assuming you already have a Node.js project set up, add the following dev dependencies:

To do that, run the following command in your terminal:

npm install --save-dev @serenity-js/{core,jasmine} jasmine

If you'd like to implement your test suite in TypeScript, also run:

npm install --save-dev typescript ts-node @types/{jasmine,node}

If you haven't done so already, initialise Jasmine configuration file at spec/support/jasmine.json by running the following command:

npx jasmine init

The configuration file should look as follows:

{
  "spec_dir": "spec",
  "spec_files": [
    "**/*[sS]pec.js"
  ],
  "helpers": [
    "helpers/**/*.js"
  ],
  "stopSpecOnExpectationFailure": false,
  "random": true
}

Configuration

Using JavaScript

In a JavaScript project, create a configuration file at spec/helpers/config.js with the following contents:

// spec/helpers/config.js
const { configure } = require('@serenity-js/core');

configure({
    crew: [
        // ... reporting services
    ],
});

Using TypeScript

To make Jasmine understand test scenarios and configuration written in TypeScript, modify the spec/support/jasmine.json file as follows:

{
  "spec_dir": "spec",
  "spec_files": [
    "**/*[sS]pec.ts"
  ],
  "helpers": [
    "helpers/**/*.ts"
  ],
  "requires": [
    "ts-node/register"
  ],
  "stopSpecOnExpectationFailure": false,
  "random": true
}

Next, create a configuration file at spec/helpers/config.ts.

This file will inform Serenity/JS what reporting services you wish to use:

// spec/helpers/config.ts
import { configure } from '@serenity-js/core';

configure({
    crew: [
        // ... reporting services
    ],
});

Reporting

To register @serenity-js/jasmine test runner adapter with Jasmine, use the --reporter option when invoking the runner:

npx jasmine --reporter='@serenity-js/jasmine'

To install and configure Serenity/JS reporting services appropriate for your project - follow the Serenity/JS reporting guide.

Love Serenity/JS? Spread the word on Twitter and LinkedIn!

Found a typo? You can fix it yourself or raise a ticket on GitHub.

If you'd like to help us bring more great tools and content to the Open-Source Community, become a GitHub Sponsor of Serenity/JS for as little as the cost of a cup of coffee.

Sponsor