Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: Support older test environments whose moduleMocker does not implement clearMocksOnScope#16169

Merged
SimenB merged 8 commits into
jestjs:mainfrom
jmuransky:jest-environemnt-jsdom-version-warning
May 12, 2026
Merged

fix: Support older test environments whose moduleMocker does not implement clearMocksOnScope#16169
SimenB merged 8 commits into
jestjs:mainfrom
jmuransky:jest-environemnt-jsdom-version-warning

Conversation

@jmuransky
Copy link
Copy Markdown
Contributor

Summary

Provides more descriptive error message so, that people trying to use new Jest will not spend too much time trying to figure out what went wrong.

Covers issue #16168

Test plan

I added tests covering the functionality.

@netlify
Copy link
Copy Markdown

netlify Bot commented May 12, 2026

Deploy Preview for jestjs ready!

Name Link
🔨 Latest commit 204f8ff
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/6a0310cdf96279000800a945
😎 Deploy Preview https://deploy-preview-16169--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 12, 2026

Open in StackBlitz

babel-jest

npm i https://pkg.pr.new/babel-jest@16169

babel-plugin-jest-hoist

npm i https://pkg.pr.new/babel-plugin-jest-hoist@16169

babel-preset-jest

npm i https://pkg.pr.new/babel-preset-jest@16169

create-jest

npm i https://pkg.pr.new/create-jest@16169

@jest/diff-sequences

npm i https://pkg.pr.new/@jest/diff-sequences@16169

expect

npm i https://pkg.pr.new/expect@16169

@jest/expect-utils

npm i https://pkg.pr.new/@jest/expect-utils@16169

jest

npm i https://pkg.pr.new/jest@16169

jest-changed-files

npm i https://pkg.pr.new/jest-changed-files@16169

jest-circus

npm i https://pkg.pr.new/jest-circus@16169

jest-cli

npm i https://pkg.pr.new/jest-cli@16169

jest-config

npm i https://pkg.pr.new/jest-config@16169

@jest/console

npm i https://pkg.pr.new/@jest/console@16169

@jest/core

npm i https://pkg.pr.new/@jest/core@16169

@jest/create-cache-key-function

npm i https://pkg.pr.new/@jest/create-cache-key-function@16169

jest-diff

npm i https://pkg.pr.new/jest-diff@16169

jest-docblock

npm i https://pkg.pr.new/jest-docblock@16169

jest-each

npm i https://pkg.pr.new/jest-each@16169

@jest/environment

npm i https://pkg.pr.new/@jest/environment@16169

jest-environment-jsdom

npm i https://pkg.pr.new/jest-environment-jsdom@16169

@jest/environment-jsdom-abstract

npm i https://pkg.pr.new/@jest/environment-jsdom-abstract@16169

jest-environment-node

npm i https://pkg.pr.new/jest-environment-node@16169

@jest/expect

npm i https://pkg.pr.new/@jest/expect@16169

@jest/fake-timers

npm i https://pkg.pr.new/@jest/fake-timers@16169

@jest/get-type

npm i https://pkg.pr.new/@jest/get-type@16169

@jest/globals

npm i https://pkg.pr.new/@jest/globals@16169

jest-haste-map

npm i https://pkg.pr.new/jest-haste-map@16169

jest-jasmine2

npm i https://pkg.pr.new/jest-jasmine2@16169

jest-leak-detector

npm i https://pkg.pr.new/jest-leak-detector@16169

jest-matcher-utils

npm i https://pkg.pr.new/jest-matcher-utils@16169

jest-message-util

npm i https://pkg.pr.new/jest-message-util@16169

jest-mock

npm i https://pkg.pr.new/jest-mock@16169

@jest/pattern

npm i https://pkg.pr.new/@jest/pattern@16169

jest-phabricator

npm i https://pkg.pr.new/jest-phabricator@16169

jest-regex-util

npm i https://pkg.pr.new/jest-regex-util@16169

@jest/reporters

npm i https://pkg.pr.new/@jest/reporters@16169

jest-resolve

npm i https://pkg.pr.new/jest-resolve@16169

jest-resolve-dependencies

npm i https://pkg.pr.new/jest-resolve-dependencies@16169

jest-runner

npm i https://pkg.pr.new/jest-runner@16169

jest-runtime

npm i https://pkg.pr.new/jest-runtime@16169

@jest/schemas

npm i https://pkg.pr.new/@jest/schemas@16169

jest-snapshot

npm i https://pkg.pr.new/jest-snapshot@16169

@jest/snapshot-utils

npm i https://pkg.pr.new/@jest/snapshot-utils@16169

@jest/source-map

npm i https://pkg.pr.new/@jest/source-map@16169

@jest/test-result

npm i https://pkg.pr.new/@jest/test-result@16169

@jest/test-sequencer

npm i https://pkg.pr.new/@jest/test-sequencer@16169

@jest/transform

npm i https://pkg.pr.new/@jest/transform@16169

@jest/types

npm i https://pkg.pr.new/@jest/types@16169

jest-util

npm i https://pkg.pr.new/jest-util@16169

jest-validate

npm i https://pkg.pr.new/jest-validate@16169

jest-watcher

npm i https://pkg.pr.new/jest-watcher@16169

jest-worker

npm i https://pkg.pr.new/jest-worker@16169

pretty-format

npm i https://pkg.pr.new/pretty-format@16169

commit: ecbae8c

Comment thread packages/jest-runtime/src/index.ts Outdated
'`moduleMocker` must be set on an environment when created',
);
this._moduleMocker = this._environment.moduleMocker;
if (typeof this._moduleMocker.clearMocksOnScope !== 'function') {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should explode here. We should just skip the clearMocksOnScope call

diff --git i/packages/jest-runtime/src/index.ts w/packages/jest-runtime/src/index.ts
index e85d58ed01..a67a073966 100644
--- i/packages/jest-runtime/src/index.ts
+++ w/packages/jest-runtime/src/index.ts
@@ -555,7 +555,7 @@ export default class Runtime {
 
     if (this._environment) {
       if (this._environment.global) {
-        this._moduleMocker.clearMocksOnScope(this._environment.global);
+        this._moduleMocker.clearMocksOnScope?.(this._environment.global);
       }
 
       if (this._environment.fakeTimers) {

it's a nice extra feature, but not require at all for the runtime to function

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right. I didn't have enough context knowledge to know that this thing is not actually required. I think there are other cases that basically die on the same thing (not sure though) - so I'll make sure to make this optional in every case and revert the error message.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the end your change request was enough and there was no other case :)

Copy link
Copy Markdown
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@SimenB SimenB changed the title Jest environemnt jsdom version warning fix: Support older test environments whose moduleMocker does not implement clearMocksOnScope May 12, 2026
@SimenB SimenB merged commit 28246a4 into jestjs:main May 12, 2026
8 of 9 checks passed
@dananaprey
Copy link
Copy Markdown

Hi! When version with this fix will be published?

Faced same issue

weirdwater added a commit to mendix/widgets-tools that referenced this pull request May 19, 2026
jest 30.4.0 introduced clearMocksOnScope(), but did not deal properly
with environments missing that method. The react-native jest environment
seems to be missing it, causing the unit tests to fail.

This is fixed by [jest#16169](jestjs/jest#16169)
but has not been released yet. Once merged, the range can be `^30.4.0`.
weirdwater added a commit to mendix/widgets-tools that referenced this pull request May 20, 2026
jest 30.4.0 introduced clearMocksOnScope(), but did not deal properly
with environments missing that method. The react-native jest environment
seems to be missing it, causing the unit tests to fail.

This is fixed by [jest#16169](jestjs/jest#16169)
but has not been released yet. Once merged, the range can be `^30.4.0`.
colinrotherham added a commit to nhsuk/nhsuk-frontend that referenced this pull request May 28, 2026
colinrotherham added a commit to nhsuk/nhsuk-frontend that referenced this pull request May 28, 2026
colinrotherham added a commit to nhsuk/nhsuk-frontend that referenced this pull request May 28, 2026
colinrotherham added a commit to nhsuk/nhsuk-frontend that referenced this pull request May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants