question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Comments:10

github_iconTop GitHub Comments

2reactions
SimenBcommented, Nov 28, 2018

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.

if (this._environment) {
  return this._environment;
}

throw new PrettyError();
0reactions
github-actions[bot]commented, May 12, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

You are trying to `import` a file after the Jest environment has ...
Torn down means: Jest already finished running and some part of your code is trying to execute after jest has already finished running...
Read more >
Configuring Jest
This option allows the use of a custom global teardown module which exports an async function that is triggered once after all test...
Read more >
Npm test - M220JS: MongoDB for Javascript Developers
Mine is Wilson Palmeira, I am enrolled in the Course M220JS. When I run npm test -t projection, it fails, below is the...
Read more >
Successfully Throwing Async Errors with the Jest Testing Library
const mockValidateUploadedFile = jest.fn().mockRejectedValue('some product/stores invalid');. Once more, the error was thrown and the test ...
Read more >
Unit Testing with Jest | CS4530, Fall 2022
Setup and Teardown; Mock Testing. Spy; Mock; Stub. Testing Asynchronous Code. Promise; Callbacks. Setting up testing using Jest in VSCode.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found