Skip to content

A companion Cypress plugin for `cy-grep` that re-runs the last failed test(s).

License

Notifications You must be signed in to change notification settings

dennisbergevin/cypress-plugin-last-failed

Repository files navigation

Cypress plugin last-failed

A companion Cypress plugin for cy-grep that re-runs the last failed test(s).

Cypress-last-failed-toggle

Cypress run example image

Features

  • 👟 A new cypress run command to only run the previous run's failed tests
  • ⏳ A new UI toggle within cypress open to filter and only run any failed tests in a given spec
  • 🤖 CI/CD support

Table of Contents


📦 Installation

  1. Install the following packages:
npm install --save-dev @bahmutov/cy-grep # Dependent package for the plugin
npm install --save-dev cypress-plugin-last-failed
  1. In cypress/support/e2e.js (For E2E tests) and/or cypress/support/component.js (For Component tests),
import { failedTestToggle } from 'cypress-plugin-last-failed';

const registerCypressGrep = require('@bahmutov/cy-grep');
registerCypressGrep();

failedTestToggle();
  1. In cypress.config, include the following within setupNodeEvents for e2e and/or component testing:
const { defineConfig } = require('cypress');
const { collectFailingTests } = require('cypress-plugin-last-failed');

module.exports = defineConfig({
  screenshotOnRunFailure: false,
  env: {
    grepOmitFiltered: true,
    grepFilterSpecs: true,
  },
  e2e: {
    setupNodeEvents(on, config) {
      collectFailingTests(on, config);

      require('@bahmutov/cy-grep/src/plugin')(config);
      return config;
    },
  },
  component: {
    setupNodeEvents(on, config) {
      collectFailingTests(on, config);

      require('@bahmutov/cy-grep/src/plugin')(config);
      return config;
    },
  },
});

👟 Run mode

  1. Run tests using cypress run:
# Example
npx cypress run
  1. If there are failed tests, run the following command from the directory of the project's cypress.config:
npx cypress-plugin-last-failed run

You can also include more cli arguments similar to cypress run, as the command harnesses the power of Cypress module API:

# Example
npx cypress-plugin-last-failed run --e2e --browser chrome

There will be a folder called test-results created in the directory of the cypress.config.

Add rule to gitignore

  • Optional: If you do not want to commit the file storing last failed tests to your remote repository, include a rule within your project's .gitignore file:

# Example

**/test-results

📃 Setting up a npm script

For convenience, you may desire to house the npx command within an npm script in your project's package.json, including any desired cli arguments:

  "scripts": {
    "last-failed": "npx cypress-plugin-last-failed run --e2e --browser electron"
  }

⌛ Open mode

Toggling the filter will run any previously failed tests on the particular spec file.

Failed test toggle

Recommended open mode env variables

  • Recommended: Set two common environment variables tied to the @bahmutov/cy-grep package to enhance the experience utilizing the grep logic within the Cypress Test Runner UI using cypress open:
{
  "env": {
    "grepOmitFiltered": true,
    "grepFilterSpecs": true
  }
}

Note

More information on grepOmitFiltered and grepFilterSpecs can be read within the README for @bahmutov/cy-grep.

Use Required Test Tags Instead Of Skipping Tests

Note

Read more about this topic within a blog post Use Required Test Tags Instead Of Skipping Tests and within the README for @bahmutov/cy-grep.

Normally, any Cypress test or suite of tests marked with a .skip will be shown when running tests or within the Cypress test runner UI.

Since this plugin uses @bahmutov/cy-grep plugin, we can instead designate skipped tests using a required tag:

it('deletes an item', { requiredTags: '@skip' }, () => {
  expect(1).to.equal(2);
});

Now running or opening Cypress in interactive mode, you will not see any tests with requiredTags including @skip (unless setting environment variable grepTags=@skip).

To run just those tests with the required tag @skip in interactive mode:

npx cypress open --env grepTags=@skip

Continuous integration support

An example of utilizing the plugin to re-run only the failed tests from a previous step in CI:

name: test-last-failed-node-script
on:
  push:
    branches:
      - 'main'
  pull_request:
  workflow_dispatch:

jobs:
  node-script:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout 📦
        uses: actions/checkout@v4
      - name: Cypress run 👟
        uses: cypress-io/github-action@v6
      - name: Output the file contents 📝
        if: always()
        run: |
          cat ./test-results/last-run.json
      - name: Custom tests 🧪
        if: always()
        uses: cypress-io/github-action@v6
        with:
          command: npx cypress-plugin-last-failed run
          working-directory: ${{ github.workspace }}

Typescript support

For more information on Typescript support involved with @bahmutov/cy-grep package, refer to it's README.

Contributions

Feel free to open a pull request or drop any feature request or bug in the issues.

Please see more details in the contributing doc.

About

A companion Cypress plugin for `cy-grep` that re-runs the last failed test(s).

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published