Skip to content

Commit f58718e

Browse files
authored
Fabric: Enable react-test-renderer tests in the new cpp-app template (#12376)
## Description The upstream RN app template includes a jest test suite (run with `yarn test`) with a default test to render your app JS using the `react-test-renderer`. However, this only tests the iOS version of your app (it forces platform === 'ios') and therefore won't catch if there are issues in your `windows` platform files. Using `@rnx-kit/jest-preset`, you can run jest tests against any platform via a custom jest config. This PR adds `@rnx-kit/jest-preset` as a dependency to the new `cpp-app` template, as well as a `jest.config.windows.js` to use it. Furthermore it adds a script so users can run these tests for windows via `yarn test:windows`. Finally this PR adds running `yarn test:windows` to the PR checks for testing the new app CLI. Closes #11939 ### Type of Change - New feature (non-breaking change which adds functionality) ### Why To expand test coverage options for app developers. Closes #11939 ### What See above. ## Screenshots ![image](https://github.com/microsoft/react-native-windows/assets/10852185/742c5e52-4135-4b34-9642-8bdc99c7f132) ## Testing Ran the tests in the provided RN template. ## Changelog Should this change be included in the release notes: yes The new Fabric app template supports `yarn test:windows` to run jest app rendering tests with react-test-renderer
1 parent 44c98f7 commit f58718e

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

.ado/templates/react-native-init-windows.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ steps:
112112
parameters:
113113
buildLogDirectory: '$(Build.BinariesDirectory)\${{ parameters.platform }}\${{ parameters.configuration }}\BuildLogs'
114114

115+
# Only run the following on apps
116+
- ${{ if endsWith(parameters.template, '-app') }}:
117+
- script: call yarn test:windows
118+
displayName: Run jest tests with react-test-renderer
119+
workingDirectory: $(Agent.BuildDirectory)\testcli
120+
115121
# Only test bundling in debug since we already bundle as part of release builds
116122
- ${{ if and(endsWith(parameters.template, '-app'), eq(parameters.configuration, 'Debug')) }}:
117123
- script: npx react-native bundle --entry-file index.js --platform windows --bundle-output test.bundle
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Fabric: Enable react-test-renderer tests in the new cpp-app template",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const config = {};
2+
3+
module.exports = require('@rnx-kit/jest-preset')('windows', config);

vnext/templates/cpp-app/template.config.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,20 @@ async function getFileMappings(config = {}, options = {}) {
107107
}
108108

109109
async function postInstall(config = {}, options = {}) {
110-
// Update package.json with new scripts
110+
// Update package.json with new scripts and dependencies
111111
await templateUtils.updateProjectPackageJson(config, options, {
112-
scripts: {windows: 'react-native run-windows'},
112+
scripts: {
113+
windows: 'react-native run-windows',
114+
'test:windows': 'jest --config jest.config.windows.js',
115+
},
116+
devDependencies: {
117+
'@rnx-kit/jest-preset': '^0.1.16',
118+
},
113119
});
114120

121+
// Install recently added dependencies
122+
await templateUtils.runNpmInstall(config, options);
123+
115124
console.log(chalk.white.bold('To run your new windows app:'));
116125
console.log(chalk.white(' npx react-native run-windows'));
117126
}

vnext/templates/templateUtils.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,26 @@
66
* @format
77
*/
88

9+
const existsSync = require('fs').existsSync;
10+
const path = require('path');
11+
const util = require('util');
12+
const exec = util.promisify(require('child_process').exec);
13+
914
const pkgUtils = require('@react-native-windows/package-utils');
1015

16+
async function runNpmInstall(config = {}, options = {}) {
17+
const projectPath = config?.root ?? process.cwd();
18+
19+
if (options?.logging) {
20+
console.log('Installing dependencies...');
21+
}
22+
const isYarn = existsSync(path.join(projectPath, 'yarn.lock'));
23+
await exec(
24+
isYarn ? 'yarn' : 'npm i',
25+
options?.logging ? {stdio: 'inherit'} : {},
26+
);
27+
}
28+
1129
async function updateProjectPackageJson(config = {}, options = {}, props = {}) {
1230
const projectPath = config?.root ?? process.cwd();
1331
const projectPackage = await pkgUtils.WritableNpmPackage.fromPath(
@@ -26,4 +44,4 @@ async function updateProjectPackageJson(config = {}, options = {}, props = {}) {
2644
await projectPackage.mergeProps(props);
2745
}
2846

29-
module.exports = {updateProjectPackageJson};
47+
module.exports = {runNpmInstall, updateProjectPackageJson};

0 commit comments

Comments
 (0)