Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
always writeable
  • Loading branch information
SimenB committed Aug 24, 2023
commit 4f9b3575694063d2925296e8078c757b7d92f3e9
7 changes: 7 additions & 0 deletions e2e/override-globals/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ describe('parent', () => {
}, 10);
});
});

it('can override atob and btoa', () => {
global.atob = () => 'hello';
global.btoa = () => 'there';

expect(`${atob()} ${btoa()}`).toBe('hello there');
});
});
37 changes: 10 additions & 27 deletions packages/jest-environment-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,41 +90,24 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
configurable: true,
enumerable: descriptor.enumerable,
get() {
const attributes: PropertyDescriptor = {
configurable: true,
};

if ('value' in descriptor) {
attributes.value = descriptor.value;
}

if ('writable' in descriptor) {
attributes.writable = descriptor.writable;
}

if ('enumerable' in descriptor) {
attributes.enumerable = descriptor.enumerable;
}

if ('get' in descriptor) {
attributes.get = descriptor.get;
}

if ('set' in descriptor) {
attributes.set = descriptor.set;
}
const value = globalThis[nodeGlobalsKey];

// override lazy getter
Object.defineProperty(global, nodeGlobalsKey, attributes);
Object.defineProperty(global, nodeGlobalsKey, {
configurable: true,
enumerable: descriptor.enumerable,
value,
writable: true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the only code change - if people wanna override globals, then sure 🤷

});

return globalThis[nodeGlobalsKey];
return value;
},
set(val: unknown) {
set(value: unknown) {
// override lazy getter
Object.defineProperty(global, nodeGlobalsKey, {
configurable: true,
enumerable: descriptor.enumerable,
value: val,
value,
writable: true,
});
},
Expand Down