Skip to content

Commit c2ef4df

Browse files
authored
fix: make sure atob and btoa are writeable (#14446)
1 parent 834d9d6 commit c2ef4df

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
### Fixes
66

77
- `[jest-core]` Fix typo in `scheduleAndRun` performance marker ([#14434](https://github.com/jestjs/jest/pull/14434))
8+
- `[jest-environment-node]` Make sure `atob` and `btoa` are writeable in Node 20 ([#14446](https://github.com/jestjs/jest/pull/14446))
89
- `[jest-worker]` Additional error wrapper for `parentPort.postMessage` to fix unhandled `DataCloneError`. ([#14437](https://github.com/jestjs/jest/pull/14437))
910

1011
### Chore & Maintenance
1112

1213
## 29.6.3
1314

15+
### Fixes
16+
1417
- `[expect, @jest/expect-utils]` `ObjectContaining` support `sumbol` as key ([#14414](https://github.com/jestjs/jest/pull/14414))
1518
- `[expect]` Remove `@types/node` from dependencies ([#14385](https://github.com/jestjs/jest/pull/14385))
1619
- `[jest-core]` Use workers in watch mode by default to avoid crashes ([#14059](https://github.com/facebook/jest/pull/14059) & [#14085](https://github.com/facebook/jest/pull/14085)).

e2e/override-globals/__tests__/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,13 @@ describe('parent', () => {
2727
}, 10);
2828
});
2929
});
30+
31+
it('can override atob and btoa', () => {
32+
// eslint-disable-next-line no-restricted-globals
33+
global.atob = () => 'hello';
34+
// eslint-disable-next-line no-restricted-globals
35+
global.btoa = () => 'there';
36+
37+
expect(`${atob()} ${btoa()}`).toBe('hello there');
38+
});
3039
});

packages/jest-environment-node/src/index.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,19 @@ const denyList = new Set([
2727
'GLOBAL',
2828
'root',
2929
'global',
30+
'globalThis',
3031
'Buffer',
3132
'ArrayBuffer',
3233
'Uint8Array',
3334
// if env is loaded within a jest test
3435
'jest-symbol-do-not-touch',
3536
]);
3637

38+
type GlobalProperties = Array<keyof typeof globalThis>;
39+
3740
const nodeGlobals = new Map(
38-
Object.getOwnPropertyNames(globalThis)
39-
.filter(global => !denyList.has(global))
41+
(Object.getOwnPropertyNames(globalThis) as GlobalProperties)
42+
.filter(global => !denyList.has(global as string))
4043
.map(nodeGlobalsKey => {
4144
const descriptor = Object.getOwnPropertyDescriptor(
4245
globalThis,
@@ -76,35 +79,34 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
7679
) as Global.Global;
7780
this.global = global;
7881

79-
const contextGlobals = new Set(Object.getOwnPropertyNames(global));
82+
const contextGlobals = new Set(
83+
Object.getOwnPropertyNames(global) as GlobalProperties,
84+
);
8085
for (const [nodeGlobalsKey, descriptor] of nodeGlobals) {
8186
if (!contextGlobals.has(nodeGlobalsKey)) {
8287
if (descriptor.configurable) {
8388
Object.defineProperty(global, nodeGlobalsKey, {
8489
configurable: true,
8590
enumerable: descriptor.enumerable,
8691
get() {
87-
// @ts-expect-error: no index signature
88-
const val = globalThis[nodeGlobalsKey] as unknown;
92+
const value = globalThis[nodeGlobalsKey];
8993

9094
// override lazy getter
9195
Object.defineProperty(global, nodeGlobalsKey, {
9296
configurable: true,
9397
enumerable: descriptor.enumerable,
94-
value: val,
95-
writable:
96-
descriptor.writable === true ||
97-
// Node 19 makes performance non-readable. This is probably not the correct solution.
98-
nodeGlobalsKey === 'performance',
98+
value,
99+
writable: true,
99100
});
100-
return val;
101+
102+
return value;
101103
},
102-
set(val: unknown) {
104+
set(value: unknown) {
103105
// override lazy getter
104106
Object.defineProperty(global, nodeGlobalsKey, {
105107
configurable: true,
106108
enumerable: descriptor.enumerable,
107-
value: val,
109+
value,
108110
writable: true,
109111
});
110112
},

0 commit comments

Comments
 (0)