diff --git a/CHANGELOG.md b/CHANGELOG.md index 35583f3898d3..28bd375931e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - `[jest-fake-timers]` Flush callbacks scheduled with `requestAnimationFrame` every 16ms when using legacy timers. ([#11523](https://github.com/facebook/jest/pull/11567)) +- `[pretty-format]` Use `globalThis` (with polyfill if required) to bring support for esbuild's browser bundling mode ([#11569](https://github.com/facebook/jest/pull/11569) ### Fixes diff --git a/e2e/__tests__/__snapshots__/circusDeclarationErrors.test.ts.snap b/e2e/__tests__/__snapshots__/circusDeclarationErrors.test.ts.snap index e073a8300d49..36c675798378 100644 --- a/e2e/__tests__/__snapshots__/circusDeclarationErrors.test.ts.snap +++ b/e2e/__tests__/__snapshots__/circusDeclarationErrors.test.ts.snap @@ -16,7 +16,7 @@ FAIL __tests__/asyncDefinition.test.js 14 | }); 15 | }); - at eventHandler (../../packages/jest-circus/build/eventHandler.js:146:11) + at eventHandler (../../packages/jest-circus/build/eventHandler.js:190:11) at test (__tests__/asyncDefinition.test.js:12:5) ● Test suite failed to run @@ -31,7 +31,7 @@ FAIL __tests__/asyncDefinition.test.js 15 | }); 16 | - at eventHandler (../../packages/jest-circus/build/eventHandler.js:114:11) + at eventHandler (../../packages/jest-circus/build/eventHandler.js:158:11) at afterAll (__tests__/asyncDefinition.test.js:13:5) ● Test suite failed to run @@ -46,7 +46,7 @@ FAIL __tests__/asyncDefinition.test.js 20 | }); 21 | - at eventHandler (../../packages/jest-circus/build/eventHandler.js:146:11) + at eventHandler (../../packages/jest-circus/build/eventHandler.js:190:11) at test (__tests__/asyncDefinition.test.js:18:3) ● Test suite failed to run @@ -60,6 +60,6 @@ FAIL __tests__/asyncDefinition.test.js 20 | }); 21 | - at eventHandler (../../packages/jest-circus/build/eventHandler.js:114:11) + at eventHandler (../../packages/jest-circus/build/eventHandler.js:158:11) at afterAll (__tests__/asyncDefinition.test.js:19:3) `; diff --git a/scripts/babel-plugin-jest-native-globals.js b/scripts/babel-plugin-jest-native-globals.js index 148a8eeb22e3..32e9cceb65af 100644 --- a/scripts/babel-plugin-jest-native-globals.js +++ b/scripts/babel-plugin-jest-native-globals.js @@ -12,21 +12,99 @@ module.exports = ({template}) => { const promiseDeclaration = template(` + var global = (function() { + if (typeof globalThis !== 'undefined') { + return globalThis; + } else if (typeof global !== 'undefined') { + return global; + } else if (typeof self !== 'undefined') { + return self; + } else if (typeof window !== 'undefined') { + return window; + } else { + return Function('return this')(); + } + }()) var Promise = global[Symbol.for('jest-native-promise')] || global.Promise; `); const symbolDeclaration = template(` + var global = (function() { + if (typeof globalThis !== 'undefined') { + return globalThis; + } else if (typeof global !== 'undefined') { + return global; + } else if (typeof self !== 'undefined') { + return self; + } else if (typeof window !== 'undefined') { + return window; + } else { + return Function('return this')(); + } + }()) var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol; `); const nowDeclaration = template(` + var global = (function() { + if (typeof globalThis !== 'undefined') { + return globalThis; + } else if (typeof global !== 'undefined') { + return global; + } else if (typeof self !== 'undefined') { + return self; + } else if (typeof window !== 'undefined') { + return window; + } else { + return Function('return this')(); + } + }()) var jestNow = global[Symbol.for('jest-native-now')] || global.Date.now; `); const fsReadFileDeclaration = template(` + var global = (function() { + if (typeof globalThis !== 'undefined') { + return globalThis; + } else if (typeof global !== 'undefined') { + return global; + } else if (typeof self !== 'undefined') { + return self; + } else if (typeof window !== 'undefined') { + return window; + } else { + return Function('return this')(); + } + }()) var jestReadFile = global[Symbol.for('jest-native-read-file')] || fs.readFileSync; `); const fsWriteFileDeclaration = template(` + var global = (function() { + if (typeof globalThis !== 'undefined') { + return globalThis; + } else if (typeof global !== 'undefined') { + return global; + } else if (typeof self !== 'undefined') { + return self; + } else if (typeof window !== 'undefined') { + return window; + } else { + return Function('return this')(); + } + }()) var jestWriteFile = global[Symbol.for('jest-native-write-file')] || fs.writeFileSync; `); const fsExistsFileDeclaration = template(` + var global = (function() { + if (typeof globalThis !== 'undefined') { + return globalThis; + } else if (typeof global !== 'undefined') { + return global; + } else if (typeof self !== 'undefined') { + return self; + } else if (typeof window !== 'undefined') { + return window; + } else { + return Function('return this')(); + } + }()) var jestExistsFile = global[Symbol.for('jest-native-exists-file')] || fs.existsSync; `);