Skip to content

Commit 5479007

Browse files
committed
fix(test): pin @nativescript/unit-test-runner to the 4.x line
Ensures backwards compat when 5.x is latest
1 parent 7d540b6 commit 5479007

1 file changed

Lines changed: 30 additions & 29 deletions

File tree

lib/commands/test-init.ts

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class TestInitCommand implements ICommand {
3636
private $resources: IResourceLoader,
3737
private $pluginsService: IPluginsService,
3838
private $logger: ILogger,
39-
private $testInitializationService: ITestInitializationService
39+
private $testInitializationService: ITestInitializationService,
4040
) {
4141
this.$projectData.initializeProjectData();
4242
}
@@ -48,11 +48,11 @@ class TestInitCommand implements ICommand {
4848
this.$options.framework ||
4949
(await this.$prompter.promptForChoice(
5050
"Select testing framework:",
51-
TESTING_FRAMEWORKS
51+
TESTING_FRAMEWORKS,
5252
));
5353
if (TESTING_FRAMEWORKS.indexOf(frameworkToInstall) === -1) {
5454
this.$errors.failWithHelp(
55-
`Unknown or unsupported unit testing framework: ${frameworkToInstall}.`
55+
`Unknown or unsupported unit testing framework: ${frameworkToInstall}.`,
5656
);
5757
}
5858

@@ -64,19 +64,18 @@ class TestInitCommand implements ICommand {
6464

6565
let modulesToInstall: IDependencyInformation[] = [];
6666
try {
67-
modulesToInstall = this.$testInitializationService.getDependencies(
68-
frameworkToInstall
69-
);
67+
modulesToInstall =
68+
this.$testInitializationService.getDependencies(frameworkToInstall);
7069
} catch (err) {
7170
this.$errors.fail(
72-
`Unable to install the unit testing dependencies. Error: '${err.message}'`
71+
`Unable to install the unit testing dependencies. Error: '${err.message}'`,
7372
);
7473
}
7574

7675
modulesToInstall = modulesToInstall.filter(
7776
(moduleToInstall) =>
7877
!moduleToInstall.projectType ||
79-
moduleToInstall.projectType === projectFilesExtension
78+
moduleToInstall.projectType === projectFilesExtension,
8079
);
8180

8281
for (const mod of modulesToInstall) {
@@ -101,7 +100,7 @@ class TestInitCommand implements ICommand {
101100
for (const peerDependency in modulePeerDependencies) {
102101
const isPeerDependencyExcluded = _.includes(
103102
mod.excludedPeerDependencies,
104-
peerDependency
103+
peerDependency,
105104
);
106105
if (isPeerDependencyExcluded) {
107106
continue;
@@ -122,17 +121,19 @@ class TestInitCommand implements ICommand {
122121
frameworkPath: this.$options.frameworkPath,
123122
ignoreScripts: this.$options.ignoreScripts,
124123
path: this.$options.path,
125-
}
124+
},
126125
);
127126
} catch (e) {
128127
this.$logger.error(e.message);
129128
}
130129
}
131130
}
132131

132+
// The Karma client only exists in the v4 line — v5+ is Vitest-only, so
133+
// an unpinned install would break these setups once v5 is `latest`.
133134
await this.$pluginsService.add(
134-
"@nativescript/unit-test-runner",
135-
this.$projectData
135+
"@nativescript/unit-test-runner@^4.0.0",
136+
this.$projectData,
136137
);
137138

138139
this.$logger.clearScreen();
@@ -142,11 +143,11 @@ class TestInitCommand implements ICommand {
142143
const testsDir = path.join(this.$projectData.appDirectoryPath, "tests");
143144
const projectTestsDir = path.relative(
144145
this.$projectData.projectDir,
145-
testsDir
146+
testsDir,
146147
);
147148
const relativeTestsDir = path.relative(
148149
this.$projectData.appDirectoryPath,
149-
testsDir
150+
testsDir,
150151
);
151152
let shouldCreateSampleTests = true;
152153
if (this.$fs.exists(testsDir)) {
@@ -157,8 +158,8 @@ class TestInitCommand implements ICommand {
157158
`Note: The "${projectTestsDir}" directory already exists, will not create example tests in the project.`,
158159
`You may create "${specFilenamePattern}" files anywhere you'd like.`,
159160
"",
160-
].join("\n")
161-
)
161+
].join("\n"),
162+
),
162163
);
163164
shouldCreateSampleTests = false;
164165
}
@@ -170,7 +171,7 @@ class TestInitCommand implements ICommand {
170171
.map((fw) => `'${fw}'`)
171172
.join(", ");
172173
const testFiles = `'${fromWindowsRelativePathToUnix(
173-
relativeTestsDir
174+
relativeTestsDir,
174175
)}/**/*${projectFilesExtension}'`;
175176
const karmaConfTemplate = this.$resources.readText("test/karma.conf.js");
176177
const karmaConf = _.template(karmaConfTemplate)({
@@ -182,51 +183,51 @@ class TestInitCommand implements ICommand {
182183
this.$fs.writeFile(path.join(projectDir, "karma.conf.js"), karmaConf);
183184

184185
const exampleFilePath = this.$resources.resolvePath(
185-
`test/example.${frameworkToInstall}${projectFilesExtension}`
186+
`test/example.${frameworkToInstall}${projectFilesExtension}`,
186187
);
187188
const targetExampleTestPath = path.join(
188189
testsDir,
189-
`example.spec${projectFilesExtension}`
190+
`example.spec${projectFilesExtension}`,
190191
);
191192

192193
if (shouldCreateSampleTests && this.$fs.exists(exampleFilePath)) {
193194
this.$fs.copyFile(exampleFilePath, targetExampleTestPath);
194195
const targetExampleTestRelativePath = path.relative(
195196
projectDir,
196-
targetExampleTestPath
197+
targetExampleTestPath,
197198
);
198199
bufferedLogs.push(
199-
`Added example test: ${color.yellow(targetExampleTestRelativePath)}`
200+
`Added example test: ${color.yellow(targetExampleTestRelativePath)}`,
200201
);
201202
}
202203

203204
// test main entry
204205
const testMainResourcesPath = this.$resources.resolvePath(
205-
`test/test-main${projectFilesExtension}`
206+
`test/test-main${projectFilesExtension}`,
206207
);
207208
const testMainPath = path.join(
208209
this.$projectData.appDirectoryPath,
209-
`test${projectFilesExtension}`
210+
`test${projectFilesExtension}`,
210211
);
211212

212213
if (!this.$fs.exists(testMainPath)) {
213214
this.$fs.copyFile(testMainResourcesPath, testMainPath);
214215
const testMainRelativePath = path.relative(projectDir, testMainPath);
215216
bufferedLogs.push(
216-
`Main test entrypoint created: ${color.yellow(testMainRelativePath)}`
217+
`Main test entrypoint created: ${color.yellow(testMainRelativePath)}`,
217218
);
218219
}
219220

220221
const testTsConfigTemplate = this.$resources.readText(
221-
"test/tsconfig.spec.json"
222+
"test/tsconfig.spec.json",
222223
);
223224
const testTsConfig = _.template(testTsConfigTemplate)({
224225
basePath: this.$projectData.getAppDirectoryRelativePath(),
225226
});
226227

227228
this.$fs.writeFile(
228229
path.join(projectDir, "tsconfig.spec.json"),
229-
testTsConfig
230+
testTsConfig,
230231
);
231232
bufferedLogs.push(`Added/replaced ${color.yellow("tsconfig.spec.json")}`);
232233

@@ -242,11 +243,11 @@ class TestInitCommand implements ICommand {
242243
...bufferedLogs,
243244
"",
244245
color.yellow(
245-
`Note: @nativescript/unit-test-runner was included in "dependencies" as a convenience to automatically adjust your app's Info.plist on iOS and AndroidManifest.xml on Android to ensure the socket connects properly.`
246+
`Note: @nativescript/unit-test-runner was included in "dependencies" as a convenience to automatically adjust your app's Info.plist on iOS and AndroidManifest.xml on Android to ensure the socket connects properly.`,
246247
),
247248
"",
248249
color.yellow(
249-
`For production you may want to move to "devDependencies" and manage the settings yourself.`
250+
`For production you may want to move to "devDependencies" and manage the settings yourself.`,
250251
),
251252
"",
252253
"",
@@ -255,7 +256,7 @@ class TestInitCommand implements ICommand {
255256
` ${greyDollarSign} ${color.green("ns test ios")}`,
256257
` ${greyDollarSign} ${color.green("ns test android")}`,
257258
"",
258-
].join("\n")
259+
].join("\n"),
259260
);
260261
}
261262
}

0 commit comments

Comments
 (0)