Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use globalThis (with fallback if needed) instead of global to enable esbuild support #11569

Merged
merged 2 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
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