Skip to content

Commit 764e577

Browse files
committed
chore: visionos support for unit testing with vitest
1 parent 51352d9 commit 764e577

9 files changed

Lines changed: 110 additions & 7 deletions

File tree

docs/man_pages/project/testing/test-android.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ Command | Description
4343
--------|------------
4444
[test init](test-init.html) | Configures your project for unit testing with a selected framework.
4545
[test ios](test-ios.html) | Runs the tests in your project on iOS devices or the iOS Simulator.
46+
[test visionos](test-visionos.html) | Runs the tests in your project in the visionOS Simulator or on Apple Vision Pro devices.
4647
<% } %>

docs/man_pages/project/testing/test-init.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ Command | Description
3333
--------|------------
3434
[test android](test-android.html) | Runs the tests in your project on Android devices or native emulators.
3535
[test ios](test-ios.html) | Runs the tests in your project on iOS devices or the iOS Simulator.
36+
[test visionos](test-visionos.html) | Runs the tests in your project in the visionOS Simulator or on Apple Vision Pro devices.
3637
<% } %>

docs/man_pages/project/testing/test-ios.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ Command | Description
4545
--------|------------
4646
[test init](test-init.html) | Configures your project for unit testing with a selected framework.
4747
[test android](test-android.html) | Runs the tests in your project on Android devices or native emulators.
48+
[test visionos](test-visionos.html) | Runs the tests in your project in the visionOS Simulator or on Apple Vision Pro devices.
4849
<% } %>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<% if (isJekyll) { %>---
2+
title: ns test visionos
3+
position: 24
4+
---<% } %>
5+
6+
# ns test visionos
7+
8+
### Description
9+
10+
Runs the tests in your project in the visionOS Simulator or on connected Apple Vision Pro devices.<% if(isConsole && isMacOS) { %> Your project must already be configured for unit testing with the Vitest framework by running `$ ns test init --framework vitest`.<% } %> Unit testing on visionOS requires the Vitest testing framework; the deprecated Karma-based frameworks are not supported on this platform.
11+
12+
<% if(isConsole && (isLinux || isWindows)) { %>WARNING: You can run this command only on macOS systems. To view the complete help for this command, run `$ ns help test visionos`<% } %>
13+
14+
### Commands
15+
16+
Usage | Synopsis
17+
------|-------
18+
Run tests in the visionOS Simulator | `$ ns test visionos`
19+
Run tests on a selected device | `$ ns test visionos --device <Device ID>`
20+
21+
<% if((isConsole && isMacOS) || isHtml) { %>
22+
23+
### Options
24+
25+
* `--device` - Specifies the serial number or the index of the connected device on which you want to run tests. To list all connected devices, grouped by platform, run `$ ns device`. `<Device ID>` is the device index or identifier as listed by the `$ ns device` command.
26+
* `--env.codeCoverage` - If set, collects code coverage for the test run.
27+
* `--force` - If set, skips the application compatibility checks and forces `npm i` to ensure all dependencies are installed. Otherwise, the command will check the application compatibility with the current CLI version and could fail requiring `ns migrate`.
28+
29+
<% } %>
30+
31+
<% if(isHtml) { %>
32+
33+
### Prerequisites
34+
35+
* Verify that [you have configured your project for unit testing](test-init.html) with the Vitest framework.
36+
* Verify that [you have stored your unit tests in `app` &#8594; `tests`](http://docs.nativescript.org/testing).
37+
* Verify that [you have configured your system and devices properly](http://docs.nativescript.org/testing).
38+
39+
### Related Commands
40+
41+
Command | Description
42+
--------|------------
43+
[test init](test-init.html) | Configures your project for unit testing with a selected framework.
44+
[test android](test-android.html) | Runs the tests in your project on Android devices or native emulators.
45+
[test ios](test-ios.html) | Runs the tests in your project on iOS devices or the iOS Simulator.
46+
<% } %>

docs/man_pages/project/testing/test.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ Usage | Synopsis
2828
<% if((isConsole && isMacOS) || isHtml) { %>### Arguments
2929
`<Platform>` is the target mobile platform on which you want to run the tests. You can set the following target platforms.
3030
* `android` - Runs the tests in your project on connected Android devices or Android emulators.
31-
* `ios` - Runs the tests in your project on connected iOS devices.<% } %>
31+
* `ios` - Runs the tests in your project on connected iOS devices.
32+
* `visionos` - Runs the tests in your project in the visionOS Simulator or on connected Apple Vision Pro devices. Requires the Vitest testing framework.<% } %>
3233

3334
<% if(isHtml) { %>
3435

@@ -45,4 +46,5 @@ Command | Description
4546
[test init](test-init.html) | Configures your project for unit testing with a selected framework.
4647
[test android](test-android.html) | Runs the tests in your project on Android devices or native emulators.
4748
[test ios](test-ios.html) | Runs the tests in your project on iOS devices or the iOS Simulator.
49+
[test visionos](test-visionos.html) | Runs the tests in your project in the visionOS Simulator or on Apple Vision Pro devices.
4850
<% } %>

lib/bootstrap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ injector.requireCommand("dev-test|android", "./commands/test");
211211
injector.requireCommand("dev-test|ios", "./commands/test");
212212
injector.requireCommand("test|android", "./commands/test");
213213
injector.requireCommand("test|ios", "./commands/test");
214-
// injector.requireCommand("test|vision", "./commands/test");
215-
// injector.requireCommand("test|visionos", "./commands/test");
214+
injector.requireCommand("test|vision", "./commands/test");
215+
injector.requireCommand("test|visionos", "./commands/test");
216216
injector.requireCommand("test|init", "./commands/test-init");
217217
injector.requireCommand("dev-generate-help", "./commands/generate-help");
218218

lib/commands/test-init.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,15 @@ class TestInitCommand implements ICommand {
345345
const closingNotes = isVitest
346346
? [
347347
color.yellow(
348-
`Note: emulator/simulator test runs connect over the local loopback. When testing on a physical Android device, keep it connected over USB (adb reverse is set up automatically); for a physical iOS device, pass a reachable 'url' to the coordinator in your test entry.`,
348+
`Note: emulator/simulator test runs connect over the local loopback. When testing on a physical Android device, keep it connected over USB (adb reverse is set up automatically); for a physical iOS or visionOS device, pass a reachable 'url' to the coordinator in your test entry.`,
349349
),
350350
"",
351351
"",
352352
`You can now run your tests:`,
353353
"",
354354
` ${greyDollarSign} ${color.green("ns test ios")}`,
355355
` ${greyDollarSign} ${color.green("ns test android")}`,
356+
` ${greyDollarSign} ${color.green("ns test visionos")}`,
356357
"",
357358
`or directly through Vitest (editor extensions, CI):`,
358359
"",

lib/commands/test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,5 +235,56 @@ class TestIosCommand extends TestCommandBase implements ICommand {
235235
}
236236
}
237237

238+
class TestVisionOSCommand extends TestIosCommand {
239+
protected platform = "visionOS";
240+
241+
// The injector discovers dependencies by parsing constructor source text,
242+
// so an inherited constructor would resolve to zero dependencies.
243+
constructor(
244+
protected $projectData: IProjectData,
245+
protected $testExecutionService: ITestExecutionService,
246+
protected $vitestExecutionService: IVitestExecutionService,
247+
protected $analyticsService: IAnalyticsService,
248+
protected $options: IOptions,
249+
protected $platformEnvironmentRequirements: IPlatformEnvironmentRequirements,
250+
protected $errors: IErrors,
251+
protected $cleanupService: ICleanupService,
252+
protected $liveSyncCommandHelper: ILiveSyncCommandHelper,
253+
protected $devicesService: Mobile.IDevicesService,
254+
protected $migrateController: IMigrateController,
255+
protected $logger: ILogger,
256+
) {
257+
super(
258+
$projectData,
259+
$testExecutionService,
260+
$vitestExecutionService,
261+
$analyticsService,
262+
$options,
263+
$platformEnvironmentRequirements,
264+
$errors,
265+
$cleanupService,
266+
$liveSyncCommandHelper,
267+
$devicesService,
268+
$migrateController,
269+
$logger,
270+
);
271+
}
272+
273+
async canExecute(args: string[]): Promise<boolean> {
274+
this.$projectData.initializeProjectData();
275+
// The Karma runner (v4 line) never supported visionOS — only the Vitest
276+
// path can drive it.
277+
if (!this.$vitestExecutionService.isVitestProject(this.$projectData)) {
278+
this.$errors.fail(
279+
"visionOS unit testing requires the Vitest runner. Run '$ ns test init --framework vitest' to configure your project.",
280+
);
281+
}
282+
283+
return super.canExecute(args);
284+
}
285+
}
286+
238287
injector.registerCommand("test|android", TestAndroidCommand);
239288
injector.registerCommand("test|ios", TestIosCommand);
289+
injector.registerCommand("test|vision", TestVisionOSCommand);
290+
injector.registerCommand("test|visionos", TestVisionOSCommand);

resources/test/vitest.config.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { defineConfig } from 'vitest/config';
22
import { nativeScript } from '@nativescript/unit-test-runner';
33

4-
// Platform is selected per run: `ns test ios` / `ns test android`, or
5-
// NS_PLATFORM=ios npx vitest run
4+
// Platform is selected per run: `ns test ios` / `ns test android` /
5+
// `ns test visionos`, or NS_PLATFORM=ios npx vitest run
66
export default defineConfig({
77
plugins: [
88
nativeScript({
9-
platform: (process.env.NS_PLATFORM as 'android' | 'ios') || 'ios',
9+
platform: process.env.NS_PLATFORM || 'ios', // 'android' | 'ios' | 'visionos'
1010
device: process.env.NS_DEVICE || undefined,
1111
}),
1212
],

0 commit comments

Comments
 (0)