Skip to content

Commit

Permalink
Use Object.defineProperties for Jest globals
Browse files Browse the repository at this point in the history
This is a backport of commit cf631ad from upstream.
It's needed when running Jest under newer versions of Node,
including for example Node v18.16.1.
  • Loading branch information
gnprice authored and chrisbobbe committed Sep 21, 2024
1 parent d94fa14 commit d607b0b
Showing 1 changed file with 46 additions and 16 deletions.
62 changes: 46 additions & 16 deletions jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,52 @@ const mockComponent = jest.requireActual('./mockComponent');
jest.requireActual('@react-native/polyfills/Object.es8');
jest.requireActual('@react-native/polyfills/error-guard');

global.__DEV__ = true;

global.performance = {
now: jest.fn(Date.now),
};

global.Promise = jest.requireActual('promise');
global.regeneratorRuntime = jest.requireActual('regenerator-runtime/runtime');
global.window = global;

global.requestAnimationFrame = function (callback) {
return setTimeout(callback, 0);
};
global.cancelAnimationFrame = function (id) {
clearTimeout(id);
};
Object.defineProperties(global, {
__DEV__: {
configurable: true,
enumerable: true,
value: true,
writable: true,
},
Promise: {
configurable: true,
enumerable: true,
value: jest.requireActual('promise'),
writable: true,
},
cancelAnimationFrame: {
configurable: true,
enumerable: true,
value: id => clearTimeout(id),
writable: true,
},
performance: {
configurable: true,
enumerable: true,
value: {
now: jest.fn(Date.now),
},
writable: true,
},
regeneratorRuntime: {
configurable: true,
enumerable: true,
value: jest.requireActual('regenerator-runtime/runtime'),
writable: true,
},
requestAnimationFrame: {
configurable: true,
enumerable: true,
value: callback => setTimeout(callback, 0),
writable: true,
},
window: {
configurable: true,
enumerable: true,
value: global,
writable: true,
},
});

// there's a __mock__ for it.
jest.setMock(
Expand Down

0 comments on commit d607b0b

Please sign in to comment.