Better error messages when the jest environment is used after teardown by async code
See original GitHub issue🚀 Feature Proposal
Provide better errors when async code uses the environment after the test suite finished executing. Something like “This method can’t be called because the test suite has finished running. This can happen if you forget to return a promise from a test method that performs asynchronous operations.”
Motivation
I just spent a while debugging the following issue:
Cannot read property 'runAllTimers' of null
TypeError: Cannot read property 'runAllTimers' of null
at Object.runAllTimers (/Users/bernie/Documents/GitHub/amara-checkout/node_modules/jest-runtime/build/index.js:969:56)
Caused by a jest.runAllTimers();
in my code. Turns out, this method calls this._environment.fakeTimers.runAllTimers()
, and _environment.fakeTimers
was null because the environment had been torn down, because the test suite had finished executing.
The root cause was that I’d omitted an await
before an async method in a test, and the test method itself was not async.
It would be great to be able to offer an error message that pointed the user to the source of the issue.
Example
n/a
Pitch
Why does this feature belong in the Jest core platform?
It’s a tweak to help people use existing functionality correctly.
Issue Analytics
- State:
- Created 5 years ago
- Comments:10
Top GitHub Comments
I can give a quick start, though.
The relevant code lives here: https://github.com/facebook/jest/blob/b502c07e4b5f24f491f6bf840bdf298a979ec0e7/packages/jest-runtime/src/index.js#L922-L963
WHat you need to do is add a guard around every access on
this._environment
that throws an explicit error that the environment has already been torn down. You can look at how I solved it in #5888 to get an idea how the error message could look like.Should probably have a
_getTestEnvironemt
function that does e.g.This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.