Skip to content

Commit

Permalink
Use globalThis (with fallback if needed) instead of global to ena…
Browse files Browse the repository at this point in the history
…ble esbuild support (#11569)
  • Loading branch information
JakeChampion committed Jun 15, 2021
1 parent 012f9f0 commit 7be63ef
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
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
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 7be63ef

Please sign in to comment.