Skip to content

Commit 7be63ef

Browse files
authored
Use globalThis (with fallback if needed) instead of global to enable esbuild support (#11569)
1 parent 012f9f0 commit 7be63ef

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Features
44

55
- `[jest-fake-timers]` Flush callbacks scheduled with `requestAnimationFrame` every 16ms when using legacy timers. ([#11523](https://github.com/facebook/jest/pull/11567))
6+
- `[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)
67

78
### Fixes
89

e2e/__tests__/__snapshots__/circusDeclarationErrors.test.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ FAIL __tests__/asyncDefinition.test.js
1616
14 | });
1717
15 | });
1818
19-
at eventHandler (../../packages/jest-circus/build/eventHandler.js:146:11)
19+
at eventHandler (../../packages/jest-circus/build/eventHandler.js:190:11)
2020
at test (__tests__/asyncDefinition.test.js:12:5)
2121
2222
● Test suite failed to run
@@ -31,7 +31,7 @@ FAIL __tests__/asyncDefinition.test.js
3131
15 | });
3232
16 |
3333
34-
at eventHandler (../../packages/jest-circus/build/eventHandler.js:114:11)
34+
at eventHandler (../../packages/jest-circus/build/eventHandler.js:158:11)
3535
at afterAll (__tests__/asyncDefinition.test.js:13:5)
3636
3737
● Test suite failed to run
@@ -46,7 +46,7 @@ FAIL __tests__/asyncDefinition.test.js
4646
20 | });
4747
21 |
4848
49-
at eventHandler (../../packages/jest-circus/build/eventHandler.js:146:11)
49+
at eventHandler (../../packages/jest-circus/build/eventHandler.js:190:11)
5050
at test (__tests__/asyncDefinition.test.js:18:3)
5151
5252
● Test suite failed to run
@@ -60,6 +60,6 @@ FAIL __tests__/asyncDefinition.test.js
6060
20 | });
6161
21 |
6262
63-
at eventHandler (../../packages/jest-circus/build/eventHandler.js:114:11)
63+
at eventHandler (../../packages/jest-circus/build/eventHandler.js:158:11)
6464
at afterAll (__tests__/asyncDefinition.test.js:19:3)
6565
`;

scripts/babel-plugin-jest-native-globals.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,99 @@
1212

1313
module.exports = ({template}) => {
1414
const promiseDeclaration = template(`
15+
var global = (function() {
16+
if (typeof globalThis !== 'undefined') {
17+
return globalThis;
18+
} else if (typeof global !== 'undefined') {
19+
return global;
20+
} else if (typeof self !== 'undefined') {
21+
return self;
22+
} else if (typeof window !== 'undefined') {
23+
return window;
24+
} else {
25+
return Function('return this')();
26+
}
27+
}())
1528
var Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
1629
`);
1730
const symbolDeclaration = template(`
31+
var global = (function() {
32+
if (typeof globalThis !== 'undefined') {
33+
return globalThis;
34+
} else if (typeof global !== 'undefined') {
35+
return global;
36+
} else if (typeof self !== 'undefined') {
37+
return self;
38+
} else if (typeof window !== 'undefined') {
39+
return window;
40+
} else {
41+
return Function('return this')();
42+
}
43+
}())
1844
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
1945
`);
2046
const nowDeclaration = template(`
47+
var global = (function() {
48+
if (typeof globalThis !== 'undefined') {
49+
return globalThis;
50+
} else if (typeof global !== 'undefined') {
51+
return global;
52+
} else if (typeof self !== 'undefined') {
53+
return self;
54+
} else if (typeof window !== 'undefined') {
55+
return window;
56+
} else {
57+
return Function('return this')();
58+
}
59+
}())
2160
var jestNow = global[Symbol.for('jest-native-now')] || global.Date.now;
2261
`);
2362
const fsReadFileDeclaration = template(`
63+
var global = (function() {
64+
if (typeof globalThis !== 'undefined') {
65+
return globalThis;
66+
} else if (typeof global !== 'undefined') {
67+
return global;
68+
} else if (typeof self !== 'undefined') {
69+
return self;
70+
} else if (typeof window !== 'undefined') {
71+
return window;
72+
} else {
73+
return Function('return this')();
74+
}
75+
}())
2476
var jestReadFile = global[Symbol.for('jest-native-read-file')] || fs.readFileSync;
2577
`);
2678
const fsWriteFileDeclaration = template(`
79+
var global = (function() {
80+
if (typeof globalThis !== 'undefined') {
81+
return globalThis;
82+
} else if (typeof global !== 'undefined') {
83+
return global;
84+
} else if (typeof self !== 'undefined') {
85+
return self;
86+
} else if (typeof window !== 'undefined') {
87+
return window;
88+
} else {
89+
return Function('return this')();
90+
}
91+
}())
2792
var jestWriteFile = global[Symbol.for('jest-native-write-file')] || fs.writeFileSync;
2893
`);
2994
const fsExistsFileDeclaration = template(`
95+
var global = (function() {
96+
if (typeof globalThis !== 'undefined') {
97+
return globalThis;
98+
} else if (typeof global !== 'undefined') {
99+
return global;
100+
} else if (typeof self !== 'undefined') {
101+
return self;
102+
} else if (typeof window !== 'undefined') {
103+
return window;
104+
} else {
105+
return Function('return this')();
106+
}
107+
}())
30108
var jestExistsFile = global[Symbol.for('jest-native-exists-file')] || fs.existsSync;
31109
`);
32110

0 commit comments

Comments
 (0)