From cb2dcd327c62edae58bb8571273add4c286fd2a8 Mon Sep 17 00:00:00 2001 From: Lorenzo Sciandra Date: Mon, 17 Oct 2022 12:40:25 -0700 Subject: [PATCH] feat(jest): move Jest config to use a custom react-native Jest env (#34971) Summary: This PR is the follow up to the conversation started here by SimenB: https://github.com/react-native-community/discussions-and-proposals/issues/509 Basically, we want to move RN to use its own custom environment so that we can tweak it going forward - this PR in fact only sets up the groundwork for that; robhogan mentioned that with this in place, Meta engineers can > iterate on it (with jest-environment-node as a starting point) against our internal product tests This is also connected to Rob's work to bring Jest 29 into the codebase https://github.com/facebook/react-native/pull/34724 and my "mirror" PR to bring template in main up to the same version (https://github.com/facebook/react-native/pull/34972) ## Changelog [General] [Changed] - move Jest config to use a custom react-native Jest env Pull Request resolved: https://github.com/facebook/react-native/pull/34971 Test Plan: Tested that `yarn test` in main works fine after the changes; CI and Meta's internal CI will also serve the purpose of verifying that it works (but there's no reason not to since it's still pretty much just relying on `node`). Reviewed By: huntie Differential Revision: D40379760 Pulled By: robhogan fbshipit-source-id: 2c6d0bc86d337fda9befce0799bda2f56cc4466c --- jest-preset.js | 2 +- jest/react-native-env.js | 16 ++++++++++++++++ package.json | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 jest/react-native-env.js diff --git a/jest-preset.js b/jest-preset.js index e49c4062687ea5..d1a749c5a2701a 100644 --- a/jest-preset.js +++ b/jest-preset.js @@ -24,5 +24,5 @@ module.exports = { 'node_modules/(?!((jest-)?react-native|@react-native(-community)?)/)', ], setupFiles: [require.resolve('./jest/setup.js')], - testEnvironment: 'node', + testEnvironment: require.resolve('./jest/react-native-env.js'), }; diff --git a/jest/react-native-env.js b/jest/react-native-env.js new file mode 100644 index 00000000000000..b45ac255d9c202 --- /dev/null +++ b/jest/react-native-env.js @@ -0,0 +1,16 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +'use strict'; + +const NodeEnv = require('jest-environment-node').TestEnvironment; + +module.exports = class ReactNativeEnv extends NodeEnv { + // this is where we'll add our custom logic +}; diff --git a/package.json b/package.json index c3dda79f101f39..209cbde1062bb7 100644 --- a/package.json +++ b/package.json @@ -120,6 +120,7 @@ "base64-js": "^1.1.2", "event-target-shim": "^5.0.1", "invariant": "^2.2.4", + "jest-environment-node": "^29.0.3", "jsc-android": "^250230.2.1", "memoize-one": "^5.0.0", "metro-react-native-babel-transformer": "0.72.3",