jest: [Bug]: Jest 30 Beta causes TypeError: Class constructor AsyncLocalStorage cannot be invoked without 'new'

Version

30.0.0-beta.5

Steps to reproduce

I am still working on getting a useful repro case. In the meantime, here are some details about what my tests look like.

I am using Jest to test a next.js API using a custom express server. I have a withServer helper that I use in these API tests that starts the express/next.js server and gets it listening for requests in a beforeAll. Then in an afterAll, the server is closed.

The tests are using the supertest package to make the requests against the API.

When I run one of these tests on its own, it works fine. But if I run more than one test that sets up this server, I end up with problems in the later tests. So one key to reproducing this is to have multiple test files that set up the server and then to run them sequentially.

Expected behavior

I expect my tests to pass.

Actual behavior

The second test file that uses our withServer helper ends up logging TypeError: Class constructor AsyncLocalStorage cannot be invoked without 'new' repeatedly and then times out after 10 seconds. At the end of the tests, this is logged:

Jest did not exit one second after the test run has completed.

'This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

Running with --detectOpenHandles doesn’t detect any open handles.

Additional context

Since this happens only when there is more than one test file and since it does not happen with the alpha releases (similar qualities as https://github.com/jestjs/jest/issues/15632 for instance), it seems like it could possibly be related to https://github.com/jestjs/jest/pull/15215 though it seems like it needs a different fix than https://github.com/jestjs/jest/pull/15636.

Environment

System:
    OS: macOS 15.4.1
    CPU: (8) arm64 Apple M3
  Binaries:
    Node: 22.16.0 - ~/.local/share/mise/installs/node/22.16.0/bin/node
    Yarn: 1.22.22 - ~/.local/share/mise/installs/yarn/1.22.22/bin/yarn
    npm: 10.9.2 - ~/.local/share/mise/installs/node/22.16.0/bin/npm
  npmPackages:
    jest: ^30.0.0-beta.5 => 30.0.0-beta.5

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 16 (15 by maintainers)

Commits related to this issue

Most upvoted comments

I think I have a way to overcome this issue. I’ll open a PR over the weekend.

I may have found the issue: https://github.com/vercel/next.js/blob/main/packages/next/src/server/node-environment.ts#L5

Adding this code to two test files:

import { AsyncLocalStorage } from 'async_hooks';

test('foo', () => {
    (globalThis as any).AsyncLocalStorage = require('async_hooks');
    console.log({ als: AsyncLocalStorage });
    console.log({ alsC: AsyncLocalStorage?.constructor });
  });

Causes AsyncLocalStorage to be undefined in the second test.

I think the issue stems from the protection code not applying to an object recursively. Meaning, if a nested object of a core module is set on the global (like next’s code does), it won’t be protected.

My current PR did fix some use cases, but not all of them I’ll need to adjust the protection code to be recursive, and that would likely be a more robust solution.

I will update the PR in the upcoming days.