Skip to content

Commit

Permalink
jest: Fix jestjs/jest#10221 locally.
Browse files Browse the repository at this point in the history
This is much easier than forking React Native, as we considered in
zulip/react-native#5. See
  jestjs/jest#10221 (comment).
  • Loading branch information
chrisbobbe committed May 21, 2021
1 parent b653fef commit de589cb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
21 changes: 19 additions & 2 deletions jest.config.js
Expand Up @@ -30,8 +30,25 @@ const projectForPlatform = platform => {
}
return {
displayName: platform,
// See https://github.com/expo/expo/blob/master/packages/jest-expo/README.md#platforms.
preset: platform === 'ios' ? 'jest-expo/ios' : 'jest-expo/android',

// Ideally, these would simply be `jest-expo/ios` and
// `jest-expo/android`; see
// https://github.com/expo/expo/blob/master/packages/jest-expo/README.md#platforms.
// These custom presets are a workaround for a bug:
//
// `jest-expo`'s presets are based on `react-native`'s preset,
// which does something messy: it overwrites the global `Promise`.
// That's facebook/react-native#29303. Jest doesn't work well with
// that; that's facebook/jest#10221.
//
// So, until one of those is fixed, we use these custom presets to
// sandwich the code that replaces `global.Promise` with a fix:
//
// 1) save `global.Promise` to something else on `global`
// 2) let the `react-native` preset do its thing (like mocking RN
// libraries)
// 3) assign `global.Promise` back to what we saved in step 1
preset: platform === 'ios' ? './jest/presetIos' : './jest/presetAndroid',

// Finding and transforming source code.
testPathIgnorePatterns: ['/node_modules/', '/src/__tests__/lib/', '-testlib.js$'],
Expand Down
14 changes: 14 additions & 0 deletions jest/presetAndroid.js
@@ -0,0 +1,14 @@
/**
* The preset we're making tweaks to.
*/
const basePreset = require('../node_modules/jest-expo/android/jest-preset.js');

// See comment in our Jest config about how this is used.
module.exports = {
...basePreset,
setupFiles: [
require.resolve('./savePromise.js'),
...basePreset.setupFiles,
require.resolve('./restorePromise.js'),
],
};
14 changes: 14 additions & 0 deletions jest/presetIos.js
@@ -0,0 +1,14 @@
/**
* The preset we're making tweaks to.
*/
const basePreset = require('../node_modules/jest-expo/ios/jest-preset.js');

// See comment in our Jest config about how this is used.
module.exports = {
...basePreset,
setupFiles: [
require.resolve('./savePromise.js'),
...basePreset.setupFiles,
require.resolve('./restorePromise.js'),
],
};
3 changes: 3 additions & 0 deletions jest/restorePromise.js
@@ -0,0 +1,3 @@
// For facebook/jest#10221. After savePromise.js and Jest's setup
// files have run, restore the natural value of `global.Promise`.
global.Promise = global.originalPromise;
4 changes: 4 additions & 0 deletions jest/savePromise.js
@@ -0,0 +1,4 @@
// For facebook/jest#10221. Before Jest's setup files have run, take
// note of what the natural value of `global.Promise` is, so we can
// restore it in restorePromise.js.
global.originalPromise = Promise;

0 comments on commit de589cb

Please sign in to comment.