npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cypress-plugin-last-failed

v2.0.2

Published

Cypress plugin to rerun last failed tests in cypress run and open mode

Downloads

17,244

Readme

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

Specify a custom test-results directory

There will be a folder called test-results created in the directory of the cypress.config. If you would like to specify a different folder for test results, use the LAST_FAILED_RESULTS_PATH environment variable:

# Example
LAST_FAILED_RESULTS_PATH=cypress/custom-test-results npx cypress-plugin-last-failed run

This environment variable will direct the plugin to store or retrieve the last run's failed test results from the specified folder.

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.