vitest-native 0.14.1 → 0.15.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -10
- package/dist/cli.d.mts +9 -1
- package/dist/cli.mjs +2609 -361
- package/dist/context.d.mts +19 -0
- package/dist/context.mjs +47 -1
- package/dist/docs-url.d.mts +4 -0
- package/dist/docs-url.mjs +9 -0
- package/dist/errors.d.mts +21 -1
- package/dist/hang-watch-setup.mjs +55 -0
- package/dist/hang-watch.d.mts +56 -0
- package/dist/hang-watch.mjs +343 -0
- package/dist/helpers.cjs +24 -53
- package/dist/helpers.mjs +22 -1
- package/dist/host-D0r3P_T1.mjs +1985 -0
- package/dist/{index-aNBCTfA5.d.mts → index-JOR602XU.d.mts} +105 -2
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +723 -149
- package/dist/jest-compat/aliases.d.mts +1 -0
- package/dist/jest-compat/aliases.mjs +11 -0
- package/dist/jest-compat/interop.mjs +1 -1
- package/dist/jest-compat/jest-object.mjs +23 -5
- package/dist/jest-compat/node-registry.mjs +836 -68
- package/dist/jest-compat/setup.mjs +205 -12
- package/dist/jest-compat.cjs +383 -47
- package/dist/jest-compat.d.cts +19 -2
- package/dist/jest-compat.d.mts +19 -2
- package/dist/jest-compat.mjs +232 -47
- package/dist/matchers.d.cts +0 -1
- package/dist/matchers.d.mts +0 -1
- package/dist/native/assets.mjs +3 -1
- package/dist/native/babel-caller.d.mts +13 -0
- package/dist/native/babel-caller.mjs +24 -0
- package/dist/native/babel-toolchain.mjs +27 -3
- package/dist/native/boundary.mjs +45 -8
- package/dist/native/bounded-process.mjs +63 -5
- package/dist/native/bridge-client.mjs +133 -0
- package/dist/native/bridge-server.mjs +215 -0
- package/dist/native/bridge-thread.mjs +79 -0
- package/dist/native/bridge.mjs +512 -0
- package/dist/native/compile-error.mjs +120 -0
- package/dist/native/explain.mjs +3 -2
- package/dist/native/expo-module-mocks.mjs +2 -0
- package/dist/native/export-condition-recovery.mjs +20 -99
- package/dist/native/hooks.mjs +256 -133
- package/dist/native/jest-resolvers.mjs +94 -0
- package/dist/native/library-jest.mjs +352 -0
- package/dist/native/loader.mjs +167 -73
- package/dist/native/match.mjs +67 -7
- package/dist/native/module-reset.mjs +44 -49
- package/dist/native/ownership-ledger.d.mts +87 -0
- package/dist/native/ownership-ledger.mjs +524 -0
- package/dist/native/ownership.d.mts +20 -0
- package/dist/native/ownership.mjs +114 -0
- package/dist/native/registry-compiler.mjs +39 -5
- package/dist/native/registry-process.mjs +327 -28
- package/dist/native/registry.mjs +752 -200
- package/dist/native/resident-health.mjs +1 -0
- package/dist/native/resolve.d.mts +7 -0
- package/dist/native/resolve.mjs +119 -4
- package/dist/native/rntl-hooks.mjs +9 -5
- package/dist/native/rntl-text-host.mjs +5 -15
- package/dist/native/root-spelling.d.mts +6 -0
- package/dist/native/root-spelling.mjs +77 -0
- package/dist/native/runner.mjs +2 -19
- package/dist/native/setup.mjs +119 -38
- package/dist/native/shared-resolver.d.mts +141 -0
- package/dist/native/shared-resolver.mjs +719 -0
- package/dist/native/test-runner.mjs +26 -0
- package/dist/native/transform.mjs +18 -22
- package/dist/native/worker.mjs +9 -4
- package/dist/node-dispatch.mjs +293 -0
- package/dist/{preset-map-CogjYmSw.mjs → preset-map-DdAexIwz.mjs} +113 -2
- package/dist/{presets-CXDyg51l.mjs → presets-_EaSw29m.mjs} +710 -130
- package/dist/presets.d.mts +1 -1
- package/dist/presets.mjs +1 -1
- package/dist/rntl-matchers.d.ts +11 -15
- package/dist/rntl-support.mjs +28 -13
- package/dist/root-mocks-BNXeosbR.mjs +155 -0
- package/dist/serializer.cjs +7 -5
- package/dist/serializer.d.cts +7 -5
- package/dist/serializer.d.mts +7 -5
- package/dist/serializer.mjs +7 -5
- package/dist/setup.mjs +26 -27
- package/docs/migrating-from-jest.md +9 -232
- package/docs/migrating-from-vitest-react-native.md +4 -4
- package/docs/release-readiness.md +9 -11
- package/docs/versioning.md +38 -9
- package/package.json +59 -33
- package/dist/host-BAAAlSkO.mjs +0 -1028
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* the Vite main process and every worker reconstruct the same decision.
|
|
8
8
|
*/
|
|
9
9
|
import path from "node:path";
|
|
10
|
+
import { projectContext } from "../context.mjs";
|
|
10
11
|
import {
|
|
11
12
|
NODE_MODULES_PATH,
|
|
12
13
|
REACT_NATIVE_PATH,
|
|
@@ -14,6 +15,7 @@ import {
|
|
|
14
15
|
containsPath,
|
|
15
16
|
installedPackageDirOf,
|
|
16
17
|
memoizePackageDirOf,
|
|
18
|
+
packageDirOf as resolvePackageDir,
|
|
17
19
|
packagePatterns,
|
|
18
20
|
} from "./match.mjs";
|
|
19
21
|
|
|
@@ -115,6 +117,17 @@ function decision(owner, format, transform, reset, reasons, evidence) {
|
|
|
115
117
|
* caller that has already resolved the same packages from the same root passes its
|
|
116
118
|
* own memoized lookup (memoizePackageDirOf) so they are not resolved again.
|
|
117
119
|
*/
|
|
120
|
+
/**
|
|
121
|
+
* TypeScript source in an installed package (not a declaration file): Node's (#544).
|
|
122
|
+
* Node already compiles it for both a require (hooks.mjs, the `.ts`/`.tsx` handlers) and
|
|
123
|
+
* an import (loader.mjs, NODE_MODULES_TS, one CommonJS instance), but Vitest inlines a
|
|
124
|
+
* file Node cannot run as published, so Vite evaluated a second copy for an import.
|
|
125
|
+
* Externalizing it leaves the one copy Node compiles. The same extensions as Node's
|
|
126
|
+
* handlers: an `.mts` or `.cts` file has none, and stays Vitest's to decide.
|
|
127
|
+
*/
|
|
128
|
+
export const INSTALLED_TYPESCRIPT = /[\\/]node_modules[\\/].*(?<!\.d)\.tsx?$/;
|
|
129
|
+
const isInstalledTypeScript = (file) => INSTALLED_TYPESCRIPT.test(file.replace(/[?#].*$/, ""));
|
|
130
|
+
|
|
118
131
|
export function createNativeOwnershipPolicy({
|
|
119
132
|
projectRoot = process.cwd(),
|
|
120
133
|
explicitTransforms = [],
|
|
@@ -125,6 +138,13 @@ export function createNativeOwnershipPolicy({
|
|
|
125
138
|
reactNativeRoots = [],
|
|
126
139
|
entryFiles = [],
|
|
127
140
|
serverDepsInlineAll = false,
|
|
141
|
+
// Whether an installed package's TypeScript source is Node's (#544). Only with nodeMocks
|
|
142
|
+
// on (experimental.nodeMocks, or ownershipModel 'resolution'): there a vi.mock reaches
|
|
143
|
+
// what Node loads, so moving these files to Node keeps every mock reaching them. With
|
|
144
|
+
// it off, a package Vite inlines is the only route for a vi.mock to reach its imports,
|
|
145
|
+
// so Vitest keeps deciding. Workers read it from their context (the plugin sets
|
|
146
|
+
// `nodeMocks` there from the same condition).
|
|
147
|
+
installedTypeScriptToNode = projectContext()?.nodeMocks === true,
|
|
128
148
|
packageDirOf,
|
|
129
149
|
} = {}) {
|
|
130
150
|
const dirOf = memoizePackageDirOf(packageDirOf);
|
|
@@ -175,6 +195,7 @@ export function createNativeOwnershipPolicy({
|
|
|
175
195
|
const externalPatterns = [
|
|
176
196
|
REACT_NATIVE_PATH,
|
|
177
197
|
...reactNativeRootPatterns,
|
|
198
|
+
...(installedTypeScriptToNode ? [INSTALLED_TYPESCRIPT] : []),
|
|
178
199
|
...nodeTransformPackages.flatMap((name) => packagePatterns(name, root, dirOf)),
|
|
179
200
|
];
|
|
180
201
|
const matchesNodeTransformedFile = buildPkgMatcher(nodeTransformPackages, root, dirOf);
|
|
@@ -300,6 +321,16 @@ export function createNativeOwnershipPolicy({
|
|
|
300
321
|
: ["serialized worker transform set"],
|
|
301
322
|
);
|
|
302
323
|
}
|
|
324
|
+
if (installedTypeScriptToNode && isInstalledTypeScript(file)) {
|
|
325
|
+
return decision(
|
|
326
|
+
"node",
|
|
327
|
+
"package",
|
|
328
|
+
"native-babel",
|
|
329
|
+
runtimeResident ? "worker-resident" : "per-file",
|
|
330
|
+
["installed-typescript"],
|
|
331
|
+
["TypeScript source under node_modules, compiled by Node for require and import"],
|
|
332
|
+
);
|
|
333
|
+
}
|
|
303
334
|
if (runtimeResident) {
|
|
304
335
|
return decision(
|
|
305
336
|
"vitest-default",
|
|
@@ -426,6 +457,89 @@ export function createNativeOwnershipPolicy({
|
|
|
426
457
|
});
|
|
427
458
|
}
|
|
428
459
|
|
|
460
|
+
/**
|
|
461
|
+
* The package directories the plugin resolved at config time, as a lookup with the same
|
|
462
|
+
* signature as `packageDirOf`. A recorded answer is used only for the root it was resolved
|
|
463
|
+
* from; any other question is resolved here once and kept for the life of the lookup.
|
|
464
|
+
*
|
|
465
|
+
* Workers rebuild the ownership policy from the same package list the plugin resolved.
|
|
466
|
+
* Without the recorded answers each worker resolved every package's directory again: a
|
|
467
|
+
* few resolutions and real-path calls per package, for hundreds of packages, in every new
|
|
468
|
+
* worker (one per test file under per-file isolation).
|
|
469
|
+
*/
|
|
470
|
+
export function recordedPackageDirOf(recorded, resolve = resolvePackageDir) {
|
|
471
|
+
const lookup = memoizePackageDirOf(resolve);
|
|
472
|
+
const root = typeof recorded?.root === "string" ? path.resolve(recorded.root) : null;
|
|
473
|
+
const dirs =
|
|
474
|
+
recorded && typeof recorded.dirs === "object" && recorded.dirs !== null ? recorded.dirs : null;
|
|
475
|
+
return (name, fromDir) => {
|
|
476
|
+
if (
|
|
477
|
+
root !== null &&
|
|
478
|
+
dirs !== null &&
|
|
479
|
+
Object.hasOwn(dirs, name) &&
|
|
480
|
+
path.resolve(fromDir) === root
|
|
481
|
+
) {
|
|
482
|
+
const dir = dirs[name];
|
|
483
|
+
return typeof dir === "string" ? dir : null;
|
|
484
|
+
}
|
|
485
|
+
return lookup(name, fromDir);
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const WORKER_POLICIES = Symbol.for("vitest-native.ownership.worker-policies");
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* This worker thread's ownership policy for `inputs`, built once and shared by everything
|
|
493
|
+
* in the thread that asks with the same inputs: the ESM loader and the require hooks
|
|
494
|
+
* (each used to build its own), and every later test file a reused worker runs. The
|
|
495
|
+
* state lives on globalThis because the two callers can hold different instances of this
|
|
496
|
+
* module. Package directories come from the project context (recordedPackageDirOf).
|
|
497
|
+
*/
|
|
498
|
+
export function workerOwnershipPolicy(
|
|
499
|
+
{
|
|
500
|
+
projectRoot,
|
|
501
|
+
projectDirs = [],
|
|
502
|
+
reactNativeRoots = [],
|
|
503
|
+
serverDepsInlineAll = false,
|
|
504
|
+
runtimeTransforms = [],
|
|
505
|
+
},
|
|
506
|
+
context = projectContext(),
|
|
507
|
+
) {
|
|
508
|
+
const state = (globalThis[WORKER_POLICIES] ??= {
|
|
509
|
+
builds: 0,
|
|
510
|
+
requests: 0,
|
|
511
|
+
policies: new Map(),
|
|
512
|
+
dirOf: null,
|
|
513
|
+
});
|
|
514
|
+
state.requests++;
|
|
515
|
+
const key = JSON.stringify([
|
|
516
|
+
path.resolve(projectRoot),
|
|
517
|
+
projectDirs,
|
|
518
|
+
reactNativeRoots,
|
|
519
|
+
serverDepsInlineAll === true,
|
|
520
|
+
runtimeTransforms,
|
|
521
|
+
]);
|
|
522
|
+
const cached = state.policies.get(key);
|
|
523
|
+
if (cached) return cached;
|
|
524
|
+
state.dirOf ??= recordedPackageDirOf(context?.packageDirs);
|
|
525
|
+
const policy = createNativeOwnershipPolicy({
|
|
526
|
+
projectRoot,
|
|
527
|
+
projectDirs,
|
|
528
|
+
reactNativeRoots,
|
|
529
|
+
serverDepsInlineAll: serverDepsInlineAll === true,
|
|
530
|
+
runtimeTransforms,
|
|
531
|
+
packageDirOf: state.dirOf,
|
|
532
|
+
});
|
|
533
|
+
state.builds++;
|
|
534
|
+
state.policies.set(key, policy);
|
|
535
|
+
return policy;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/** How many policies workerOwnershipPolicy has built in this thread. */
|
|
539
|
+
export function workerOwnershipPolicyBuilds() {
|
|
540
|
+
return globalThis[WORKER_POLICIES]?.builds ?? 0;
|
|
541
|
+
}
|
|
542
|
+
|
|
429
543
|
/**
|
|
430
544
|
* Compare an actual graph observation with the owner assigned by the project.
|
|
431
545
|
* Delegated packages are intentionally inconclusive: Vitest is allowed to inline or
|
|
@@ -1,10 +1,26 @@
|
|
|
1
1
|
// Short-lived entrypoint for a cold React Native registry build.
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// compiler failure is returned structurally for the parent to report once.
|
|
3
|
+
// One JSON options object in on stdin, one JSON result out. No logging: the output is a
|
|
4
|
+
// protocol, and a failure is returned for the parent to report once.
|
|
6
5
|
import fs from "node:fs";
|
|
7
|
-
import {
|
|
6
|
+
import { Worker } from "node:worker_threads";
|
|
7
|
+
import { buildLibraryRegistry, buildRegistry } from "./registry.mjs";
|
|
8
|
+
|
|
9
|
+
// A compile runs synchronously on this thread, so this process cannot notice its parent
|
|
10
|
+
// dying, and the parent's timeout dies with the parent. A watchdog thread ends the
|
|
11
|
+
// process when it has been orphaned (POSIX reparents it, so its ppid changes) or has
|
|
12
|
+
// run longer than the parent would ever wait (registry-process.mjs gives it 120 s), so
|
|
13
|
+
// a stuck compile cannot outlive the run.
|
|
14
|
+
const ORPHAN_POLL_MS = 5_000;
|
|
15
|
+
const DEADLINE_MS = 130_000;
|
|
16
|
+
new Worker(
|
|
17
|
+
`const start = process.ppid;
|
|
18
|
+
const deadline = Date.now() + ${DEADLINE_MS};
|
|
19
|
+
setInterval(() => {
|
|
20
|
+
if (process.ppid !== start || Date.now() > deadline) process.kill(process.pid, "SIGKILL");
|
|
21
|
+
}, ${ORPHAN_POLL_MS});`,
|
|
22
|
+
{ eval: true },
|
|
23
|
+
).unref();
|
|
8
24
|
import { compileOnThisThread } from "./transform.mjs";
|
|
9
25
|
|
|
10
26
|
// No test code runs in this process, so Babel runs on its one thread (transform.mjs,
|
|
@@ -21,7 +37,8 @@ function respond(value) {
|
|
|
21
37
|
}
|
|
22
38
|
|
|
23
39
|
try {
|
|
24
|
-
const options = JSON.parse(fs.readFileSync(0, "utf8"));
|
|
40
|
+
const { testDelayMs, ...options } = JSON.parse(fs.readFileSync(0, "utf8"));
|
|
41
|
+
if (testDelayMs > 0) Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, testDelayMs);
|
|
25
42
|
const started = performance.now();
|
|
26
43
|
const registryFile = buildRegistry({
|
|
27
44
|
...options,
|
|
@@ -34,9 +51,26 @@ try {
|
|
|
34
51
|
count = JSON.parse(fs.readFileSync(`${registryFile}.json`, "utf8")).count ?? null;
|
|
35
52
|
} catch {}
|
|
36
53
|
}
|
|
54
|
+
// The library registry is layered on React Native's and built in the same child,
|
|
55
|
+
// after it. Its failure is reported apart, so it never costs React Native's.
|
|
56
|
+
let libraryRegistryFile = null;
|
|
57
|
+
let libraryError;
|
|
58
|
+
if (registryFile && (options.libraryEntries?.length ?? 0) > 0) {
|
|
59
|
+
try {
|
|
60
|
+
libraryRegistryFile = buildLibraryRegistry({
|
|
61
|
+
...options,
|
|
62
|
+
diagnostics: false,
|
|
63
|
+
failOnError: true,
|
|
64
|
+
});
|
|
65
|
+
} catch (error) {
|
|
66
|
+
libraryError = messageOf(error);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
37
69
|
respond({
|
|
38
70
|
ok: registryFile !== null,
|
|
39
71
|
registryFile,
|
|
72
|
+
libraryRegistryFile,
|
|
73
|
+
libraryError,
|
|
40
74
|
count,
|
|
41
75
|
durationMs: Math.round(performance.now() - started),
|
|
42
76
|
heapUsed: process.memoryUsage().heapUsed,
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
// Parent-side controller for bounded cold registry compilation.
|
|
2
|
+
import fs from "node:fs";
|
|
2
3
|
import { fileURLToPath } from "node:url";
|
|
3
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
_warnRegistryUnavailable,
|
|
6
|
+
buildLibraryRegistry,
|
|
7
|
+
buildRegistry,
|
|
8
|
+
registryLocation,
|
|
9
|
+
} from "./registry.mjs";
|
|
4
10
|
import { runBoundedProcess } from "./bounded-process.mjs";
|
|
5
11
|
|
|
6
12
|
const INITIAL_HEAP_MB = 96;
|
|
@@ -16,6 +22,9 @@ export async function compileRegistryInChild(
|
|
|
16
22
|
retryHeapMb = RETRY_HEAP_MB,
|
|
17
23
|
timeoutMs = DEFAULT_TIMEOUT_MS,
|
|
18
24
|
spawnProcess,
|
|
25
|
+
signal,
|
|
26
|
+
priority,
|
|
27
|
+
unref,
|
|
19
28
|
} = {},
|
|
20
29
|
) {
|
|
21
30
|
const result = await runBoundedProcess(compilerFile, options, {
|
|
@@ -23,6 +32,9 @@ export async function compileRegistryInChild(
|
|
|
23
32
|
retryHeapMb,
|
|
24
33
|
timeoutMs,
|
|
25
34
|
spawnProcess,
|
|
35
|
+
signal,
|
|
36
|
+
priority,
|
|
37
|
+
unref,
|
|
26
38
|
label: "compiler",
|
|
27
39
|
});
|
|
28
40
|
if (result.ok && typeof result.registryFile !== "string") {
|
|
@@ -43,53 +55,334 @@ export const registryCompilerDefaults = Object.freeze({
|
|
|
43
55
|
timeoutMs: DEFAULT_TIMEOUT_MS,
|
|
44
56
|
});
|
|
45
57
|
|
|
58
|
+
const COMPILER_FILE = () => fileURLToPath(new URL("./registry-compiler.mjs", import.meta.url));
|
|
59
|
+
|
|
60
|
+
// Test seam: `wait` (as registryBuild: 'wait'); `compilerDelayMs` holds the compiler.
|
|
61
|
+
let seams = {};
|
|
62
|
+
export function _setRegistryBuildOptions(options = {}) {
|
|
63
|
+
seams = options;
|
|
64
|
+
}
|
|
65
|
+
export function registryWaitForced() {
|
|
66
|
+
return seams.wait === true;
|
|
67
|
+
}
|
|
68
|
+
function compilerInput(options) {
|
|
69
|
+
return seams.compilerDelayMs > 0 ? { ...options, testDelayMs: seams.compilerDelayMs } : options;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The compiler's priority when it runs beside the tests: the lowest, so it takes only
|
|
74
|
+
* CPU the workers leave idle and never slows the run it is building for.
|
|
75
|
+
*/
|
|
76
|
+
const BACKGROUND_PRIORITY = 19;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Background builds in flight in this process, by registry file: every project and
|
|
80
|
+
* every config reload that misses the same cache entry joins one compiler.
|
|
81
|
+
*/
|
|
82
|
+
const inFlight = new Map();
|
|
83
|
+
let exitHookInstalled = false;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Signals that end this process while a build runs. Vitest handles SIGINT and SIGTERM
|
|
87
|
+
* itself during a run (and turns them into process.exit, which the exit hook covers),
|
|
88
|
+
* but removes its handlers when it starts to close, and a signal while close waits for
|
|
89
|
+
* the build would otherwise kill this process and leave the compiler running.
|
|
90
|
+
*/
|
|
91
|
+
const STOP_SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
92
|
+
let signalHandlersInstalled = false;
|
|
93
|
+
|
|
94
|
+
function onStopSignal(signal) {
|
|
95
|
+
// Installed first (prependListener), so this sees whether anything else will handle
|
|
96
|
+
// the signal: Vitest during a run, whose own handler then ends the process gracefully.
|
|
97
|
+
const handledElsewhere = process.listenerCount(signal) > 1;
|
|
98
|
+
for (const running of inFlight.values()) running.cancel();
|
|
99
|
+
removeSignalHandlers();
|
|
100
|
+
// Otherwise the signal does what it would have done without this handler.
|
|
101
|
+
if (!handledElsewhere) process.kill(process.pid, signal);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function installSignalHandlers() {
|
|
105
|
+
if (signalHandlersInstalled || process.platform === "win32") return;
|
|
106
|
+
signalHandlersInstalled = true;
|
|
107
|
+
for (const signal of STOP_SIGNALS) process.prependListener(signal, onStopSignal);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function removeSignalHandlers() {
|
|
111
|
+
if (!signalHandlersInstalled) return;
|
|
112
|
+
signalHandlersInstalled = false;
|
|
113
|
+
for (const signal of STOP_SIGNALS) process.off(signal, onStopSignal);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Resolve a warm registry, or start compiling a cache miss without waiting for it.
|
|
118
|
+
*
|
|
119
|
+
* Loading React Native file by file runs the same modules with the same semantics, so
|
|
120
|
+
* a cold run can start at once and use the registry when it lands; the plugin still
|
|
121
|
+
* waits under the hot runtime, on CI and with registryBuild: 'wait'. Rejected: a
|
|
122
|
+
* detached child (it outlives the run), building in teardown (slower than building
|
|
123
|
+
* first), and building only for the next run (a large cold suite would load React
|
|
124
|
+
* Native file by file throughout, which costs more than the build past roughly thirty
|
|
125
|
+
* files per worker). So workers pick it up mid-run (activeRegistry in context.mjs).
|
|
126
|
+
*
|
|
127
|
+
* Returns `{ registry, libraryRegistry }` on a hit; on a miss `{ pending: { file, since } }`,
|
|
128
|
+
* `since` being the metadata's mtime before the lookup (0 when absent); `{}` when no
|
|
129
|
+
* registry can be had. settleRegistryBuilds, a stop signal or this process exiting ends it.
|
|
130
|
+
*
|
|
131
|
+
* The library registry (`libraryEntries`) compiles in the same child, after React
|
|
132
|
+
* Native's. A run picks up React Native's registry mid-run, and the library registry
|
|
133
|
+
* from the next run: until then those libraries load file by file, as they do when it
|
|
134
|
+
* cannot be built. When React Native's registry is warm and only the library registry
|
|
135
|
+
* is missing, it returns `{ wait: true }`: that build is a fraction of React Native's,
|
|
136
|
+
* so the caller builds it first (buildRegistryFor) rather than run without it.
|
|
137
|
+
*/
|
|
138
|
+
export function startRegistryBuild(options, compiler = compileRegistryInChild) {
|
|
139
|
+
try {
|
|
140
|
+
if (process.env.VITEST_NATIVE_NO_REGISTRY === "1") {
|
|
141
|
+
buildRegistry(options);
|
|
142
|
+
return {};
|
|
143
|
+
}
|
|
144
|
+
const location = registryLocation(options);
|
|
145
|
+
const { registryFile, metaFile } = location;
|
|
146
|
+
const joined = inFlight.get(registryFile);
|
|
147
|
+
if (joined) return { pending: joined.pending };
|
|
148
|
+
// Before the lookup, so a registry published in between still differs from `since`.
|
|
149
|
+
const since = fs.statSync(metaFile, { throwIfNoEntry: false })?.mtimeMs ?? 0;
|
|
150
|
+
const warm = cachedRegistries(options, location);
|
|
151
|
+
if (warm.complete) {
|
|
152
|
+
if (warm.libraryError) _warnRegistryUnavailable(warm.libraryError);
|
|
153
|
+
return { registry: warm.registry, libraryRegistry: warm.libraryRegistry };
|
|
154
|
+
}
|
|
155
|
+
if (warm.registry) return { wait: true };
|
|
156
|
+
|
|
157
|
+
const controller = new AbortController();
|
|
158
|
+
const started = performance.now();
|
|
159
|
+
const done = (async () => {
|
|
160
|
+
try {
|
|
161
|
+
const result = await compiler(COMPILER_FILE(), compilerInput(options), {
|
|
162
|
+
signal: controller.signal,
|
|
163
|
+
priority: BACKGROUND_PRIORITY,
|
|
164
|
+
// Never what keeps a finished run alive; settleRegistryBuilds waits for it
|
|
165
|
+
// explicitly, and the exit hook stops it.
|
|
166
|
+
unref: true,
|
|
167
|
+
});
|
|
168
|
+
// A concurrent run's compiler may have published it instead, visible a moment
|
|
169
|
+
// later where rename cannot replace a file (Windows), as in buildRegistryFor.
|
|
170
|
+
let built = cachedRegistries(options, location);
|
|
171
|
+
if (!built.complete && !result.ok && !result.cancelled) {
|
|
172
|
+
for (const delayMs of [10, 20, 40, 80]) {
|
|
173
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
174
|
+
built = cachedRegistries(options, location);
|
|
175
|
+
if (built.complete) break;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (built.registry) {
|
|
179
|
+
if (options.diagnostics) {
|
|
180
|
+
console.log(
|
|
181
|
+
`[vitest-native] (native) precompiled RN registry in the background ` +
|
|
182
|
+
`(${Math.round(performance.now() - started)}ms); test files that start ` +
|
|
183
|
+
`from now on use it`,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
if (built.libraryError) _warnRegistryUnavailable(built.libraryError);
|
|
187
|
+
else if (!built.complete && !result.cancelled) {
|
|
188
|
+
_warnRegistryUnavailable(
|
|
189
|
+
`library registry: ${result.libraryError ?? result.error ?? "bounded compiler completed without a usable cache entry"}`,
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
return { registry: built.registry, libraryRegistry: built.libraryRegistry };
|
|
193
|
+
}
|
|
194
|
+
if (!result.cancelled) {
|
|
195
|
+
_warnRegistryUnavailable(
|
|
196
|
+
result.error ?? "bounded compiler completed without a usable cache entry",
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
return null;
|
|
200
|
+
} catch (error) {
|
|
201
|
+
_warnRegistryUnavailable(error?.message ?? error);
|
|
202
|
+
return null;
|
|
203
|
+
} finally {
|
|
204
|
+
inFlight.delete(registryFile);
|
|
205
|
+
if (inFlight.size === 0) removeSignalHandlers();
|
|
206
|
+
}
|
|
207
|
+
})();
|
|
208
|
+
const build = {
|
|
209
|
+
pending: { file: registryFile, since },
|
|
210
|
+
done,
|
|
211
|
+
cancel: () => controller.abort(),
|
|
212
|
+
};
|
|
213
|
+
inFlight.set(registryFile, build);
|
|
214
|
+
installSignalHandlers();
|
|
215
|
+
if (!exitHookInstalled) {
|
|
216
|
+
exitHookInstalled = true;
|
|
217
|
+
// Whatever ends this process (a signal Vitest turns into process.exit, an
|
|
218
|
+
// uncaught error) takes the compiler with it: nothing outlives the run.
|
|
219
|
+
process.once("exit", () => {
|
|
220
|
+
for (const running of inFlight.values()) running.cancel();
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
if (options.diagnostics) {
|
|
224
|
+
console.log(
|
|
225
|
+
`[vitest-native] (native) no precompiled RN registry for this graph yet; compiling ` +
|
|
226
|
+
`it in the background while test files load React Native file by file`,
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
return { pending: build.pending };
|
|
230
|
+
} catch (error) {
|
|
231
|
+
_warnRegistryUnavailable(error?.message ?? error);
|
|
232
|
+
return {};
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Called when Vitest closes. A registry still compiling gets up to `budgetMs` to
|
|
238
|
+
* finish, so the next run starts warm, and is stopped after that. It is stopped at once
|
|
239
|
+
* when the last run ended cancelled (`--bail`, Ctrl-C in a terminal); a watch-mode
|
|
240
|
+
* cancel does not close, so the build keeps running for the reruns.
|
|
241
|
+
* Resolves once every compiler has exited.
|
|
242
|
+
*/
|
|
243
|
+
export async function settleRegistryBuilds(budgetMs, { cancelled = false } = {}) {
|
|
244
|
+
const builds = [...inFlight.values()];
|
|
245
|
+
if (builds.length === 0) return;
|
|
246
|
+
const waitMs = cancelled ? 0 : Math.max(0, budgetMs);
|
|
247
|
+
if (waitMs > 0) {
|
|
248
|
+
let timer;
|
|
249
|
+
await Promise.race([
|
|
250
|
+
Promise.all(builds.map((build) => build.done)),
|
|
251
|
+
new Promise((resolve) => {
|
|
252
|
+
timer = setTimeout(resolve, waitMs);
|
|
253
|
+
}),
|
|
254
|
+
]);
|
|
255
|
+
clearTimeout(timer);
|
|
256
|
+
}
|
|
257
|
+
// Only what is still running: a finished build has left inFlight.
|
|
258
|
+
for (const build of builds) if (inFlight.get(build.pending.file) === build) build.cancel();
|
|
259
|
+
// The compilers are unref'd; hold the process open until they have gone, so the rest
|
|
260
|
+
// of Vitest's close still runs.
|
|
261
|
+
const keepAlive = setTimeout(() => {}, 10_000);
|
|
262
|
+
await Promise.all(builds.map((build) => build.done));
|
|
263
|
+
clearTimeout(keepAlive);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* A CI run: `CI` set to anything but "false", or, unless `CI=false`, one of these
|
|
268
|
+
* providers' own variables set. Only decides whether a cold run waits for the registry.
|
|
269
|
+
*/
|
|
270
|
+
export const CI_VARIABLES = Object.freeze([
|
|
271
|
+
"CI",
|
|
272
|
+
"CONTINUOUS_INTEGRATION",
|
|
273
|
+
"GITHUB_ACTIONS",
|
|
274
|
+
"GITLAB_CI",
|
|
275
|
+
"BUILDKITE",
|
|
276
|
+
"CIRCLECI",
|
|
277
|
+
"TF_BUILD",
|
|
278
|
+
"JENKINS_URL",
|
|
279
|
+
"TEAMCITY_VERSION",
|
|
280
|
+
"TRAVIS",
|
|
281
|
+
"APPVEYOR",
|
|
282
|
+
"CODEBUILD_BUILD_ID",
|
|
283
|
+
"BITBUCKET_BUILD_NUMBER",
|
|
284
|
+
]);
|
|
285
|
+
|
|
286
|
+
export function isCI(env = process.env) {
|
|
287
|
+
if (env.CI === "false") return false;
|
|
288
|
+
return CI_VARIABLES.some((name) => env[name] !== undefined && env[name] !== "");
|
|
289
|
+
}
|
|
290
|
+
|
|
46
291
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
292
|
+
* The published registries for `options`, read through the cache contract every
|
|
293
|
+
* reader uses (key + manifest), without building anything. `complete` is false when
|
|
294
|
+
* React Native's registry or a requested library registry is missing.
|
|
295
|
+
*/
|
|
296
|
+
function cachedRegistries(options, location) {
|
|
297
|
+
const registry = buildRegistry({
|
|
298
|
+
...options,
|
|
299
|
+
location,
|
|
300
|
+
cacheOnly: true,
|
|
301
|
+
failOnError: true,
|
|
302
|
+
diagnostics: false,
|
|
303
|
+
});
|
|
304
|
+
if (!registry) return { registry: null, libraryRegistry: null, complete: false };
|
|
305
|
+
const wantsLibrary = (options.libraryEntries?.length ?? 0) > 0;
|
|
306
|
+
if (!wantsLibrary) return { registry, libraryRegistry: null, complete: true };
|
|
307
|
+
try {
|
|
308
|
+
const libraryRegistry = buildLibraryRegistry({
|
|
309
|
+
...options,
|
|
310
|
+
cacheOnly: true,
|
|
311
|
+
failOnError: true,
|
|
312
|
+
diagnostics: false,
|
|
313
|
+
});
|
|
314
|
+
return { registry, libraryRegistry, complete: libraryRegistry !== null };
|
|
315
|
+
} catch (error) {
|
|
316
|
+
// Entries that do not resolve, or a build of these inputs that failed before: a
|
|
317
|
+
// build would fail the same way. React Native's registry stays in use.
|
|
318
|
+
const reason = String(error?.message ?? error).replace(/^\[vitest-native\] /, "");
|
|
319
|
+
return {
|
|
320
|
+
registry,
|
|
321
|
+
libraryRegistry: null,
|
|
322
|
+
complete: true,
|
|
323
|
+
libraryError: `library registry: ${reason}`,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Resolve warm registries in the parent, or compile a cache miss in the bounded
|
|
330
|
+
* child: React Native's registry and, when `libraryEntries` names any, the library
|
|
331
|
+
* registry layered on it — one build lifecycle for both. Returns
|
|
332
|
+
* `{ registry, libraryRegistry }`, or null without a React Native registry. A
|
|
333
|
+
* library registry that cannot be built leaves `libraryRegistry` null and React
|
|
334
|
+
* Native's registry in use. The optional compiler is a test seam proving warm
|
|
335
|
+
* production lookups do not spawn; ordinary callers always use
|
|
336
|
+
* compileRegistryInChild.
|
|
50
337
|
*/
|
|
51
338
|
export async function buildRegistryFor(options, compiler = compileRegistryInChild) {
|
|
52
339
|
try {
|
|
53
340
|
// Preserve the explicit escape hatch's existing diagnostic and silence
|
|
54
341
|
// contracts, and avoid launching a process for work the user disabled.
|
|
55
342
|
if (process.env.VITEST_NATIVE_NO_REGISTRY === "1") {
|
|
56
|
-
|
|
343
|
+
buildRegistry(options);
|
|
344
|
+
return null;
|
|
57
345
|
}
|
|
58
346
|
|
|
59
|
-
// A warm run is just the cache
|
|
347
|
+
// A warm run is just the cache keys + manifest stats in this process. In
|
|
60
348
|
// particular, it never initializes Babel and never launches a child.
|
|
61
|
-
const
|
|
62
|
-
|
|
349
|
+
const location = registryLocation(options);
|
|
350
|
+
const warm = cachedRegistries(options, location);
|
|
351
|
+
if (warm.complete) {
|
|
352
|
+
if (warm.libraryError) _warnRegistryUnavailable(warm.libraryError);
|
|
353
|
+
return { registry: warm.registry, libraryRegistry: warm.libraryRegistry };
|
|
354
|
+
}
|
|
63
355
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
356
|
+
// A background build of the same entry is already running in this process (another
|
|
357
|
+
// project in the run, or a config reload): wait for it instead of compiling twice.
|
|
358
|
+
// It was started for its own project's library entries, so read the cache for these
|
|
359
|
+
// options afterwards, and compile what is still missing.
|
|
360
|
+
const joined = inFlight.get(location.registryFile);
|
|
361
|
+
if (joined) {
|
|
362
|
+
await joined.done;
|
|
363
|
+
const after = cachedRegistries(options, location);
|
|
364
|
+
if (after.complete) {
|
|
365
|
+
if (after.libraryError) _warnRegistryUnavailable(after.libraryError);
|
|
366
|
+
return { registry: after.registry, libraryRegistry: after.libraryRegistry };
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const result = await compiler(COMPILER_FILE(), compilerInput(options));
|
|
68
371
|
|
|
69
372
|
// Validate through the same cache contract every reader uses instead of
|
|
70
373
|
// trusting a path returned over the child protocol. This also turns a losing
|
|
71
374
|
// compiler in a concurrent cold-cache race into success once the winner's
|
|
72
375
|
// atomic metadata write becomes visible (notably on Windows, where rename
|
|
73
376
|
// cannot replace an existing target).
|
|
74
|
-
let
|
|
75
|
-
|
|
76
|
-
cacheOnly: true,
|
|
77
|
-
failOnError: true,
|
|
78
|
-
diagnostics: false,
|
|
79
|
-
});
|
|
80
|
-
if (!registryFile && !result.ok) {
|
|
377
|
+
let built = cachedRegistries(options, location);
|
|
378
|
+
if (!built.complete && !result.ok) {
|
|
81
379
|
for (const delayMs of [10, 20, 40, 80]) {
|
|
82
380
|
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
cacheOnly: true,
|
|
86
|
-
failOnError: true,
|
|
87
|
-
diagnostics: false,
|
|
88
|
-
});
|
|
89
|
-
if (registryFile) break;
|
|
381
|
+
built = cachedRegistries(options, location);
|
|
382
|
+
if (built.complete) break;
|
|
90
383
|
}
|
|
91
384
|
}
|
|
92
|
-
if (
|
|
385
|
+
if (built.registry) {
|
|
93
386
|
if (options.diagnostics) {
|
|
94
387
|
if (result.ok) {
|
|
95
388
|
const count = typeof result.count === "number" ? `${result.count} modules, ` : "";
|
|
@@ -110,7 +403,13 @@ export async function buildRegistryFor(options, compiler = compileRegistryInChil
|
|
|
110
403
|
);
|
|
111
404
|
}
|
|
112
405
|
}
|
|
113
|
-
|
|
406
|
+
if (built.libraryError) _warnRegistryUnavailable(built.libraryError);
|
|
407
|
+
else if (!built.complete) {
|
|
408
|
+
_warnRegistryUnavailable(
|
|
409
|
+
`library registry: ${result.libraryError ?? result.error ?? "bounded compiler completed without a usable cache entry"}`,
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
return { registry: built.registry, libraryRegistry: built.libraryRegistry };
|
|
114
413
|
}
|
|
115
414
|
|
|
116
415
|
_warnRegistryUnavailable(
|