From 3754dc0aa85c7c320980f598250089a4b043cb09 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 20 Mar 2021 09:17:14 +0100 Subject: [PATCH 1/7] fix(env-jsdom): remove node-specific globals --- CHANGELOG.md | 5 +++-- packages/jest-environment-node/src/index.ts | 4 ++++ packages/jest-util/src/installCommonGlobals.ts | 5 ----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c38f3bf4195..b49fd8b16a93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,7 +38,7 @@ - `[babel-plugin-jest-hoist]` Add `__dirname` and `__filename` to whitelisted globals ([#10903](https://github.com/facebook/jest/pull/10903)) - `[expect]` [**BREAKING**] Revise `expect.not.objectContaining()` to be the inverse of `expect.objectContaining()`, as documented. ([#10708](https://github.com/facebook/jest/pull/10708)) - `[expect]` [**BREAKING**] Make `toContain` more strict with the received type ([#10119](https://github.com/facebook/jest/pull/10119) & [#10929](https://github.com/facebook/jest/pull/10929)) -- `[expect]` [**BREAKING**] `matcherResult` on `JestAssertionError` are now strings rather than functions ([#10989] (https://github.com/facebook/jest/pull/10989)) +- `[expect]` [**BREAKING**] `matcherResult` on `JestAssertionError` are now strings rather than functions ([#10989](https://github.com/facebook/jest/pull/10989)) - `[jest-circus]` Fixed the issue of beforeAll & afterAll hooks getting executed even if it is inside a skipped `describe` block [#10451](https://github.com/facebook/jest/issues/10451) - `[jest-circus]` Fix `testLocation` on Windows when using `test.each` ([#10871](https://github.com/facebook/jest/pull/10871)) - `[jest-cli]` Use testFailureExitCode when bailing from a failed test ([#10958](https://github.com/facebook/jest/pull/10958)) @@ -49,10 +49,11 @@ - `[jest-each]` [**BREAKING**] Ignore excess words in headings ([#8766](https://github.com/facebook/jest/pull/8766)) - `[jest-environment]` [**BREAKING**] Drop support for `runScript` for test environments ([#11155](https://github.com/facebook/jest/pull/11155)) - `[jest-environment-jsdom]` Use inner realm’s `ArrayBuffer` constructor ([#10885](https://github.com/facebook/jest/pull/10885)) +- `[jest-environment-jsdom]` [**BREAKING**] Remove Node globals `setImmediate`, `clearImmediate` and `Buffer` [#11222](https://github.com/facebook/jest/pull/11222) - `[jest-globals]` [**BREAKING**] Disallow return values other than a `Promise` from hooks and tests ([#10512](https://github.com/facebook/jest/pull/10512)) - `[jest-globals]` [**BREAKING**] Disallow mixing a done callback and returning a `Promise` from hooks and tests ([#10512](https://github.com/facebook/jest/pull/10512)) - `[jest-haste-map]` Vendor `NodeWatcher` from `sane` ([#10919](https://github.com/facebook/jest/pull/10919)) -- `[jest-jasmine2]` Fixed the issue of beforeAll & afterAll hooks getting executed even if it is inside a skipped `describe` block when it has child `tests` marked as either `only` or `todo` [#10451](https://github.com/facebook/jest/issues/10451) +- `[jest-jasmine2]` Fixed the issue of `beforeAll` & `afterAll` hooks getting executed even if it is inside a skipped `describe` block when it has child `tests` marked as either `only` or `todo` [#10451](https://github.com/facebook/jest/issues/10451) - `[jest-jasmine2]` Fixed the issues of child `tests` marked with `only` or `todo` getting executed even if it is inside a skipped parent `describe` block [#10451](https://github.com/facebook/jest/issues/10451) - `[jest-reporter]` Handle empty files when reporting code coverage with V8 ([#10819](https://github.com/facebook/jest/pull/10819)) - `[jest-resolve]` Replace read-pkg-up with escalade package ([#10781](https://github.com/facebook/jest/pull/10781)) diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index 6cbb18339880..ebf6d4648e4f 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -36,6 +36,9 @@ class NodeEnvironment implements JestEnvironment { global.clearTimeout = clearTimeout; global.setInterval = setInterval; global.setTimeout = setTimeout; + global.Buffer = Buffer; + global.setImmediate = setImmediate; + global.clearImmediate = clearImmediate; global.ArrayBuffer = ArrayBuffer; // TextEncoder (global or via 'util') references a Uint8Array constructor // different than the global one used by users in tests. This makes sure the @@ -64,6 +67,7 @@ class NodeEnvironment implements JestEnvironment { global.AbortController = AbortController; } installCommonGlobals(global, config.globals); + this.moduleMocker = new ModuleMocker(global); const timerIdToRef = (id: number) => ({ diff --git a/packages/jest-util/src/installCommonGlobals.ts b/packages/jest-util/src/installCommonGlobals.ts index ece4a40649d0..7f3526c8772f 100644 --- a/packages/jest-util/src/installCommonGlobals.ts +++ b/packages/jest-util/src/installCommonGlobals.ts @@ -62,10 +62,5 @@ export default function ( }; }); - // Forward some others (this breaks the sandbox but for now it's OK). - globalObject.Buffer = global.Buffer; - globalObject.setImmediate = global.setImmediate; - globalObject.clearImmediate = global.clearImmediate; - return Object.assign(globalObject, deepCyclicCopy(globals)); } From 1e15a27553ad6bc8a039f566ca1e0772cdcf1ce4 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 20 Mar 2021 09:59:36 +0100 Subject: [PATCH 2/7] use browser-version of source-map-support --- .../src/deepCyclicCopyReplaceable.ts | 7 ++++-- packages/jest-runner/src/runTest.ts | 23 ++++++++++++++----- packages/jest-runtime/src/index.ts | 13 +++++++---- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts b/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts index 8928927b8d2d..9c3dd0ac9d26 100644 --- a/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts +++ b/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts @@ -7,9 +7,8 @@ import {plugins} from 'pretty-format'; -const builtInObject = [ +const builtInObject: Array = [ Array, - Buffer, Date, Float32Array, Float64Array, @@ -25,6 +24,10 @@ const builtInObject = [ Uint8ClampedArray, ]; +if (typeof Buffer !== 'undefined') { + builtInObject.push(Buffer); +} + const isBuiltInObject = (object: any) => builtInObject.includes(object.constructor); diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index 4e8237c283ec..1581461e7894 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -206,13 +206,24 @@ async function runTestInternal( }, }; + const isBrowserEnv = typeof environment.global?.document !== 'undefined'; + // For tests - runtime - .requireInternalModule( - require.resolve('source-map-support'), - 'source-map-support', - ) - .install(sourcemapOptions); + if (isBrowserEnv) { + runtime.requireInternalModule( + require.resolve('source-map-support/browser-source-map-support'), + ); + const sourceMapSupport = environment.global + .sourceMapSupport as typeof sourcemapSupport; + + sourceMapSupport.install({...sourcemapOptions, environment: 'browser'}); + } else { + runtime + .requireInternalModule( + require.resolve('source-map-support'), + ) + .install(sourcemapOptions); + } // For runtime errors sourcemapSupport.install(sourcemapOptions); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 04521637efc0..5e2b20cf20cc 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -652,12 +652,10 @@ export default class Runtime { if (options?.isInternalModule) { moduleRegistry = this._internalModuleRegistry; + } else if (this._isolatedModuleRegistry) { + moduleRegistry = this._isolatedModuleRegistry; } else { - if (this._isolatedModuleRegistry) { - moduleRegistry = this._isolatedModuleRegistry; - } else { - moduleRegistry = this._moduleRegistry; - } + moduleRegistry = this._moduleRegistry; } const module = moduleRegistry.get(modulePath); @@ -1823,6 +1821,11 @@ export default class Runtime { throw moduleNotFoundError; } + e.message = `Jest: Got error evaluating ${path.relative( + this._config.rootDir, + module.filename, + )}.\n${e.message}`; + throw e; } From 0a0e64984d0e7709e91e243fc8e93e4eb0cca126 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 20 Mar 2021 10:26:21 +0100 Subject: [PATCH 3/7] rollback runtime changes --- packages/jest-runtime/src/index.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 5e2b20cf20cc..04521637efc0 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -652,10 +652,12 @@ export default class Runtime { if (options?.isInternalModule) { moduleRegistry = this._internalModuleRegistry; - } else if (this._isolatedModuleRegistry) { - moduleRegistry = this._isolatedModuleRegistry; } else { - moduleRegistry = this._moduleRegistry; + if (this._isolatedModuleRegistry) { + moduleRegistry = this._isolatedModuleRegistry; + } else { + moduleRegistry = this._moduleRegistry; + } } const module = moduleRegistry.get(modulePath); @@ -1821,11 +1823,6 @@ export default class Runtime { throw moduleNotFoundError; } - e.message = `Jest: Got error evaluating ${path.relative( - this._config.rootDir, - module.filename, - )}.\n${e.message}`; - throw e; } From e9873b0ed360648e6ebb8612a59045211d37c82e Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 20 Mar 2021 10:45:00 +0100 Subject: [PATCH 4/7] fix test --- e2e/env-test/__tests__/equivalent.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/e2e/env-test/__tests__/equivalent.test.js b/e2e/env-test/__tests__/equivalent.test.js index 7bc5ed20dd0b..9e21e72724c6 100644 --- a/e2e/env-test/__tests__/equivalent.test.js +++ b/e2e/env-test/__tests__/equivalent.test.js @@ -10,7 +10,9 @@ const {isArrayBuffer} = require('util').types; const isJSDOM = typeof window !== 'undefined' && typeof document !== 'undefined'; -test('Buffer', () => { +const skipTestJSDOM = isJSDOM ? test.skip : test; + +skipTestJSDOM('Buffer', () => { const bufFromArray = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); expect(isArrayBuffer(bufFromArray.buffer)).toBeTruthy(); const bufFromArrayBuffer = Buffer.from(new ArrayBuffer(6)); From e1e26f87609ebfd3492b110dc2e8a7a23c9847e0 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 25 Mar 2021 19:21:16 +0100 Subject: [PATCH 5/7] bump testing library --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index a0d3a5e91284..5711142a16dd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4287,8 +4287,8 @@ __metadata: linkType: hard "@testing-library/dom@npm:^7.28.1": - version: 7.29.6 - resolution: "@testing-library/dom@npm:7.29.6" + version: 7.30.1 + resolution: "@testing-library/dom@npm:7.30.1" dependencies: "@babel/code-frame": ^7.10.4 "@babel/runtime": ^7.12.5 @@ -4298,7 +4298,7 @@ __metadata: dom-accessibility-api: ^0.5.4 lz-string: ^1.4.4 pretty-format: ^26.6.2 - checksum: 6102dabce8526f9ccfd4c4e08d60d1c7f2bd125482587cb4664cd652b9c00dd430faf5e08701513f3d5bfa90e89fdcca26313b9ffe644fd1936bbf449ac62a14 + checksum: a886bdb20955e5f029fa10932184128dd701dac866dc8016e66f339d89ab5b63185c3584ea5eb2a5ffbcb779bad6952f1f0933cc47f6772450211cd85b55861e languageName: node linkType: hard From 66c0e034a3a1a9fb3ae4d85665b5758608f5c0f0 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 25 Mar 2021 19:23:04 +0100 Subject: [PATCH 6/7] keep Buffer around --- packages/jest-environment-jsdom/src/index.ts | 3 +++ packages/jest-runner/src/runTest.ts | 23 +++++--------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index 9e795c520d4d..421370bcf628 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -53,6 +53,9 @@ class JSDOMEnvironment implements JestEnvironment { this.global.Error.stackTraceLimit = 100; installCommonGlobals(global as any, config.globals); + // TODO: remove this ASAP, but it currntly causes tests to run really slow + global.Buffer = Buffer; + // Report uncaught errors. this.errorEventListener = event => { if (userErrorListenerCount === 0 && event.error) { diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index 1581461e7894..4e8237c283ec 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -206,24 +206,13 @@ async function runTestInternal( }, }; - const isBrowserEnv = typeof environment.global?.document !== 'undefined'; - // For tests - if (isBrowserEnv) { - runtime.requireInternalModule( - require.resolve('source-map-support/browser-source-map-support'), - ); - const sourceMapSupport = environment.global - .sourceMapSupport as typeof sourcemapSupport; - - sourceMapSupport.install({...sourcemapOptions, environment: 'browser'}); - } else { - runtime - .requireInternalModule( - require.resolve('source-map-support'), - ) - .install(sourcemapOptions); - } + runtime + .requireInternalModule( + require.resolve('source-map-support'), + 'source-map-support', + ) + .install(sourcemapOptions); // For runtime errors sourcemapSupport.install(sourcemapOptions); From 633ef87e3839b0690e8929664d9969bbac5f6dfa Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 25 Mar 2021 19:27:16 +0100 Subject: [PATCH 7/7] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b49fd8b16a93..d00f74cc0afb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,7 +49,7 @@ - `[jest-each]` [**BREAKING**] Ignore excess words in headings ([#8766](https://github.com/facebook/jest/pull/8766)) - `[jest-environment]` [**BREAKING**] Drop support for `runScript` for test environments ([#11155](https://github.com/facebook/jest/pull/11155)) - `[jest-environment-jsdom]` Use inner realm’s `ArrayBuffer` constructor ([#10885](https://github.com/facebook/jest/pull/10885)) -- `[jest-environment-jsdom]` [**BREAKING**] Remove Node globals `setImmediate`, `clearImmediate` and `Buffer` [#11222](https://github.com/facebook/jest/pull/11222) +- `[jest-environment-jsdom]` [**BREAKING**] Remove Node globals `setImmediate` and `clearImmediate` [#11222](https://github.com/facebook/jest/pull/11222) - `[jest-globals]` [**BREAKING**] Disallow return values other than a `Promise` from hooks and tests ([#10512](https://github.com/facebook/jest/pull/10512)) - `[jest-globals]` [**BREAKING**] Disallow mixing a done callback and returning a `Promise` from hooks and tests ([#10512](https://github.com/facebook/jest/pull/10512)) - `[jest-haste-map]` Vendor `NodeWatcher` from `sane` ([#10919](https://github.com/facebook/jest/pull/10919))