Skip to content

Commit 56269b0

Browse files
authored
test: fix config tests failing when a platform is not installed (#2740)
1 parent 75425e4 commit 56269b0

1 file changed

Lines changed: 32 additions & 23 deletions

File tree

packages/app/example/test/config.test.mjs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import { equal, match, notEqual, ok } from "node:assert/strict";
33
import * as fs from "node:fs";
44
import * as path from "node:path";
5-
import { after, before, test } from "node:test";
6-
import { URL, fileURLToPath } from "node:url";
5+
import { test } from "node:test";
6+
import { readJSONFile } from "../../scripts/helpers.js";
77

88
/**
99
* @param {string} cwd
@@ -34,28 +34,29 @@ function regexp(p) {
3434
return new RegExp(p.replaceAll("\\", "\\\\"));
3535
}
3636

37+
/**
38+
* @param {string} spec
39+
* @param {string} projectRoot
40+
* @returns {boolean}
41+
*/
42+
function requiresDependency(spec, projectRoot) {
43+
/** @type {{ dependencies: Record<string, string> }} */
44+
const { dependencies } = readJSONFile(path.join(projectRoot, "package.json"));
45+
return Object.hasOwn(dependencies, spec);
46+
}
47+
3748
test("react-native config", async (t) => {
3849
const currentDir = process.cwd();
3950
const loadConfig = await getLoadConfig(currentDir);
4051

41-
const projectRoot = path.sep + path.join("packages", "app");
42-
const exampleRoot = path.join(projectRoot, "example");
43-
const reactNativePath = path.join(
44-
exampleRoot,
45-
"node_modules",
46-
"react-native"
47-
);
48-
49-
before(() => process.chdir(fileURLToPath(new URL("..", import.meta.url))));
50-
51-
after(() => process.chdir(currentDir));
52+
const reactNativePath = path.join(currentDir, "node_modules", "react-native");
5253

5354
await t.test("contains Android config", () => {
54-
const sourceDir = path.join(exampleRoot, "android");
55+
const sourceDir = path.join(currentDir, "android");
5556
const config = loadConfig();
5657

5758
equal(typeof config, "object");
58-
match(config.root, regexp(exampleRoot));
59+
match(config.root, regexp(currentDir));
5960
match(config.reactNativePath, regexp(reactNativePath));
6061
equal(
6162
config.dependencies["react-native-test-app"].name,
@@ -74,11 +75,11 @@ test("react-native config", async (t) => {
7475
"contains iOS config",
7576
{ skip: process.platform === "win32" },
7677
() => {
77-
const sourceDir = path.join(exampleRoot, "ios");
78+
const sourceDir = path.join(currentDir, "ios");
7879
const config = loadConfig();
7980

8081
equal(typeof config, "object");
81-
match(config.root, regexp(exampleRoot));
82+
match(config.root, regexp(currentDir));
8283
match(config.reactNativePath, regexp(reactNativePath));
8384
equal(
8485
config.dependencies["react-native-test-app"].name,
@@ -98,13 +99,17 @@ test("react-native config", async (t) => {
9899

99100
await t.test(
100101
"contains macOS config",
101-
{ skip: process.platform === "win32" },
102+
{
103+
skip:
104+
process.platform === "win32" ||
105+
!requiresDependency("react-native-macos", currentDir),
106+
},
102107
() => {
103-
const sourceDir = path.join(exampleRoot, "macos");
108+
const sourceDir = path.join(currentDir, "macos");
104109
const config = loadConfig();
105110

106111
equal(typeof config, "object");
107-
match(config.root, regexp(exampleRoot));
112+
match(config.root, regexp(currentDir));
108113
match(config.reactNativePath, regexp(reactNativePath));
109114
equal(
110115
config.dependencies["react-native-test-app"].name,
@@ -124,7 +129,11 @@ test("react-native config", async (t) => {
124129

125130
await t.test(
126131
"contains Windows config",
127-
{ skip: process.platform !== "win32" },
132+
{
133+
skip:
134+
process.platform !== "win32" ||
135+
!requiresDependency("react-native-windows", currentDir),
136+
},
128137
() => {
129138
const projectFile = path.join(
130139
"node_modules",
@@ -142,14 +151,14 @@ test("react-native config", async (t) => {
142151
const config = loadConfig();
143152

144153
equal(typeof config, "object");
145-
match(config.root, regexp(exampleRoot));
154+
match(config.root, regexp(currentDir));
146155
match(config.reactNativePath, regexp(reactNativePath));
147156
equal(
148157
config.dependencies["react-native-test-app"].name,
149158
"react-native-test-app"
150159
);
151160
equal(config.platforms.windows.npmPackageName, "react-native-windows");
152-
match(config.project.windows.folder, regexp(exampleRoot));
161+
match(config.project.windows.folder, regexp(currentDir));
153162
match(config.project.windows.sourceDir, /windows/);
154163
match(config.project.windows.solutionFile, /Example.sln/);
155164
match(config.project.windows.project.projectFile, regexp(projectFile));

0 commit comments

Comments
 (0)