パッケージの詳細

cypress-fail-fast

javierbrea662.3kMIT7.1.1

Skip the rest of Cypress tests on first failure

cypress, cypress-plugin, fail-fast, failure

readme

Build status Coverage Status Quality Gate Mutation testing badge

Renovate Last commit Last release

NPM downloads License

Cypress Fail Fast

Enables fail fast in Cypress, skipping the rest of tests on first failure.

It can be configured to skip all remaining tests in current spec file, in current run, or even in parallel runs.

Table of Contents

Installation

Add the plugin to devDependencies

npm i --save-dev cypress-fail-fast

Now, depending on your Cypress version, use one of the next methods:

Installation on Cypress 10 and higher

Inside cypress.config.ts file:

import cypressFailFast from "cypress-fail-fast/plugin";

export default defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      cypressFailFast(on, config);
    },
  },
});

In case you are using JavaScript, you may explicit the file extension in some cases:

import cypressFailFast from "cypress-fail-fast/plugin.js"

Note: This example shows how to install the plugin for e2e testing type. Read Cypress configuration docs for further info.

At the top of your support file (usually cypress/support/e2e.js for e2e testing type)

import "cypress-fail-fast";

Installation on Cypress versions lower than 10

Inside cypress/plugins/index.js:

module.exports = (on, config) => {
  require("cypress-fail-fast/plugin")(on, config);
  return config;
};

At the top of cypress/support/index.js:

import "cypress-fail-fast";

From now, if one test fail after its last retry, the rest of tests will be skipped:

Cypress results screenshot

Limitations and notes

  • All spec files will be loaded, even after entering "skip mode", but every tests and hooks inside them will be skipped.
  • The spec strategy does not work in headed mode, because for Cypress events it is like running a single spec, so all remaining tests will be skipped.

Configuration

Environment variables

  • FAIL_FAST_STRATEGY: 'spec'|'run'|'parallel'
    • If spec, only remaining tests in current spec file are skipped. This mode only works in "headless" mode.
    • If run, all remaining tests in all spec files are skipped (default value).
    • Use parallel to provide your own callbacks allowing to notify from one run to the others when remaining tests should be skipped.
  • FAIL_FAST_ENABLED: boolean = true Allows disabling the "fail-fast" feature globally, but it could be still enabled for specific tests or describes using configuration by test.
  • FAIL_FAST_PLUGIN: boolean = true If false, it disables the "fail-fast" feature totally, ignoring even plugin configurations by test.
  • FAIL_FAST_BAIL: Number = 1 Enable the skip mode immediately upon n number of failing test suite. Defaults to 1.

Examples

CYPRESS_FAIL_FAST_PLUGIN=false npm run cypress

or set the "env" key in the cypress.json configuration file:

{
  "env":
  {
    "FAIL_FAST_STRATEGY": "run",
    "FAIL_FAST_ENABLED": true,
    "FAIL_FAST_BAIL": 2,
  }
}

Configuration by test

If you want to configure the plugin on a specific test, you can set this by using the failFast property in test configuration. The plugin allows next config values:

  • failFast: Configuration for the plugin, containing any of next properties:
    • enabled : Indicates wheter a failure of the current test or children tests (if configuration is applied to a suite) should produce to skip the rest of tests or not. Note that the value defined in this property has priority over the value of the environment variable CYPRESS_FAIL_FAST_ENABLED _(but not over CYPRESS_FAIL_FAST_PLUGIN, which disables the plugin totally)_.

Example

In the next example, tests are configured to "fail-fast" only in case the test with the "sanity test" description fails. If any of the other tests fails, "fail-fast" will not be applied.

describe("All tests", {
  failFast: {
    enabled: false, // Children tests and describes will inherit this configuration
  },
}, () => {
  it("sanity test", {
    failFast: {
      enabled: true, // Overwrite configuration defined in parents
    },
  }, () => {
    // Will skip the rest of tests if this one fails
    expect(true).to.be.true;
  });

  it("second test",() => {
    // Will continue executing tests if this one fails
    expect(true).to.be.true;
  });
});

Configuration examples for usual scenarios

You want to disable "fail-fast" in all specs except one:

Set the FAIL_FAST_ENABLED key in the cypress.json configuration file:

{
  "env":
  {
    "FAIL_FAST_ENABLED": false
  }
}

Enable "fail-fast" in those specs you want using configurations by test:

describe("All tests", { failFast: { enabled: true } }, () => {
  // If any test in this describe fails, the rest of tests and specs will be skipped
});
You want to totally disable "fail-fast" in your local environment:

Set the FAIL_FAST_PLUGIN key in your local cypress.env.json configuration file:

{
  "env":
  {
    "FAIL_FAST_PLUGIN": false
  }
}

Configuration for parallel runs

The plugin configuration supports defining two callbacks that, used in combination, allow to skip tests in one run when other run starts skipping tests also. Where, or how do you store the "flag" that allows to communicate your runs is in your hands, the plugin does not care about it.

To implement it, the plugin can receive an object with extra configuration as third argument when it is registered in the cypress/plugins/index.js file:

  • parallelCallbacks: Object containing next properties:
    • onCancel: function() This callback is executed on first test failure that produces the plugin starts skipping tests.
    • isCancelled: function(): boolean If this callback returns true, the plugin skips remaining tests.

These callbacks are executed only when the environment variable FAIL_FAST_STRATEGY is set to parallel.

Here is an example of configuration that would skip tests on many parallel runs when one of them starts skipping tests. It would only work if all parallel runs have access to the folder where the isCancelled flag is being stored as a file (easy to achieve if all of your parallel runs are being executed on Docker images on a same machine, for example). Note that this is only an example, you could also implement it storing the flag in a REST API, etc.

// Example valid for Cypress versions lower than 10. Use config file on Cypress 10

const fs = require("fs");
const path = require("path");

// Flag file is stored in the /cypress folder
const isCancelledFlagFile = path.resolve(__dirname, "..", ".run-is-cancelled");

module.exports = (on, config) => {
  require("cypress-fail-fast/plugin")(on, config, {
    parallelCallbacks: {
      onCancel: () => {
        // Create flag file when the plugin starts skipping tests
        fs.writeFileSync(isCancelledFlagFile, "");
      },
      isCancelled: () => {
        // If any other run has created the file, start skipping tests
        return fs.existsSync(isCancelledFlagFile);
      },
    },
  });

  return config;
};

Note that this example requires to remove the created file when all of the runs have finished, or tests will always be skipped whenever any run starts again. So, the FAIL_FAST_STRATEGY environment variable should be set to parallel only in CI pipelines where the workspace is cleaned on finish, for example.

Usage with TypeScript

If you are using TypeScript in the Cypress plugins file, this plugin includes TypeScript declarations and can be imported like the following:

import cypressFailFast = require("cypress-fail-fast/plugin");

export default (on: Cypress.PluginEvents, config: Cypress.PluginConfigOptions): Cypress.PluginConfigOptions => {
  cypressFailFast(on, config);
  return config;
};

Note: The example above is only valid for Cypress versions lower than 10. Use the configuration file in Cypress 10.

Tests

To ensure the plugin stability, the current major version is being tested with Cypress major versions 9.x, 10.x, 11.x, 12.x and 13.x, and new releases will be published for each new Cypress minor or major releases, updating the E2E tests.

Minor versions used in the E2E tests can be checked in the devDependencies of the package.json files of the E2E tests:

Even when current major version may work with previous Cypress versions, it is not currently tested, so, to be sure it works you should use:

  • Cypress 8.x may work, but it was tested until cypress-fail-fast 7.0.x
  • If you need Cypress 7 support, use cypress-fail-fast 6.x
  • If you need Cypress 6 support, use cypress-fail-fast 5.x
  • If you need Cypress 5 or lower, use cypress-fail-fast <= 4.x

If you find any issue for a specific Cypress version, please report it at https://github.com/javierbrea/cypress-fail-fast/issues.

Acknowledgements

This plugin has been developed based on the solutions proposed by the community on this Cypress issue, so thanks to all! I hope this plugin can be deprecated soon, as soon as the Cypress team adds native support for this feature. 😃

Contributing

Contributors are welcome. Please read the contributing guidelines and code of conduct.

License

MIT, see LICENSE for details.

更新履歴

Change Log

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

[unreleased]

Added

Changed

Fixed

Removed

BREAKING CHANGES

[7.1.1] - 2024-08-04

Fixed

  • fix(#296): Force to set the shouldSkip flag to true when a before hook fails in Cypress 13.

Changed

  • docs(#295): Change installation example to ES6 import syntax
  • chore(deps): Update devDependencies

[7.1.0] - 2023-11-21

Added

  • test: Add Cypress v13 tests. Use Cypress 13 in TypeScript tests

Removed

  • test: Drop Cypress 12 tests

Changed

  • chore(deps): Update devDependencies

[7.0.3] - 2023-08-26

Fixed

Changed

  • test(deps): Update Cypress to latest version in TypeScript E2E tests

[7.0.2] - 2023-08-16 [YANKED]

Changed

  • chore(deps): Update devDependencies
  • chore(deps): Use NodeJs 16.x, 18.x and 20.x in pipelines
  • refactor: Remove redundant double negation
  • refactor: Use optional chain expressions

Added

  • chore: Handle concurrency in pipelines

[7.0.1] - 2023-04-04

Changed

  • chore(deps): Update devDependencies

[7.0.0] - 2022-12-08

Added

  • test: Add Cypress v12 tests

Removed

  • test: Drop support for Cypress 7

[6.0.0] - 2022-12-07

Added

  • feat(#275): Extend suite configuration definitions for Cypress 11.x
  • test: Add Cypress v11 tests. Use Cypress 11 in TypeScript tests

Changed

  • chore(deps): Update devDependencies

Removed

  • test: Drop support for Cypress 6

Fixed

  • docs(#245): Fix parallel configuration example

[5.0.1] - 2022-08-30

Changed

  • chore(deps): Update devDependencies

[5.0.0] - 2022-06-02

Added

  • feat(#213): Add support for Cypress 10.
  • test(#213): Run E2E tests using also Cypress 10. Adapt config.
  • docs(#213): Add docs about how to install the plugin in Cypress 10

Removed

  • chore: Drop support for Cypress 5

[4.0.0] - 2022-05-30

Removed

  • chore: Drop support for Node.js versions lower than v14

Changed

  • chore(deps): Update devDependencies

[3.4.1] - 2022-03-02

Fixed

  • fix(#193): Do not log "Enabling skip mode" in every failed test. When a test fails, log "Failed tests: x/y", where y is the bail option.

Changed

  • chore(deps): Update devDependencies

[3.4.0] - 2022-02-24

Added

  • feat(#186): Add CYPRESS_FAIL_FAST_BAIL option

Changed

  • chore(deps): Update package-lock files to NPM v8
  • test(e2e): Increase tests stability. Fix flaky test in parallel specs
  • test(e2e): Turn tsc check into an assertion
  • refactor: Reduce cognitive complexity
  • chore(deps): Update devDependencies

[3.3.0] - 2021-11-13

Added

  • feat(#169): Support Cypress 9.x
  • test(#169): Run E2E tests also with Cypress 9.x
  • chore: Do not run pipelines with Node.js v12.x in order to make tests lighter

Changed

  • chore(deps): Support any Cypress version greater than 5.x in peerDependencies.
  • chore(deps): Update devDependencies
  • chore(deps): Configure renovate to not upgrade Cypress major versions in E2E tests of versions 7.x and 8.x

Removed

  • docs: Remove npm dependencies broken badge

[3.2.0] - 2021-11-01

Changed

  • chore(deps): Update devDependencies
  • chore(deps): Support any NodeJs version greater than 10.x.

Fixed

  • fix(#124): Skip nested before hooks when one fails (#164)

[3.1.1] - 2021-08-21

Added

  • test(#151): Add TypeScript types check. Check types also in E2E tests
  • docs: Add Cypress v8.x support to docs

Changed

  • chore(deps): Update dependencies

Fixed

  • fix(#151): Fix TypeScript declarations. Remove TestConfigOverrides recursively references

[3.1.0] - 2021-07-22

Added

  • chore(#129): Support Cypress v8.x in peerDependencies. Add E2E tests using Cypress v8

Changed

  • chore(deps): Update dependencies

[3.0.0] - 2021-06-23

Added

  • feat(#119): Force the next test to fail when a "before" or "beforeEach" hook fails, so the execution is marked as "failed", and fail fast mode can be enabled.
  • feat: Add logs when skip mode is enabled, and when Cypress runner is stopped.

Changed

  • refactor: Improve code readability
  • chore(deps): Update dependencies

Removed

  • feat: Do not apply fail fast on other hooks apart from "before" and "beforeEach"

BREAKING CHANGES

  • Fail fast is only applied on "before" and "beforeEach" hooks failures. Other hooks are ignored.

[2.4.0] - 2021-06-10

Added

  • feat(#91): Enter skip mode if any hook fails

Changed

  • chore(deps): Update devDependencies

[2.3.3] - 2021-05-29

Changed

  • chore(deps): Update devDependencies
  • chore: Migrate Sonar project

[2.3.2] - 2021-04-28

Added

  • chore(deps): Support Node v16.x in engines. Run tests also in node 16.0.0

Changed

  • chore(deps): Update devDependencies

[2.3.1] - 2021-04-07

Added

  • chore(deps): Support Cypress v7.x in peerDependencies
  • test(e2e): Run e2e tests also in Cypress v7.x

Changed

  • chore(pipelines): Update node versions
  • chore(pipelines): Do not run tests in Node 10, because it is not supported by Cypress v7.x
  • chore(deps): Update devDependencies
  • chore(renovate): Configure renovate to not update Cypress to a version higher than 6.x in Cypress 6.x e2e tests folder
  • test(e2e): Do not trace npm commands logs until DEBUG environment variable is set to true

[2.3.0] - 2021-04-04

Added

  • feat: Add FAIL_FAST_STRATEGY environment variable, allowing to skip tests only in current spec file, in current run or in parallel runs (#29)
  • feat: Add configuration allowing to implement fail-fast in parallel runs (#33).

Changed

  • chore(ci): Separate test mutation job to a new workflow
  • chore(deps): Update devDependencies

[2.2.2] - 2021-03-30

Fixed

  • fix: Fix configuration by test in Cypress versions higher than 6.6 (#73)

Changed

  • chore(deps): Update devDependencies

[2.2.1] - 2021-03-24

Fixed

  • fix(ts): Make failFast property optional in test configuration (#69)
  • docs: Fix typo in readme

Changed

  • chore(deps): Update devDependencies

[2.2.0] - 2021-02-21

Added

  • test(e2e): Check that test:after:run event is executed in failed tests using mochawesome reporter (closes #61)
  • feat: Stop Cypress runner in before hook in headless mode when tests should be skipped (closes #52)

Fixed

  • fix: Mark current test as pending when it has to be skipped (related to #61)

[2.1.1] - 2021-02-21

Changed

  • chore(deps): Configure renovate to no upgrade Cypress version in v5 e2e tests

Fixed

  • fix: Revert change producing unstability (#61).

Removed

  • chore(deps): Remove unused devDependency

[2.1.0] - 2021-02-21

Added

  • test(e2e): Check that test:after:run event is executed in failed tests.

Changed

  • feat: Do not stop runner from failed test hook and execute flag task "parallely" in order to let execute test:after:run events. (closes #61)
  • test(e2e): Update Cypress 6 to latest version.
  • chore(deps): Update devDependencies

Removed

  • test(unit): Remove duplicated test

[2.0.0] - 2021-01-17

Added

  • feat: Add FAIL_FAST_ENABLED environment variable (#53)
  • feat: Allow environment variables to be enabled with 1, and disabled with 0

Changed

  • feat: Rename FAIL_FAST environment variable to FAIL_FAST_PLUGIN (#53)
  • test(e2e): Allow some tests to be executed only in last Cypress version in order to reduce timings
  • chore(deps): Update devDependencies

BREAKING CHANGES

  • feat: Plugin is now enabled by default (#44). To disable it, FAIL_FAST_PLUGIN environment variable has to be explicitly set as "false". Removed FAIL_FAST environment variable, which now has not any effect.

[1.4.0] - 2021-01-02

Added

  • feat: Add suite and tests plugin custom configuration. Enable or disable plugin for suites or tests using the enabled property from custom config
  • test(e2e): Add helper to run E2E tests with different specs files and configurations

Changed

  • feat: Do not log plugin tasks, except when setting shouldSkip flag to true
  • docs: Change TypeScript example
  • refactor: Do not check plugin configuration inside Node.js plugin
  • refactor: Rename plugin tasks. Start all with same namespace

Removed

  • chore: Remove unused eslint settings from src folder

[1.3.1] - 2020-12-31

Fixed

  • docs: Fix E2E tests versions links

[1.3.0] - 2020-12-31

Added

  • feat: Add TypeScript declarations (#37)
  • test(e2e): Add E2E tests using TypeScript in Cypress

Changed

  • test(e2e): Refactor E2E tests to avoid code duplications. Now there is a common tests runner and code is generated for each different Cypress variant (except package.json files in order to allow renovate continue updating dependencies)
  • docs: Update contributing guidelines
  • chore(deps): Update dependencies

[1.2.1] - 2020-12-10

Fixed

  • docs(badge): Fix build badge

[1.2.0] - 2020-12-10

Added

  • chore(deps): Add node 10.x support

Changed

  • docs(readme): Improve docs
  • chore(deps): Update Cypress 6.x version used in E2E
  • chore(pipeline): Migrate pipelines to Github actions

[1.1.0] - 2020-11-28

Added

  • test(deps): Add support for Cypress v6.x

Fixed

  • docs(readme): Fix installation instructions

[1.0.0] - 2020-11-28

Added

  • First release