jest: Tests are run even when beforeAll throws error
When beforeAll throws an error, or returns a Promise that rejects, the tests are still run. I would expect the tests to not even run at all.
See example repo: https://github.com/dirkmc/jest-before-all-error-handling
And the output:
> jest __tests__/test.js
FAIL __tests__/test.js
● test › tests 1 === 1
My error
at __tests__/test.js:5:14
at Object.<anonymous> (__tests__/test.js:4:12)
● test › tests 2 === 2
My error
at __tests__/test.js:5:14
at Object.<anonymous> (__tests__/test.js:4:12)
test
✕ tests 1 === 1 (2ms)
✕ tests 2 === 2 (1ms)
Test Suites: 1 failed, 1 total
Tests: 2 failed, 2 total
Snapshots: 0 total
Time: 0.154s, estimated 1s
Ran all test suites matching "__tests__/test.js".
console.log __tests__/test.js:3
before
console.log __tests__/test.js:12
test 1
console.log __tests__/test.js:17
test 2
console.log __tests__/test.js:9
after
npm ERR! Test failed. See above for more details.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 67
- Comments: 25 (14 by maintainers)
Commits related to this issue
- test: Fail early if the local yarn build is missing (#6351) Previously if the tests were run without first having run `yarn build`, these three test suites would fail with over 1000 lines of unclear... — committed to yarnpkg/yarn by edmorley 6 years ago
- beforeAll fail doesnt prevent test runs https://github.com/facebook/jest/issues/2713 — committed to ebemunk/node-uci by ebemunk 6 years ago
- Switch to Mocha I'm getting bit really hard by these issues: - https://github.com/facebook/jest/issues/2713 - https://github.com/jasmine/jasmine/issues/577 - https://github.com/jasmine/jasmine/issue... — committed to metabolize/apollo-resolver-gcs by paulmelnikow 6 years ago
- Switch to Mocha (#4) I'm getting bit really hard by these issues: - https://github.com/facebook/jest/issues/2713 - https://github.com/jasmine/jasmine/issues/577 - https://github.com/jasmine/jasm... — committed to metabolize/apollo-resolver-gcs by paulmelnikow 6 years ago
- Prefer `jest-circus/runner` so we bail out when `beforeAll` explodes. Per https://github.com/facebook/jest/issues/2713#issuecomment-430932326 — committed to randytarampi/react-native-appium by randytarampi 5 years ago
- Prefer `jest-circus/runner` so we bail out when `beforeAll` explodes. Per https://github.com/facebook/jest/issues/2713#issuecomment-430932326 — committed to randytarampi/react-native-appium by randytarampi 5 years ago
- use jest-circus runner to improve beforeAll/beforeEach failure behavior (#12) Updates jest.config.js to recommend use of the jest-circus runner to enable tests to early-exit when beforeAll/beforeEach... — committed to microsoft/axe-pipelines-samples by dbjorge 5 years ago
Ya, I agree. I have to say this is an astonishingly bad developer experience for cases that rely on complex setup procedures. We have several projects with integration tests that need to add fixtures to a real database, and this issue makes it very difficult to differentiate between setup errors and failed tests, especially since setup runs independently for each test file.
It looks like this may be a better place to direct our comments, though.
This is how jasmine behaves, I’m afraid this is a wontfix for now.
No, I mean from within a test suite, I have a condition which means the rest of the file should not run so I want to skip all remaining tests or terminate the process or something. Like the behaviour which the OP expected when an exception is thrown in
beforeAll
.My specific use case is I’m writing some integration tests which hit the DB but I want don’t want to run them if there’s already data in there.
It turns out that you can expect that behavior if:
jest-circus
npm packageJEST_CIRCUS=1
environment variable when runningjest
I’m surprised and bummed that this behavior is not applied by default in Jest.
If you’re looking for information about when Jest Circus is going to be released, see #6295. 🎪
In what version and PR was it fixed? I’ve tried installing
jest-circus
from npm and settingJEST_CIRCUS=1
, but the behavior is the same: an exception inbeforeAll
does not prevent the tests from running.@rattrayalex-stripe the old test runner for Jest (which is still currently the default) was powered by a jasmine fork called jest-jasmine2. We’ve written our own runner called jest-circus, which we’re migrating everything to
We fixed this issue in the jest-circus runner, and wont be fixing it in the jest-jasmine2 runner (my understanding is that it’s a pretty tough one to fix in the jasmine runner)
Sorry, what’s the relation with jasmine here? For people who are not using jasmine, failures in
beforeAll
et al still seem to require a try/catch + process.exit.Is there any clean way to abort a test run other than
process.exit(1)
?Ah, gotcha, thanks – guess if I’d been more patient on my request for a jest-circus readme I’d have found that out 😅 thanks @rickhanlonii !
PR very much welcome porting the fix from Jasmine: https://github.com/jasmine/jasmine/commit/585287b9d6e03dc546be7269e1ea569f4bd9ca0d
Problem with
process.exit
is that I don’t get any console error messages before exit.jest-circus
is shipping with Jest 27, which is now @next.2
, and the milestone is 80% complete.I couldn’t test because I used
ts-jest
, and upgrading to 27 breaks imports.Using
jest
andjest-circus
26.6.3 (withts-jest
26.5.0), I still experience the issue described by the OP: ifbeforeAll()
throws an error, all test still execute, and the error is reported for each.This is fixed in Jest circus, we won’t be fixing it for Jasmine. If somebody sends a PR that’s fine, but I’m going to close this as we won’t get to it