From 6de8504da590ab4c0c42eac418b344a0014330e8 Mon Sep 17 00:00:00 2001 From: Jake Champion Date: Mon, 14 Jun 2021 22:54:41 +0100 Subject: [PATCH] use globalThis (inline polyfill) instead of global By using `global`, the package `pretty-format` can not be bundled for the browser by `esbuild` because `global` is not defined in a browser environment. Switching to `globalThis`, with a polyfill for older environments, allows `pretty-format` (and hopefully other packages) to be bundled by `esbuild --platform browser`. --- .../circusDeclarationErrors.test.ts.snap | 8 +- scripts/babel-plugin-jest-native-globals.js | 78 +++++++++++++++++++ 2 files changed, 82 insertions(+), 4 deletions(-) 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; `);