Allure Jasmine

Allure Jasmine npm latest version

Generate beautiful HTML reports using Allure Report and your Jasmine tests.

This adapter also can be used with the Jasmine-based versions of Jest (version 26 or older).

Allure Report Jasmine Example

How to start

1. Prepare your project

  1. Open a terminal and go to the project directory. For example:

    cd /home/user/myproject
  2. Install the Allure Report command-line tool, if it is not yet installed in your operating system. Note that Allure Report requires Java, see the installation instructions.

    npm install --save-dev allure-commandline
    yarn add --dev allure-commandline
    pnpm install --dev allure-commandline
  3. Install the Allure Jasmine adapter.

    npm install --save-dev allure-jasmine
    yarn add --dev allure-jasmine
    pnpm install --dev allure-jasmine
  4. Create a helper file, e.g., helpers/setup.js or helpers/setup.ts.

    You can choose any filename, as long as the test framework can find it according to its configuration.

    • If you use Jasmine, make sure that the filename matches one of the regular expressions in the helpers option from your spec/support/jasmine.json file.

    • If you use Jest, make sure that the filename is listed in the setupFilesAfterEnv option from your jest.config.js file.

    In the helper file, write the following minimal setup code. This not only registers the reporter for gathering your test results, but also creates the allure variable which you can then import and use in your tests, see the examples in Writing tests below.

    import { Status, TestResult } from "allure-js-commons"; import { JasmineAllureReporter } from "allure-jasmine"; import { JasmineAllureInterface } from "allure-jasmine/dist/src/JasmineAllureReporter"; const reporter = new JasmineAllureReporter({}); jasmine.getEnv().addReporter(reporter); export const allure = reporter.getInterface(); // Enable type hinting (for TypeScript projects only) declare global { const allure: JasmineAllureInterface; }
  5. Configure other options if necessary, see Allure Jasmine configuration.

2. Run tests

Run your Jasmine tests same way as your would run them usually. For example:

npm test
yarn test
pnpm test

This will save necessary data into allure-results or other directory, according to the Configuration. If the directory already exists, the new files will be added to the existing ones, so that a future report will be based on them all.

3. Generate a report

Finally, run Allure to convert the test results into an HTML report. This will automatically open your browser to view the report.

npx allure serve allure-results
yarn dlx allure serve allure-results
pnpx allure serve allure-results

If necessary, replace allure-results with the path to the directory specified in the Configuration.

There are some options that can affect how the report is generated. Run allure --help for the full list of options.

Writing tests

The Allure Jasmine adapter extends the standard reporting features of Jasmine by providing additional capabilities for crafting more informative and structured tests. This section highlights key enhancements that can be utilized:

Adding Metadata

Allure allows you to enrich your reports with a variety of metadata. This additional information provides context and details for each test, enhancing the report's usefulness. Refer to the metadata reference section for an exhaustive list of what can be added.

To assign a metadata field, call a corresponding method at any point inside a test method's body. Note, however, that it is highly recommended to assign all metadata as early as possible. Otherwise, there is a risk of the test failing before having all metadata set, which is bad for the test report's readability.

import { allure } from "./helpers/setup"; describe("Test My Website", function() { it("Test Authentication", async function() { await allure.description( "This test attempts to log into the website using a login and a password. Fails if any error happens.\n\nNote that this test does not test 2-Factor Authentication.", ); await allure.owner("John Doe"); await"", "Website"); await allure.issue("AUTH-123", ""); await allure.tms("TMS-456", ""); // ... });

Organize tests

As described in Improving navigation in your test report, Allure supports multiple ways to organize tests into hierarchical structures.

To specify a test's location in the behavior-based hierarchy:

import { allure } from "./helpers/setup"; describe("Test My Website", function () { it("Test Authentication", async function () { await allure.epic("Web interface"); await allure.feature("Essential features"); await allure.story("Authentication"); // ... }); });

To specify a test's location in the suite-based hierarchy:

import { allure } from "./helpers/setup"; describe("Test My Website", function () { it("Test Authentication", async function () { await allure.parentSuite("Tests for web interface"); await allure.suite("Tests for essential features"); await allure.subSuite("Tests for authentication"); // ... }); });

Divide a test into steps

To create steps and sub-steps, you can use the step() function, see the reference.

import { allure } from "./helpers/setup"; describe("Test My Website", function () { it("Test Authentication", async function () { await allure.step("Step 1", async function () { await allure.step("Sub-step 1", async function () { // ... }); await allure.step("Sub-step 2", async function () { // ... }); }); await allure.step("Step 2", async function () { // ... }); }); });

Describe parametrized tests

Since tests in Jasmine, unlike in some other frameworks, are written as anonymous functions, it is very easy to implement the parametrized tests pattern, i.e. to run the same test logic with different test data. To do so, just write the test inside a loop and use the variable parameters in both its title and its body.

To display a parameter value in the test report, pass it to the parameter() function.

import { allure } from "./helpers/setup"; describe("Test My Website", function () { for (const login of ["johndoe", "[email protected]"]) { it(`Test Authentication as ${login}`, async function () { allure.parameter("login", login); allure.parameter("time", new Date().toUTCString(), { excluded: true }); // ... }); } });

Attach screenshots and other files

In Allure reports, you have the ability to attach various types of files, which can greatly enhance the comprehensibility of the report. A common practice is to attach screenshots that capture the state of the user interface at specific moments during test execution.

For detailed instructions on how to implement attachments, refer to the attachments section in the Allure Jasmine reference.

import { allure } from "./helpers/setup"; import { ContentType } from "allure-js-commons"; import * as fs from "fs"; describe("Test My Website", function () { it("Test Authentication", async function () { // ... await allure.attachment("Text file", "This is the file content.", ContentType.TEXT); await allure.attachment("Screenshot", fs.readFileSync("/path/to/image.png"), { contentType: ContentType.PNG, fileExtension: "png", }); }); });

Select tests via a test plan file

Test plan is currently not supported by the Allure Jasmine adapter.

Environment information

For the main page of the report, you can collect various information about the environment in which the tests were executed. To do so, pass the information to allure.writeEnvironmentInfo().

For example, it is a good idea to use this to remember the OS version and Node.js version. This may help the future reader investigate bugs that are reproducible only in some environments.

Allure Report Environments Widget

import * as os from "os"; // ... allure.writeEnvironmentInfo({ os_platform: os.platform(), os_release: os.release(), os_version: os.version(), node_version: process.version, });

Note that this feature should be used for properties that do not change for all tests in the report. If you have properties that can be different for different tests, consider using Parametrized tests.

Powered by

Join our newsletter

Join our community

We aim to make Allure Report as reliable and user-friendly as possible, and together with the community, we're here to help when problems arise.

© 2024 Qameta Software Inc. All rights reserved.