Repository navigation
Expand file tree
/
Copy pathoptions.ts
More file actions
165 lines (148 loc) · 4.74 KB
/
Copy pathoptions.ts
File metadata and controls
165 lines (148 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { availableParallelism } from 'node:os';
import { resolve } from 'node:path';
import {
DEFAULT_NATIVE_SCRIPT_VITEST_PORT,
totalSlots,
type NativeScriptSlotLayout,
} from '../protocol.js';
import type { NativeScriptWorkerCount } from '../threading.js';
import { resolveNativeScriptWorkerCount } from '../threading.js';
export { DEFAULT_NATIVE_SCRIPT_VITEST_PORT } from '../protocol.js';
export type NativeScriptPlatform = 'android' | 'ios' | 'visionos' | 'tvos';
/**
* Canonical platform ids plus the loose spellings that reach the plugin via
* NS_PLATFORM or the NativeScript CLI (`iOS`, `visionOS`, `vision`, ...).
*/
export type NativeScriptPlatformInput = NativeScriptPlatform | (string & {});
const NATIVE_SCRIPT_PLATFORMS: readonly NativeScriptPlatform[] = [
'android',
'ios',
'visionos',
'tvos',
];
const NATIVE_SCRIPT_PLATFORM_ALIASES: Record<string, NativeScriptPlatform> = {
vision: 'visionos',
};
export function normalizeNativeScriptPlatform(
platform: NativeScriptPlatformInput,
): NativeScriptPlatform {
const lowered = String(platform).toLowerCase();
const normalized = NATIVE_SCRIPT_PLATFORM_ALIASES[lowered] ?? lowered;
if (!NATIVE_SCRIPT_PLATFORMS.includes(normalized as NativeScriptPlatform)) {
throw new RangeError(
`Unknown NativeScript platform '${String(platform)}'. Supported platforms: ${NATIVE_SCRIPT_PLATFORMS.join(', ')}`,
);
}
return normalized as NativeScriptPlatform;
}
export interface NativeScriptLaunchCommand {
command: string;
args: string[];
}
export interface NativeScriptPluginOptions {
platform: NativeScriptPlatformInput;
/** NativeScript app root (where nativescript.config.ts lives). Defaults to the Vitest root. */
appPath?: string;
/**
* Run specs on the application's main (UI) thread. This is the default and
* is required for tests that create Views, navigate Frames, or touch any
* other UI API.
*/
mainThread?: boolean;
/** Additional isolated NativeScript Worker runtimes for parallel non-UI specs. */
workers?: NativeScriptWorkerCount;
host?: string;
port?: number;
device?: string;
launch?: boolean;
launchCommand?: NativeScriptLaunchCommand;
/**
* Map the device's 127.0.0.1:<port> to this host via `adb reverse` so
* physical Android devices reach the runner over USB instead of LAN.
*/
adbReverse?: boolean;
adbPath?: string;
connectTimeout?: number;
include?: string[];
}
export interface ResolvedNativeScriptPluginOptions {
platform: NativeScriptPlatform;
appPath: string;
slots: NativeScriptSlotLayout;
host: string;
port: number;
device?: string;
launch: boolean;
launchCommand: NativeScriptLaunchCommand;
adbReverse: boolean;
adbPath: string;
connectTimeout: number;
include: string[];
}
const DEFAULT_INCLUDE = [
'app/**/*.spec.ts',
'app/**/*.spec.js',
'src/**/*.spec.ts',
'src/**/*.spec.js',
];
const COVERAGE_ENVIRONMENT_FLAG = '--env.codeCoverage';
export function withNativeScriptCoverageLaunchCommand(
launchCommand: NativeScriptLaunchCommand,
): NativeScriptLaunchCommand {
if (launchCommand.args.includes(COVERAGE_ENVIRONMENT_FLAG)) {
return launchCommand;
}
return {
...launchCommand,
args: [...launchCommand.args, COVERAGE_ENVIRONMENT_FLAG],
};
}
export function resolveNativeScriptPluginOptions(
options: NativeScriptPluginOptions,
cwd = process.cwd(),
parallelism = availableParallelism(),
): ResolvedNativeScriptPluginOptions {
const platform = normalizeNativeScriptPlatform(options.platform);
const port = options.port ?? DEFAULT_NATIVE_SCRIPT_VITEST_PORT;
if (!Number.isInteger(port) || port < 1 || port > 65_535) {
throw new RangeError(
'NativeScript Vitest port must be between 1 and 65535',
);
}
const slots: NativeScriptSlotLayout = {
main: options.mainThread ?? true,
workers: resolveNativeScriptWorkerCount(options.workers, parallelism),
};
if (totalSlots(slots) === 0) {
throw new RangeError(
'NativeScript Vitest needs at least one execution context: enable mainThread or set workers > 0',
);
}
const appPath = resolve(cwd, options.appPath ?? '.');
const defaultArgs = [
'ns',
'run',
platform,
'--no-hmr',
'--env.unitTesting',
`--env.testRunnerPort=${port}`,
];
if (options.device) defaultArgs.push('--device', options.device);
return {
platform,
appPath,
slots,
host: options.host ?? '127.0.0.1',
port,
device: options.device,
launch: options.launch ?? true,
launchCommand: options.launchCommand ?? {
command: 'npx',
args: defaultArgs,
},
adbReverse: options.adbReverse ?? platform === 'android',
adbPath: options.adbPath ?? 'adb',
connectTimeout: options.connectTimeout ?? 120_000,
include: options.include ?? DEFAULT_INCLUDE,
};
}