vitest-native 0.14.0-rc.10 → 0.14.0-rc.11
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 +3 -2
- package/dist/cli.mjs +77 -5
- package/dist/helpers.cjs +53 -2
- package/dist/{host-DCPzgC3D.mjs → host-C3wt9zUE.mjs} +222 -35
- package/dist/index.mjs +83 -107
- package/dist/jest-compat/node-registry.mjs +154 -34
- package/dist/jest-compat/setup.mjs +7 -3
- package/dist/jest-compat.cjs +222 -47
- package/dist/jest-compat.mjs +198 -47
- package/dist/native/assets.d.mts +2 -0
- package/dist/native/assets.mjs +34 -0
- package/dist/native/babel-toolchain.mjs +64 -0
- package/dist/native/babel-worker.mjs +53 -0
- package/dist/native/esm-interop.mjs +216 -0
- package/dist/native/hooks.mjs +2 -11
- package/dist/native/match.d.mts +1 -1
- package/dist/native/match.mjs +17 -7
- package/dist/native/metro-profile-compiler.mjs +2 -31
- package/dist/native/registry-compiler.mjs +6 -0
- package/dist/native/registry.mjs +63 -75
- package/dist/native/resolve.d.mts +6 -0
- package/dist/native/resolve.mjs +100 -9
- package/dist/native/transform.mjs +287 -59
- package/dist/{preset-map-BlBriTA0.mjs → preset-map-L1-vD19T.mjs} +34 -16
- package/dist/setup.mjs +28 -23
- package/docs/migrating-from-jest.md +10 -2
- package/package.json +17 -18
- package/CHANGELOG.md +0 -2617
- package/dist/cli.cjs +0 -1993
- package/dist/cli.d.cts +0 -3
- package/dist/errors-B3lJK46O.cjs +0 -75
- package/dist/host-D3wzmsw8.cjs +0 -1296
- package/dist/index-D5cOy_iY.d.cts +0 -487
- package/dist/memory-bBnzadf6.cjs +0 -155
- package/dist/pool-DaTJRKJb.cjs +0 -127
- package/dist/preset-map-jSFsNy1Q.cjs +0 -241
- package/dist/preset-modules-C7gu3bPn.cjs +0 -48
- package/dist/presets-DH7fC0YV.cjs +0 -3944
- package/dist/rolldown-runtime-C0BPl7ul.cjs +0 -43
- package/dist/worker-vitest-BfiQB8ND.cjs +0 -62
- package/docs/hot-default-design.md +0 -222
package/README.md
CHANGED
|
@@ -589,8 +589,9 @@ JavaScript runs for real — `expo` itself, expo-router, React Navigation and Ex
|
|
|
589
589
|
jest-expo: from the package's own `mocks/<Name>.ts` where it ships one, else from Expo's table
|
|
590
590
|
of the native modules (`Device.modelName` is `'mock'`). An optional native module a package
|
|
591
591
|
probes for, and that no installed package defines, is absent, as on a device.
|
|
592
|
-
- **Expo SDK
|
|
593
|
-
|
|
592
|
+
- **Expo SDK 57 and 58 are supported.** Earlier SDKs are not: the plugin and `doctor` warn about
|
|
593
|
+
one, so upgrade to SDK 57 or 58. Pin your `react-native` to the version your Expo SDK
|
|
594
|
+
supports, not the newest RN release.
|
|
594
595
|
|
|
595
596
|
Packed Expo SDK 58 and SDK 57 apps are installed from the release tarball and tested in CI: Expo
|
|
596
597
|
module auto-detection and rendering, and expo-router's testing library against file-based `app/`
|
package/dist/cli.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
import { i as autoDetectedPresetApplies, n as DEFAULT_ASSET_EXTS, o as presetShadowing, r as PRESET_MODULES, s as testsItself, t as AUTO_DETECT_PRESETS } from "./preset-map-
|
|
2
|
+
import { C as validatePeerDependency, _ as fsModuleCacheOnByDefault, b as installedExpoVersion, c as detectEngine, i as ownVitestPackage, l as resolveTransformPreset, o as usesVitePlus, p as formatNativeOwnershipManifest, r as installedViteVersion, s as detectEcosystemPackages, u as createNativeOwnershipPolicy, x as unsupportedExpoSdkMessage, y as PEER_REQUIREMENTS } from "./host-C3wt9zUE.mjs";
|
|
3
|
+
import { i as autoDetectedPresetApplies, n as DEFAULT_ASSET_EXTS, o as presetShadowing, r as PRESET_MODULES, s as testsItself, t as AUTO_DETECT_PRESETS } from "./preset-map-L1-vD19T.mjs";
|
|
4
4
|
import { n as formatHotMemoryPlan, t as createHotMemoryPlan } from "./memory-BFxB285Z.mjs";
|
|
5
5
|
import { createRequire } from "node:module";
|
|
6
6
|
import fs from "node:fs";
|
|
@@ -422,6 +422,67 @@ function findConfigFile(root) {
|
|
|
422
422
|
function withoutComments(source) {
|
|
423
423
|
return source.replace(/\/\*[\s\S]*?\*\//g, " ").replace(/(^|[^:])\/\/[^\n]*/g, "$1");
|
|
424
424
|
}
|
|
425
|
+
const settingOf = (value) => value === void 0 ? null : value === "false" ? "off" : "set";
|
|
426
|
+
/**
|
|
427
|
+
* How a config sets Vitest's `fsModuleCache`, read as text, in each spelling: inside
|
|
428
|
+
* `experimental: { ... }` (Vitest 4.0.11 and later) and anywhere else (the top-level
|
|
429
|
+
* option, Vitest 5). `off` for a literal `false`, `set` for any other value, null when
|
|
430
|
+
* that spelling is absent.
|
|
431
|
+
*/
|
|
432
|
+
function fsModuleCacheSetting(source) {
|
|
433
|
+
const code = withoutComments(source);
|
|
434
|
+
const nested = /\bexperimental\s*:\s*\{[^{}]*?\bfsModuleCache\s*:\s*([^,}\s]*)/.exec(code);
|
|
435
|
+
const rest = nested ? code.replace(nested[0], " ") : code;
|
|
436
|
+
const top = /\bfsModuleCache\s*:\s*([^,}\s]*)/.exec(rest);
|
|
437
|
+
return {
|
|
438
|
+
experimental: settingOf(nested?.[1]),
|
|
439
|
+
topLevel: settingOf(top?.[1])
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
/** Whether `version` is at or above `[major, minor, patch]`. */
|
|
443
|
+
function versionAtLeast(version, floor) {
|
|
444
|
+
const parts = version.replace(/^[^0-9]*/, "").split(/[.-]/).slice(0, 3).map(Number);
|
|
445
|
+
for (let i = 0; i < 3; i++) {
|
|
446
|
+
const part = parts[i] ?? 0;
|
|
447
|
+
if (part !== floor[i]) return part > floor[i];
|
|
448
|
+
}
|
|
449
|
+
return true;
|
|
450
|
+
}
|
|
451
|
+
/** The first Vitest with `experimental.fsModuleCache`. */
|
|
452
|
+
const EXPERIMENTAL_FS_MODULE_CACHE_FROM = [
|
|
453
|
+
4,
|
|
454
|
+
0,
|
|
455
|
+
11
|
|
456
|
+
];
|
|
457
|
+
const TROUBLESHOOTING_STALE_IMPORT = "https://danfry1.github.io/vitest-native/guide/troubleshooting#cannot-find-module-keeps-failing-after-the-missing-file-exists";
|
|
458
|
+
/**
|
|
459
|
+
* The transform-cache note for a Vitest the plugin does not turn the cache on for.
|
|
460
|
+
* Empty where it does (fsModuleCacheOnByDefault, which the plugin reads too), and where
|
|
461
|
+
* the config turns the cache off on purpose.
|
|
462
|
+
*/
|
|
463
|
+
function transformCacheAdvice(vitestVersion, configSource) {
|
|
464
|
+
const major = Number(vitestVersion?.replace(/^[^0-9]*/, "").split(".")[0]);
|
|
465
|
+
if (!vitestVersion || !(major >= 4) || fsModuleCacheOnByDefault(major)) return [];
|
|
466
|
+
const setting = configSource === null ? {
|
|
467
|
+
experimental: null,
|
|
468
|
+
topLevel: null
|
|
469
|
+
} : fsModuleCacheSetting(configSource);
|
|
470
|
+
if (setting.experimental === "off") return [];
|
|
471
|
+
const hasOption = versionAtLeast(vitestVersion, EXPERIMENTAL_FS_MODULE_CACHE_FROM);
|
|
472
|
+
const caveat = `A Vitest bug can keep a "Cannot find module" failure cached after the file exists (reproduced on Vitest 5.0.5, unverified on 4); ${TROUBLESHOOTING_STALE_IMPORT} lists when it happens and how to recover.`;
|
|
473
|
+
if (setting.experimental === "set" && hasOption) return [`Vitest ${vitestVersion}: the config sets \`experimental.fsModuleCache\`; while it is on, transformed modules are kept on disk between runs. ${caveat}`];
|
|
474
|
+
const lines = [`Vitest ${vitestVersion} re-transforms every imported module on each run, about half a second per thousand imported modules on one benchmark. Vitest 5 keeps transformed modules on disk between runs, and vitest-native turns that on.`];
|
|
475
|
+
if (setting.topLevel !== null) lines.push("The config sets `test.fsModuleCache`, the Vitest 5 spelling, which has no effect on Vitest 4.");
|
|
476
|
+
lines.push(hasOption ? `To keep them on disk on Vitest 4, set \`test.experimental.fsModuleCache: true\`. ${caveat}` : `Vitest 4.0.11 adds \`test.experimental.fsModuleCache\` to keep them on disk.`);
|
|
477
|
+
return lines;
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* The Vitest that runs here, as the peer check reports it: the project's own, or under
|
|
481
|
+
* Vite+ the copy this package loads.
|
|
482
|
+
*/
|
|
483
|
+
function runningVitestVersion(from) {
|
|
484
|
+
return packageVersion(from, "vitest") ?? (usesVitePlus(from) ? ownVitestPackage()?.version ?? null : null);
|
|
485
|
+
}
|
|
425
486
|
/**
|
|
426
487
|
* Whether a config is built on one imported from elsewhere — a shared preset in a
|
|
427
488
|
* workspace, typically. Such a file legitimately never mentions vitest-native, and
|
|
@@ -540,7 +601,7 @@ function runDoctor(root, nodeVersion = process.versions.node) {
|
|
|
540
601
|
for (const { name, minimum, maximumMajor, minimumByMajor, optional } of PEER_REQUIREMENTS) {
|
|
541
602
|
if (optional) continue;
|
|
542
603
|
const from = rootFor(name);
|
|
543
|
-
const running = name === "vite" ? installedViteVersion(from) : name === "vitest" ?
|
|
604
|
+
const running = name === "vite" ? installedViteVersion(from) : name === "vitest" ? runningVitestVersion(from) : null;
|
|
544
605
|
const error = validatePeerDependency(name, minimum, from, maximumMajor, minimumByMajor, running);
|
|
545
606
|
if (error) fail(error);
|
|
546
607
|
else {
|
|
@@ -600,8 +661,12 @@ function runDoctor(root, nodeVersion = process.versions.node) {
|
|
|
600
661
|
}
|
|
601
662
|
if (detected.length) for (const d of detected) pass(d);
|
|
602
663
|
else if (!installedAny) lines.push(" (none — no preset-covered packages installed)");
|
|
603
|
-
const expo =
|
|
604
|
-
|
|
664
|
+
const expo = installedExpoVersion(root) ?? (configRoot === root ? null : installedExpoVersion(configRoot));
|
|
665
|
+
const unsupported = expo && unsupportedExpoSdkMessage(expo);
|
|
666
|
+
if (unsupported) {
|
|
667
|
+
lines.push("", "Expo");
|
|
668
|
+
warn(unsupported);
|
|
669
|
+
} else if (expo) {
|
|
605
670
|
lines.push("", "Expo");
|
|
606
671
|
pass((decision.engine === "native" ? `expo ${expo} detected. Under the native engine Expo modules run their real JavaScript on native-module mocks, as under jest-expo (unless the config sets \`engine: 'mock'\` or \`presets: { expo: true }\`, where the expo preset shadows ${PRESET_MODULES.expo.join(", ")}). ` : `expo ${expo} detected. Under the mock engine the expo preset shadows ${PRESET_MODULES.expo.join(", ")}; other Expo modules load their real JavaScript. `) + `Coming from jest-expo, run \`vitest-native migrate\`. See https://danfry1.github.io/vitest-native/guide/expo`);
|
|
607
672
|
}
|
|
@@ -619,15 +684,22 @@ function runDoctor(root, nodeVersion = process.versions.node) {
|
|
|
619
684
|
}
|
|
620
685
|
lines.push("", "Config");
|
|
621
686
|
const configFile = findConfigFile(configRoot);
|
|
687
|
+
let configSource = null;
|
|
622
688
|
if (!configFile) warn("no vitest.config.* found — run `vitest-native init` to create one.");
|
|
623
689
|
else {
|
|
624
690
|
const content = fs.readFileSync(path.join(configRoot, configFile), "utf8");
|
|
691
|
+
configSource = content;
|
|
625
692
|
const usage = analyzeConfigUsage(content);
|
|
626
693
|
if (usage.imports && usage.invokes) pass(`${configFile} uses vitest-native.`);
|
|
627
694
|
else if (usage.imports) warn(`${configFile} imports vitest-native but never calls reactNative() — the plugin is not in \`plugins: [...]\`, so React Native imports will not resolve.`);
|
|
628
695
|
else if (extendsSharedConfig(content)) lines.push(` · ${configFile} builds on a shared config, so this cannot tell from here whether the plugin is wired up. Check the config it extends.`);
|
|
629
696
|
else warn(`${configFile} exists but does not import vitest-native` + (content.includes("vitest-native") ? " (it is only mentioned in a comment)" : "") + " — add `reactNative()` to `plugins: [...]`, or run `vitest-native init`.");
|
|
630
697
|
}
|
|
698
|
+
const advice = transformCacheAdvice(runningVitestVersion(rootFor("vitest")), configSource);
|
|
699
|
+
if (advice.length) {
|
|
700
|
+
lines.push("", "Transform cache");
|
|
701
|
+
for (const line of advice) lines.push(` · ${line}`);
|
|
702
|
+
}
|
|
631
703
|
lines.push("", ok ? "✓ No blocking problems found." : "✗ Blocking problems found (see ✗ above).");
|
|
632
704
|
return {
|
|
633
705
|
lines,
|
package/dist/helpers.cjs
CHANGED
|
@@ -1,5 +1,56 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
|
|
2
|
+
//#region src/errors.mjs
|
|
3
|
+
/**
|
|
4
|
+
* The error types this package throws.
|
|
5
|
+
*
|
|
6
|
+
* Plain .mjs on purpose. The shipped native and jest-compat runtimes are copied to dist
|
|
7
|
+
* verbatim and import this by relative path, and the test suite loads those same files
|
|
8
|
+
* from src — so the module has to resolve identically in both trees. A .ts source would
|
|
9
|
+
* only exist in one of them.
|
|
10
|
+
*
|
|
11
|
+
* WHAT SURVIVES WHERE, measured rather than assumed:
|
|
12
|
+
*
|
|
13
|
+
* - In the main process, and in a user's own try/catch inside a test, everything is
|
|
14
|
+
* intact.
|
|
15
|
+
* - Across the worker/reporter boundary Vitest keeps only `name`, `message` and
|
|
16
|
+
* `stack`. `code` and every other own property is dropped.
|
|
17
|
+
*
|
|
18
|
+
* So the message must stay self-sufficient: nothing may move out of it into a field on
|
|
19
|
+
* the assumption a reader will see it.
|
|
20
|
+
*
|
|
21
|
+
* `isVitestNativeError` deliberately checks SHAPE rather than `instanceof`. Bundled
|
|
22
|
+
* entries and the verbatim .mjs runtimes can hold separate copies of this module, and a
|
|
23
|
+
* prototype check would quietly fail across that seam. Shape does not care.
|
|
24
|
+
*/
|
|
25
|
+
const PREFIX = "[vitest-native]";
|
|
26
|
+
/** Guarantees the prefix exactly once, however the caller wrote the message. */
|
|
27
|
+
function withPrefix(message) {
|
|
28
|
+
return message.startsWith(PREFIX) ? message : `${PREFIX} ${message}`;
|
|
29
|
+
}
|
|
30
|
+
function buildMessage(message, options) {
|
|
31
|
+
const base = withPrefix(message);
|
|
32
|
+
return options?.docs ? `${base}\nSee ${options.docs}` : base;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Error options, omitted entirely when there is no cause.
|
|
36
|
+
*
|
|
37
|
+
* `new Error(msg, { cause: undefined })` installs an own `cause` property set to
|
|
38
|
+
* undefined, which a plain `new Error(msg)` does not — enough to make some reporters
|
|
39
|
+
* print an empty cause and to make these errors shaped differently from every other
|
|
40
|
+
* error in the process. Passing nothing keeps them identical.
|
|
41
|
+
*/
|
|
42
|
+
function errorOptions(options) {
|
|
43
|
+
return options && "cause" in options && options.cause !== void 0 ? { cause: options.cause } : void 0;
|
|
44
|
+
}
|
|
45
|
+
/** A failure originating in vitest-native. */
|
|
46
|
+
var VitestNativeError = class extends Error {
|
|
47
|
+
constructor(code, message, options) {
|
|
48
|
+
super(buildMessage(message, options), errorOptions(options));
|
|
49
|
+
this.name = "VitestNativeError";
|
|
50
|
+
this.code = code;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
//#endregion
|
|
3
54
|
//#region src/helpers.ts
|
|
4
55
|
/**
|
|
5
56
|
* Test helpers for vitest-native.
|
|
@@ -9,7 +60,7 @@ const require_errors = require("./errors-B3lJK46O.cjs");
|
|
|
9
60
|
*/
|
|
10
61
|
function getMock() {
|
|
11
62
|
const mock = globalThis.__vitest_native_mock;
|
|
12
|
-
if (!mock) throw new
|
|
63
|
+
if (!mock) throw new VitestNativeError("HELPERS_BEFORE_SETUP", "helpers were called before setup ran. Ensure the vitest-native plugin is configured.");
|
|
13
64
|
return mock;
|
|
14
65
|
}
|
|
15
66
|
function getNativeControl() {
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { n as VitestNativeTypeError, t as VitestNativeError } from "./errors-CxoBUfVJ.mjs";
|
|
2
|
-
import { t as AUTO_DETECT_PRESETS } from "./preset-map-
|
|
2
|
+
import { t as AUTO_DETECT_PRESETS } from "./preset-map-L1-vD19T.mjs";
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
4
|
import fs from "node:fs";
|
|
5
5
|
import path from "node:path";
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
6
7
|
//#region src/validate.ts
|
|
7
8
|
const KNOWN_OPTIONS = [
|
|
8
9
|
"platform",
|
|
@@ -149,6 +150,165 @@ const PEER_REQUIREMENTS = [
|
|
|
149
150
|
optional: true
|
|
150
151
|
}
|
|
151
152
|
];
|
|
153
|
+
/**
|
|
154
|
+
* The Expo SDKs this release supports: the ones CI installs as packed Expo apps (SDK 57
|
|
155
|
+
* and 58). Expo is not a peer of this package, so it is not in PEER_REQUIREMENTS, but the
|
|
156
|
+
* plugin's startup check and doctor read the range from here, as they do the peers'.
|
|
157
|
+
*/
|
|
158
|
+
const SUPPORTED_EXPO_SDKS = {
|
|
159
|
+
minimum: 57,
|
|
160
|
+
maximum: 58
|
|
161
|
+
};
|
|
162
|
+
/** The SDKs from `minimum` to `maximum` as a list: "57 and 58", "57, 58 or 59". */
|
|
163
|
+
function expoSdkList(conjunction, { minimum, maximum } = SUPPORTED_EXPO_SDKS) {
|
|
164
|
+
const sdks = [];
|
|
165
|
+
for (let sdk = minimum; sdk <= maximum; sdk++) sdks.push(sdk);
|
|
166
|
+
return sdks.length === 1 ? String(sdks[0]) : `${sdks.slice(0, -1).join(", ")} ${conjunction} ${sdks.at(-1)}`;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* The version in the `expo/package.json` Node resolves from `dir`, or null. Where expo's
|
|
170
|
+
* `exports` leaves its manifest out, the package directory Node would load `expo` from,
|
|
171
|
+
* found along Node's own lookup paths.
|
|
172
|
+
*/
|
|
173
|
+
function expoVersionFrom(dir) {
|
|
174
|
+
const req = createRequire(path.join(dir, "package.json"));
|
|
175
|
+
let manifest;
|
|
176
|
+
try {
|
|
177
|
+
manifest = req.resolve("expo/package.json");
|
|
178
|
+
} catch (error) {
|
|
179
|
+
if (error.code !== "ERR_PACKAGE_PATH_NOT_EXPORTED") return null;
|
|
180
|
+
manifest = (req.resolve.paths("expo") ?? []).map((modules) => path.join(modules, "expo", "package.json")).find((file) => fs.existsSync(file));
|
|
181
|
+
}
|
|
182
|
+
if (!manifest) return null;
|
|
183
|
+
try {
|
|
184
|
+
const { version } = JSON.parse(fs.readFileSync(manifest, "utf8"));
|
|
185
|
+
return typeof version === "string" ? version : null;
|
|
186
|
+
} catch {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* The version of the `expo` the project at `root` uses, or null: the one Node resolves from
|
|
192
|
+
* `root`, else the one a dependency the project declares resolves as its peer. A workspace
|
|
193
|
+
* library that declares only expo-image reaches `expo` that way under an isolated install
|
|
194
|
+
* (pnpm links the peer beside expo-image, not into the library), and a library that uses
|
|
195
|
+
* no Expo package resolves none, whatever another workspace member installs. The plugin
|
|
196
|
+
* and doctor both read it from here.
|
|
197
|
+
*/
|
|
198
|
+
function installedExpoVersion(root) {
|
|
199
|
+
const own = expoVersionFrom(root);
|
|
200
|
+
if (own) return own;
|
|
201
|
+
let manifest;
|
|
202
|
+
try {
|
|
203
|
+
manifest = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8"));
|
|
204
|
+
} catch {
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
const req = createRequire(path.join(root, "package.json"));
|
|
208
|
+
for (const field of ["dependencies", "devDependencies"]) {
|
|
209
|
+
const deps = manifest[field];
|
|
210
|
+
if (!deps || typeof deps !== "object") continue;
|
|
211
|
+
for (const name of Object.keys(deps)) try {
|
|
212
|
+
const file = req.resolve(`${name}/package.json`);
|
|
213
|
+
const peers = JSON.parse(fs.readFileSync(file, "utf8")).peerDependencies;
|
|
214
|
+
if (!peers || typeof peers !== "object" || !("expo" in peers)) continue;
|
|
215
|
+
const version = expoVersionFrom(path.dirname(file));
|
|
216
|
+
if (version) return version;
|
|
217
|
+
} catch {}
|
|
218
|
+
}
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Why the installed Expo SDK is not supported, or null when it is, or when the version is
|
|
223
|
+
* not one this can read. A warning, not an error: an Expo app on an older SDK mostly runs,
|
|
224
|
+
* and failing it at startup would stop the tests that do.
|
|
225
|
+
*/
|
|
226
|
+
function unsupportedExpoSdkMessage(expoVersion) {
|
|
227
|
+
const match = /^(\d+)\.\d+\.\d+(?:[-+].*)?$/.exec(expoVersion);
|
|
228
|
+
if (!match) return null;
|
|
229
|
+
const sdk = Number(match[1]);
|
|
230
|
+
const { minimum, maximum } = SUPPORTED_EXPO_SDKS;
|
|
231
|
+
if (sdk >= minimum && sdk <= maximum) return null;
|
|
232
|
+
const supported = `vitest-native supports Expo SDK ${expoSdkList("and")}`;
|
|
233
|
+
return sdk < minimum ? `Expo SDK ${sdk} (expo ${expoVersion}) is not supported: ${supported}. Upgrade to Expo SDK ${expoSdkList("or")}; on an older SDK, tests that load Expo modules can fail.` : `Expo SDK ${sdk} (expo ${expoVersion}) is newer than this release supports: ${supported}. Check for a newer vitest-native.`;
|
|
234
|
+
}
|
|
235
|
+
//#endregion
|
|
236
|
+
//#region src/fs-module-cache.ts
|
|
237
|
+
/**
|
|
238
|
+
* Vitest's persistent transform cache (`fsModuleCache`), keyed for what this plugin
|
|
239
|
+
* contributes.
|
|
240
|
+
*
|
|
241
|
+
* Vitest 5 can keep transformed modules on disk between runs, as Jest keeps its
|
|
242
|
+
* transform cache. Measured on a 104-file production Expo suite, it cut warm runs by
|
|
243
|
+
* about 11%. Vitest keys each entry on the module's id and source, the config file's
|
|
244
|
+
* contents, the plugin NAMES, `resolve`, NODE_ENV and its own version — not on plugin
|
|
245
|
+
* versions, nor on anything a plugin reads from disk. This plugin's output depends on
|
|
246
|
+
* both: its own version, and the installed packages (which presets are detected, which
|
|
247
|
+
* packages compile). Vitest's official extension point, a cache key generator, carries
|
|
248
|
+
* that part of the key:
|
|
249
|
+
* - the vitest-native version,
|
|
250
|
+
* - the plugin's resolved options and the environment it hands to workers,
|
|
251
|
+
* - the project's lockfile(s), so any dependency change invalidates the cache.
|
|
252
|
+
* Without a lockfile the root package.json stands in for it.
|
|
253
|
+
*/
|
|
254
|
+
const LOCKFILES = [
|
|
255
|
+
"bun.lock",
|
|
256
|
+
"bun.lockb",
|
|
257
|
+
"package-lock.json",
|
|
258
|
+
"npm-shrinkwrap.json",
|
|
259
|
+
"pnpm-lock.yaml",
|
|
260
|
+
"yarn.lock"
|
|
261
|
+
];
|
|
262
|
+
const sha1 = (value) => createHash("sha1").update(value).digest("hex");
|
|
263
|
+
/** A digest of the nearest lockfiles at or above `root`, else of its package.json. */
|
|
264
|
+
function dependencyFingerprint(root) {
|
|
265
|
+
for (let dir = path.resolve(root);; dir = path.dirname(dir)) {
|
|
266
|
+
const found = LOCKFILES.map((name) => path.join(dir, name)).filter((file) => fs.existsSync(file));
|
|
267
|
+
if (found.length > 0) {
|
|
268
|
+
const hash = createHash("sha1");
|
|
269
|
+
for (const file of found) hash.update(file).update(fs.readFileSync(file));
|
|
270
|
+
return hash.digest("hex");
|
|
271
|
+
}
|
|
272
|
+
if (path.dirname(dir) === dir) break;
|
|
273
|
+
}
|
|
274
|
+
try {
|
|
275
|
+
return sha1(fs.readFileSync(path.join(root, "package.json")));
|
|
276
|
+
} catch {
|
|
277
|
+
return "no-manifest";
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
/** The version of the package rooted at `packageDir`. */
|
|
281
|
+
function packageVersionAt(packageDir) {
|
|
282
|
+
try {
|
|
283
|
+
const manifest = JSON.parse(fs.readFileSync(path.join(packageDir, "package.json"), "utf8"));
|
|
284
|
+
return typeof manifest.version === "string" ? manifest.version : "unknown";
|
|
285
|
+
} catch {
|
|
286
|
+
return "unknown";
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
/** This plugin's contribution to a module's cache key in one project. */
|
|
290
|
+
function fsModuleCacheKey(ownPackageDir, projectRoot, pluginState) {
|
|
291
|
+
const state = JSON.stringify(pluginState, (_key, value) => typeof value === "function" || value instanceof RegExp ? String(value) : value);
|
|
292
|
+
return sha1(`vitest-native@${packageVersionAt(ownPackageDir)}\0${dependencyFingerprint(projectRoot)}\0${state}`);
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Whether the plugin turns the cache on for this Vitest major when the user has not
|
|
296
|
+
* chosen: from Vitest 5, where `fsModuleCache` is a top-level option. Vitest 4 has it
|
|
297
|
+
* only as `experimental.fsModuleCache` (from 4.0.11), and it is left to the user there.
|
|
298
|
+
* `vitest-native doctor` reads this too, so its Vitest 4 advisory cannot drift from
|
|
299
|
+
* what the plugin does.
|
|
300
|
+
*/
|
|
301
|
+
function fsModuleCacheOnByDefault(vitestMajor) {
|
|
302
|
+
return vitestMajor >= 5;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Whether to turn the cache on: a Vitest the plugin defaults it on for, and the user
|
|
306
|
+
* has not chosen either way, in either spelling.
|
|
307
|
+
*/
|
|
308
|
+
function shouldDefaultFsModuleCache(vitestMajor, userTest) {
|
|
309
|
+
if (!fsModuleCacheOnByDefault(vitestMajor)) return false;
|
|
310
|
+
return userTest?.fsModuleCache === void 0 && userTest?.experimental?.fsModuleCache === void 0;
|
|
311
|
+
}
|
|
152
312
|
//#endregion
|
|
153
313
|
//#region src/native/match.mjs
|
|
154
314
|
/**
|
|
@@ -242,18 +402,28 @@ function containsPath(dir, target) {
|
|
|
242
402
|
return inner === outer || inner.startsWith(outer.endsWith(path.sep) ? outer : outer + path.sep);
|
|
243
403
|
}
|
|
244
404
|
/**
|
|
245
|
-
* The on-disk directory a package resolves to
|
|
405
|
+
* The on-disk directory a package resolves to from `fromDir`, or null.
|
|
246
406
|
*
|
|
247
|
-
* `pkg/package.json` is tried first because it names the package root exactly
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
407
|
+
* `pkg/package.json` is tried first because it names the package root exactly. A
|
|
408
|
+
* package whose `exports` map does not list `./package.json` (expo-symbols 57) refuses
|
|
409
|
+
* that request, and resolving its entry instead fails too when `exports` offers only
|
|
410
|
+
* an `import` condition. Node finds a package's DIRECTORY before it applies `exports`,
|
|
411
|
+
* by looking in each of the request's lookup paths (`require.resolve.paths`, the
|
|
412
|
+
* node_modules chain plus NODE_PATH), so the directory is looked up there next. Last,
|
|
413
|
+
* the entry is resolved and the manifest that names the package found above it: the
|
|
414
|
+
* package's own name resolving from inside it (a self-reference) is in no lookup path.
|
|
415
|
+
* Symlinked and workspace packages resolve to their real location, which is the whole
|
|
416
|
+
* point — see buildPkgMatcher.
|
|
251
417
|
*/
|
|
252
|
-
function packageDirOf(name,
|
|
253
|
-
const req = createRequire(path.join(
|
|
418
|
+
function packageDirOf(name, fromDir) {
|
|
419
|
+
const req = createRequire(path.join(fromDir, "package.json"));
|
|
254
420
|
try {
|
|
255
421
|
return canonicalDir(path.dirname(req.resolve(`${name}/package.json`)));
|
|
256
422
|
} catch {}
|
|
423
|
+
for (const lookup of req.resolve.paths(name) ?? []) {
|
|
424
|
+
const candidate = path.join(lookup, ...name.split("/"));
|
|
425
|
+
if (fs.existsSync(path.join(candidate, "package.json"))) return canonicalDir(candidate);
|
|
426
|
+
}
|
|
257
427
|
let dir;
|
|
258
428
|
try {
|
|
259
429
|
dir = path.dirname(req.resolve(name));
|
|
@@ -914,7 +1084,7 @@ function detectEcosystemPackages(projectRoot, explicit = [], testRoots = [], nev
|
|
|
914
1084
|
]);
|
|
915
1085
|
const candidates = /* @__PURE__ */ new Set();
|
|
916
1086
|
const found = roots.flatMap((root) => manifestsFrom(root));
|
|
917
|
-
const
|
|
1087
|
+
const declaringDirs = [...new Set(found.map((entry) => entry.dir))];
|
|
918
1088
|
for (const { manifest } of found) for (const field of [
|
|
919
1089
|
"dependencies",
|
|
920
1090
|
"devDependencies",
|
|
@@ -924,12 +1094,13 @@ function detectEcosystemPackages(projectRoot, explicit = [], testRoots = [], nev
|
|
|
924
1094
|
if (deps && typeof deps === "object") for (const name of Object.keys(deps)) candidates.add(name);
|
|
925
1095
|
}
|
|
926
1096
|
if (candidates.size === 0) return [];
|
|
927
|
-
const
|
|
1097
|
+
const installedFrom = (fromDir, name) => {
|
|
1098
|
+
const dir = packageDirOf(name, fromDir);
|
|
1099
|
+
if (!dir) return null;
|
|
928
1100
|
try {
|
|
929
|
-
const file = req.resolve(`${name}/package.json`);
|
|
930
1101
|
return {
|
|
931
|
-
manifest:
|
|
932
|
-
dir
|
|
1102
|
+
manifest: JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf8")),
|
|
1103
|
+
dir
|
|
933
1104
|
};
|
|
934
1105
|
} catch {
|
|
935
1106
|
return null;
|
|
@@ -937,22 +1108,14 @@ function detectEcosystemPackages(projectRoot, explicit = [], testRoots = [], nev
|
|
|
937
1108
|
};
|
|
938
1109
|
/** Resolve a package's manifest from whichever root can see it. */
|
|
939
1110
|
const installedOf = (name) => {
|
|
940
|
-
for (const
|
|
941
|
-
const installed =
|
|
1111
|
+
for (const dir of declaringDirs) {
|
|
1112
|
+
const installed = installedFrom(dir, name);
|
|
942
1113
|
if (installed) return installed;
|
|
943
1114
|
}
|
|
944
1115
|
return null;
|
|
945
1116
|
};
|
|
946
1117
|
const manifestOf = (name) => installedOf(name)?.manifest ?? null;
|
|
947
|
-
const
|
|
948
|
-
const dependencyOf = (parent, name) => {
|
|
949
|
-
let req = requireByDir.get(parent.dir);
|
|
950
|
-
if (!req) {
|
|
951
|
-
req = createRequire(path.join(parent.dir, "package.json"));
|
|
952
|
-
requireByDir.set(parent.dir, req);
|
|
953
|
-
}
|
|
954
|
-
return installedVia(req, name);
|
|
955
|
-
};
|
|
1118
|
+
const dependencyOf = (parent, name) => installedFrom(parent.dir, name);
|
|
956
1119
|
/**
|
|
957
1120
|
* The packages the run itself lives in — the project, in other words.
|
|
958
1121
|
*
|
|
@@ -1024,22 +1187,46 @@ function detectEcosystemPackages(projectRoot, explicit = [], testRoots = [], nev
|
|
|
1024
1187
|
if (deps && typeof deps === "object") for (const name of Object.keys(deps)) projectDeclared.add(name);
|
|
1025
1188
|
}
|
|
1026
1189
|
}
|
|
1190
|
+
const detectedSet = new Set(detected);
|
|
1027
1191
|
const inClosure = new Set(detected);
|
|
1028
|
-
const
|
|
1192
|
+
const walked = /* @__PURE__ */ new Set();
|
|
1193
|
+
const queue = [];
|
|
1194
|
+
const reach = (name, installed) => {
|
|
1195
|
+
inClosure.add(name);
|
|
1196
|
+
if (walked.has(name)) return;
|
|
1197
|
+
walked.add(name);
|
|
1198
|
+
queue.push(installed);
|
|
1199
|
+
};
|
|
1200
|
+
for (const name of detected) {
|
|
1201
|
+
if (!projectDeclared.has(name)) continue;
|
|
1202
|
+
const installed = installedOf(name);
|
|
1203
|
+
if (installed) reach(name, installed);
|
|
1204
|
+
}
|
|
1205
|
+
const neverSeeded = /* @__PURE__ */ new Set([...shadowed, ...neverTransform]);
|
|
1206
|
+
for (const name of explicit) {
|
|
1207
|
+
if (neverSeeded.has(name)) continue;
|
|
1208
|
+
const installed = installedOf(name);
|
|
1209
|
+
if (!installed || walked.has(name)) continue;
|
|
1210
|
+
walked.add(name);
|
|
1211
|
+
queue.push(installed);
|
|
1212
|
+
}
|
|
1213
|
+
/** A dependency or peer the walk may not take, whatever its manifest says. */
|
|
1214
|
+
const excluded = (dep) => skip.has(dep) || dep.startsWith("@react-native/") || rnOwned.has(dep) || toolchain.has(dep) || ownsTheRun(dep);
|
|
1029
1215
|
while (queue.length > 0) {
|
|
1030
1216
|
const pkg = queue.pop();
|
|
1031
1217
|
const deps = pkg.manifest.dependencies;
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
if (inClosure.has(dep) || skip.has(dep) || dep.startsWith("@react-native/")) continue;
|
|
1035
|
-
if (rnOwned.has(dep)) continue;
|
|
1036
|
-
if (toolchain.has(dep)) continue;
|
|
1037
|
-
if (ownsTheRun(dep)) continue;
|
|
1218
|
+
for (const dep of deps && typeof deps === "object" ? Object.keys(deps) : []) {
|
|
1219
|
+
if (walked.has(dep) || excluded(dep)) continue;
|
|
1038
1220
|
const installed = dependencyOf(pkg, dep);
|
|
1039
1221
|
if (!installed) continue;
|
|
1040
|
-
if (publishesOnlyEsm(installed.manifest)) continue;
|
|
1041
|
-
|
|
1042
|
-
|
|
1222
|
+
if (!inClosure.has(dep) && publishesOnlyEsm(installed.manifest)) continue;
|
|
1223
|
+
reach(dep, installed);
|
|
1224
|
+
}
|
|
1225
|
+
const peers = pkg.manifest.peerDependencies;
|
|
1226
|
+
for (const peer of peers && typeof peers === "object" ? Object.keys(peers) : []) {
|
|
1227
|
+
if (walked.has(peer) || excluded(peer) || !detectedSet.has(peer)) continue;
|
|
1228
|
+
const installed = dependencyOf(pkg, peer);
|
|
1229
|
+
if (installed) reach(peer, installed);
|
|
1043
1230
|
}
|
|
1044
1231
|
}
|
|
1045
1232
|
return [...inClosure].sort();
|
|
@@ -1177,4 +1364,4 @@ function assertHotWorkerLoadsRunningVitest(running, workerEntry) {
|
|
|
1177
1364
|
}, loadedRoot, "the hot runtime's worker", " Or set `hotRuntime: false`: per-file isolation loads Vitest through Vitest's own resolver, which reaches the running copy.");
|
|
1178
1365
|
}
|
|
1179
1366
|
//#endregion
|
|
1180
|
-
export { validateOptions as _, runningVite as a, detectEngine as c, findNativeInlineConflicts as d, findNativeOwnershipConflict as f,
|
|
1367
|
+
export { validatePeerDependency as C, validateOptions as S, fsModuleCacheOnByDefault as _, runningVite as a, installedExpoVersion as b, detectEngine as c, findNativeInlineConflicts as d, findNativeOwnershipConflict as f, fsModuleCacheKey as g, packageDirOf as h, ownVitestPackage as i, resolveTransformPreset as l, containsPath as m, assertHotWorkerLoadsRunningVitest as n, usesVitePlus as o, formatNativeOwnershipManifest as p, installedViteVersion as r, detectEcosystemPackages as s, assertHostResolvesRunningVitest as t, createNativeOwnershipPolicy as u, shouldDefaultFsModuleCache as v, warnUnknownOptions as w, unsupportedExpoSdkMessage as x, PEER_REQUIREMENTS as y };
|