Repository navigation
Expand file tree
/
Copy pathplugin.ts
More file actions
68 lines (62 loc) · 2.3 KB
/
Copy pathplugin.ts
File metadata and controls
68 lines (62 loc) · 2.3 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
import type { PoolRunnerInitializer, VitestPluginContext } from 'vitest/node';
import { defaultInclude } from 'vitest/config';
import { totalSlots } from '../protocol.js';
import type { NativeScriptPluginOptions } from './options.js';
import {
resolveNativeScriptPluginOptions,
withNativeScriptCoverageLaunchCommand,
} from './options.js';
import { NativeScriptPoolWorker } from './pool-worker.js';
import { WebSocketNativeScriptPoolSession } from './session.js';
export interface NativeScriptPlugin {
name: 'nativescript-unit-test-runner';
configureVitest(context: VitestPluginContext): void;
}
function usesVitestDefaultInclude(include: readonly string[]): boolean {
return (
include.length === defaultInclude.length &&
include.every((pattern, index) => pattern === defaultInclude[index])
);
}
export function nativeScript(
options: NativeScriptPluginOptions,
): NativeScriptPlugin {
const resolved = resolveNativeScriptPluginOptions(options);
const slotCount = totalSlots(resolved.slots);
return {
name: 'nativescript-unit-test-runner',
configureVitest({ project }: VitestPluginContext): void {
const session = new WebSocketNativeScriptPoolSession({
...resolved,
launchCommand:
project.config.coverage?.enabled === true
? withNativeScriptCoverageLaunchCommand(resolved.launchCommand)
: resolved.launchCommand,
});
let nextSlot = 0;
const poolRunner: PoolRunnerInitializer = {
name: 'nativescript',
createPoolWorker: () => {
const slot = nextSlot % slotCount;
nextSlot += 1;
return new NativeScriptPoolWorker(slot, session);
},
};
project.config.pool = poolRunner.name;
project.config.poolRunner = poolRunner;
project.config.maxWorkers = slotCount;
// The device owns a fixed set of long-lived runtimes (main thread +
// Workers). Reusing each matching pool runner keeps its host-side
// lifecycle aligned with the device slot and avoids assigning two files
// to one slot concurrently.
project.config.isolate = false;
if (
options.include ||
!project.config.include ||
usesVitestDefaultInclude(project.config.include)
) {
project.config.include = resolved.include;
}
},
};
}