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.

The idea comes from Greg on a video call, with a code example
provided by a @testing-library/react-native contributor [1]. Follow
that example, with tweaks for our project -- using camelCase for the
file names, putting the new files in our `jest` directory, and
adding a TODO mentioning that we'd maybe like to get back to using
jest-expo, which wraps React Native's preset.

[1] jestjs/jest#10221 (comment),
    which links to
    sbalay/without_await@64a76486f.
  • Loading branch information
chrisbobbe committed Jan 11, 2021
1 parent d4b94ff commit dc8a532
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion jest.config.js
Expand Up @@ -24,7 +24,7 @@ const transformModulesWhitelist = [
const transformIgnorePattern = `node_modules/(?!${transformModulesWhitelist.join('|')})`;

module.exports = {
preset: 'react-native',
preset: './jest/preset.js',

// Finding and transforming source code.

Expand Down
19 changes: 19 additions & 0 deletions jest/preset.js
@@ -0,0 +1,19 @@
/**
* The preset we're making tweaks to.
*/
// TODO: Switch back to jest-expo (i.e., use 'jest-expo/jest-preset')
// when it has a version that depends on Jest 26, not Jest 25.
const basePreset = require('react-native/jest-preset');

module.exports = {
...basePreset,
setupFiles: [
// Until facebook/jest#10221 is resolved, sandwich the faulty
// logic in react-native's preset (in which `global.Promise` is
// harmfully replaced) with a fix. See
// https://github.com/facebook/jest/issues/10221#issuecomment-714335771.
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 dc8a532

Please sign in to comment.