Skip to content

Commit

Permalink
use globalThis (inline polyfill) instead of global
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
JakeChampion committed Jun 14, 2021
1 parent 91b94c8 commit 6de8504
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
`;
78 changes: 78 additions & 0 deletions scripts/babel-plugin-jest-native-globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
`);

Expand Down

0 comments on commit 6de8504

Please sign in to comment.