From c937439e875ab5072aa2605686d2f103b8f8ea0c Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Thu, 22 May 2025 09:44:15 -0700 Subject: [PATCH] Add a basic Jest test to helloworld (#51534) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51534 Aims to give us CI coverage of React Native's Jest preset, preventing future bugs like https://github.com/facebook/react-native/pull/51525. Changes: - Add a basic "it renders" Jest test to helloworld - Add "Run Helloworld tests" step to `test-ios-helloworld` job in CI - Also convert helloworld's `App.tsx` to TypeScript, as easiest way to unblock Jest JSX behaviour. Changelog: [Internal] Reviewed By: cortinico Differential Revision: D75218901 fbshipit-source-id: 601155c59c4483696971df4c29d51549d97f49f2 --- .github/actions/test-ios-helloworld/action.yml | 8 +++++++- packages/helloworld/{App.jsx => App.tsx} | 3 +-- packages/helloworld/__tests__/App.test.tsx | 18 ++++++++++++++++++ packages/helloworld/package.json | 2 ++ packages/helloworld/tsconfig.json | 3 +++ yarn.lock | 8 ++++++++ 6 files changed, 39 insertions(+), 3 deletions(-) rename packages/helloworld/{App.jsx => App.tsx} (94%) create mode 100644 packages/helloworld/__tests__/App.test.tsx create mode 100644 packages/helloworld/tsconfig.json diff --git a/.github/actions/test-ios-helloworld/action.yml b/.github/actions/test-ios-helloworld/action.yml index 6a7c7367e29..e85cad17a77 100644 --- a/.github/actions/test-ios-helloworld/action.yml +++ b/.github/actions/test-ios-helloworld/action.yml @@ -83,7 +83,13 @@ runs: yarn bootstrap ios "${args[@]}" | cat - - name: Build HelloWorld project + - name: Run Helloworld tests + shell: bash + run: | + cd packages/helloworld + yarn test + + - name: Build HelloWorld project shell: bash run: | cd packages/helloworld diff --git a/packages/helloworld/App.jsx b/packages/helloworld/App.tsx similarity index 94% rename from packages/helloworld/App.jsx rename to packages/helloworld/App.tsx index 9bbe08bbaed..09f7928a852 100644 --- a/packages/helloworld/App.jsx +++ b/packages/helloworld/App.tsx @@ -4,7 +4,6 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local * @format */ @@ -19,7 +18,7 @@ import { useColorScheme, } from 'react-native'; -function App(): React.Node { +function App(): React.ReactNode { const isDarkMode = useColorScheme() === 'dark'; return ( diff --git a/packages/helloworld/__tests__/App.test.tsx b/packages/helloworld/__tests__/App.test.tsx new file mode 100644 index 00000000000..399fc98c4b4 --- /dev/null +++ b/packages/helloworld/__tests__/App.test.tsx @@ -0,0 +1,18 @@ +/** + * 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 + */ + +import App from '../App'; +import React from 'react'; +import ReactTestRenderer from 'react-test-renderer'; + +test('renders correctly', async () => { + await ReactTestRenderer.act(() => { + ReactTestRenderer.create(); + }); +}); diff --git a/packages/helloworld/package.json b/packages/helloworld/package.json index bc92f858d55..82448d2dd67 100644 --- a/packages/helloworld/package.json +++ b/packages/helloworld/package.json @@ -23,6 +23,8 @@ "@react-native/core-cli-utils": "0.80.0-main", "@react-native/eslint-config": "0.80.0-main", "@react-native/metro-config": "0.80.0-main", + "@react-native/typescript-config": "0.80.0-main", + "@types/jest": "^29.5.14", "chalk": "^4.1.2", "commander": "^12.0.0", "eslint": "^8.19.0", diff --git a/packages/helloworld/tsconfig.json b/packages/helloworld/tsconfig.json new file mode 100644 index 00000000000..88fa317f10c --- /dev/null +++ b/packages/helloworld/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "@react-native/typescript-config" +} diff --git a/yarn.lock b/yarn.lock index e1770521d2a..878a4cc3eb5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1918,6 +1918,14 @@ dependencies: "@types/istanbul-lib-report" "*" +"@types/jest@^29.5.14": + version "29.5.14" + resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.14.tgz#2b910912fa1d6856cadcd0c1f95af7df1d6049e5" + integrity sha512-ZN+4sdnLUbo8EVvVc2ao0GFW6oVrQRPn4K2lglySj7APvSrgzxHiNNK99us4WDMi57xxA2yggblIAMNhXOotLQ== + dependencies: + expect "^29.0.0" + pretty-format "^29.0.0" + "@types/jest@^29.5.3": version "29.5.12" resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.12.tgz#7f7dc6eb4cf246d2474ed78744b05d06ce025544"