Run failed tests

The @japa/run-failed-tests plugin allows you to run only failed tests on subsequent runs. Here's how the plugin works under the hood.

  • You ran the tests suite, and a couple of tests failed.
  • On the next run, only the failed test will run.
  • If all tests are green, the next run will execute all the tests.

The workflow is usually helpful during refactoring, where you want to just focus on the failing tests only.

The runFailedTests function uses the test title filter to run only the failing tests. The test will be skipped, if you change the title of the failing test before the next run.

Installation and setup

You can install the package from npm registry as follows.

npm i -D @japa/run-failed-tests@1.1.1

And register it as a plugin within the bin/test.js file.

import { runFailedTests } from '@japa/run-failed-tests'
import { configure, processCliArgs } from '@japa/runner'
configure({
...processCliArgs(process.argv.slice(2)),
...{
files: ['tests/**/*.spec.js'],
plugins: [runFailedTests()]
}
})
const { runFailedTests } = require('@japa/run-failed-tests')
const { configure, processCliArgs } = require('@japa/runner')
configure({
...processCliArgs(process.argv.slice(2)),
...{
files: ['tests/**/*.spec.js'],
plugins: [runFailedTests()]
}
})

You can also apply the plugin conditionally. For example, disable it during CI/CD workflow.

const developmentPlugins = []
if (!process.env.CI) {
developmentPlugins.push(runFailedTests())
}
configure({
plugins: [].concat(developmentPlugins)
})

Once this plugin is activated, it will print the following message in the terminal to notify you that only a subset of tests will run.