It can be helpful to stop large test suites as soon as an error occurs. Stopping the test run after the first failed test saves you time, especially during development.
Jest comes with a built-in feature to fail early if one test fails. This tutorial shows you how to configure Jest to bail out early!
Jest Series Overview
- Fail Early (And Stop Testing If One Test Fails)
Jest Bail — Stop After the First Failing Test
By default, Jest runs all your tests and shows their result in the console. Yet, Jest comes with a bail
option allowing you to stop running tests after n
failed tests.
You may configure the bail
option to a boolean value or a number. Using a boolean for bail
will either stop or not stop after the first failed test. In contrast, you can use a number to be specific about the number of tests before stopping the test run.
Jest Configuration File
Configure either a number or a boolean value in your Jest configuration file to stop running the test suite:
jest.config.js
'use strict'
module.exports = {
// stop after first failing test
bail: true
// stop after 3 failed tests
bail: 3
}
Jest CLI
You can also use the bail
configuration from the command line. For example, you may pass the --bail
flag to stop testing after the first failed test. You may also pass a number n
of failed test as an argument to --bail <n>
to stop after n tests:
package.json
{
"scripts": {
"start": "node server.js",
"test": "cross-env NODE_ENV=test jest --bail"
"test-and-fail-after-3": "cross-env NODE_ENV=test jest --bail 3"
}
}
Enjoy testing!