vitest-native 0.10.0 → 0.11.1
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/CHANGELOG.md +257 -0
- package/README.md +1 -0
- package/dist/cli.cjs +25 -7
- package/dist/cli.mjs +22 -4
- package/dist/{ecosystem-IuZ-Gv0R.cjs → ecosystem-DGlajfDG.cjs} +280 -14
- package/dist/{ecosystem-CO5vqExO.mjs → ecosystem-DJMGR2YY.mjs} +254 -12
- package/dist/helpers.cjs +1 -1
- package/dist/helpers.mjs +1 -1
- package/dist/{index-BJL4cQ-l.d.cts → index-CrMeE8rd.d.mts} +5 -5
- package/dist/{index-CvCU0cLl.d.mts → index-DIMbjH8R.d.cts} +6 -3
- package/dist/index.d.mts +1 -2
- package/dist/index.mjs +78 -48
- package/dist/jest-compat.cjs +4 -4
- package/dist/jest-compat.d.cts +0 -1
- package/dist/jest-compat.d.mts +0 -1
- package/dist/jest-compat.mjs +2 -2
- package/dist/native/hooks.mjs +77 -12
- package/dist/native/loader.mjs +10 -5
- package/dist/native/match.d.mts +33 -0
- package/dist/native/match.mjs +94 -22
- package/dist/native/registry.mjs +30 -2
- package/dist/{pool-CFND7plG.cjs → pool-BU-1ecIF.cjs} +6 -6
- package/dist/{pool-Bcxp4Qrg.mjs → pool-Bcxn9a3I.mjs} +3 -3
- package/dist/{preset-map-DxilQ4pI.cjs → preset-map-BoG19dLa.cjs} +1 -0
- package/dist/{preset-map-BpDG_D5m.mjs → preset-map-DuX0ocdn.mjs} +1 -0
- package/dist/{presets-BGTqVH6C.cjs → presets-CwO_c4aL.cjs} +131 -6
- package/dist/{presets-CK85x8ey.mjs → presets-IStcJEOa.mjs} +123 -4
- package/dist/presets.d.mts +2 -2
- package/dist/presets.mjs +2 -2
- package/dist/{chunk-CjcI7cDX.mjs → rolldown-runtime-BhDjJH2R.mjs} +1 -1
- package/dist/{chunk-CZWwpsFl.cjs → rolldown-runtime-C0BPl7ul.cjs} +1 -1
- package/dist/serializer.cjs +1 -1
- package/dist/serializer.mjs +1 -1
- package/dist/setup.d.mts +1 -1
- package/dist/setup.mjs +5 -5
- package/docs/versioning.md +1 -1
- package/package.json +17 -15
- /package/dist/{errors-flXDIeWk.cjs → errors-B3lJK46O.cjs} +0 -0
- /package/dist/{errors-Dm5yLNTF.mjs → errors-CxoBUfVJ.mjs} +0 -0
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
import { Mock } from "vitest";
|
|
2
|
-
|
|
3
|
-
//#region \0rolldown/runtime.js
|
|
4
|
-
//#endregion
|
|
5
2
|
//#region src/types.d.ts
|
|
6
3
|
/** Static methods available on the Image component mock. */
|
|
7
4
|
interface ImageStatic {
|
|
@@ -354,6 +351,9 @@ declare function deviceInfo(): Preset;
|
|
|
354
351
|
//#region src/presets/mmkv.d.ts
|
|
355
352
|
declare function mmkv(): Preset;
|
|
356
353
|
//#endregion
|
|
354
|
+
//#region src/presets/netinfo.d.ts
|
|
355
|
+
declare function netInfo(): Preset;
|
|
356
|
+
//#endregion
|
|
357
357
|
//#region src/presets/svg.d.ts
|
|
358
358
|
declare function svg(): Preset;
|
|
359
359
|
//#endregion
|
|
@@ -375,7 +375,7 @@ declare function keyboardController(): Preset;
|
|
|
375
375
|
//#region src/presets/worklets.d.ts
|
|
376
376
|
declare function worklets(): Preset;
|
|
377
377
|
declare namespace index_d_exports {
|
|
378
|
-
export { asyncStorage, bottomSheet, deviceInfo, expo, flashList, gestureHandler, keyboardController, mmkv, navigation, reanimated, safeAreaContext, screens, svg, vectorIcons, webview, worklets };
|
|
378
|
+
export { asyncStorage, bottomSheet, deviceInfo, expo, flashList, gestureHandler, keyboardController, mmkv, navigation, netInfo, reanimated, safeAreaContext, screens, svg, vectorIcons, webview, worklets };
|
|
379
379
|
}
|
|
380
380
|
//#endregion
|
|
381
|
-
export {
|
|
381
|
+
export { gestureHandler as _, flashList as a, ReactNativeMock as b, svg as c, deviceInfo as d, expo as f, safeAreaContext as g, navigation as h, bottomSheet as i, netInfo as l, asyncStorage as m, worklets as n, vectorIcons as o, screens as p, keyboardController as r, webview as s, index_d_exports as t, mmkv as u, reanimated as v, VitestNativeOptions as x, Preset as y };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Mock } from "vitest";
|
|
2
|
-
|
|
2
|
+
//#endregion
|
|
3
3
|
//#region src/types.d.ts
|
|
4
4
|
/** Static methods available on the Image component mock. */
|
|
5
5
|
interface ImageStatic {
|
|
@@ -352,6 +352,9 @@ declare function deviceInfo(): Preset;
|
|
|
352
352
|
//#region src/presets/mmkv.d.ts
|
|
353
353
|
declare function mmkv(): Preset;
|
|
354
354
|
//#endregion
|
|
355
|
+
//#region src/presets/netinfo.d.ts
|
|
356
|
+
declare function netInfo(): Preset;
|
|
357
|
+
//#endregion
|
|
355
358
|
//#region src/presets/svg.d.ts
|
|
356
359
|
declare function svg(): Preset;
|
|
357
360
|
//#endregion
|
|
@@ -373,7 +376,7 @@ declare function keyboardController(): Preset;
|
|
|
373
376
|
//#region src/presets/worklets.d.ts
|
|
374
377
|
declare function worklets(): Preset;
|
|
375
378
|
declare namespace index_d_exports {
|
|
376
|
-
export { asyncStorage, bottomSheet, deviceInfo, expo, flashList, gestureHandler, keyboardController, mmkv, navigation, reanimated, safeAreaContext, screens, svg, vectorIcons, webview, worklets };
|
|
379
|
+
export { asyncStorage, bottomSheet, deviceInfo, expo, flashList, gestureHandler, keyboardController, mmkv, navigation, netInfo, reanimated, safeAreaContext, screens, svg, vectorIcons, webview, worklets };
|
|
377
380
|
}
|
|
378
381
|
//#endregion
|
|
379
|
-
export {
|
|
382
|
+
export { gestureHandler as _, flashList as a, ReactNativeMock as b, svg as c, deviceInfo as d, expo as f, safeAreaContext as g, navigation as h, bottomSheet as i, netInfo as l, asyncStorage as m, worklets as n, vectorIcons as o, screens as p, keyboardController as r, webview as s, index_d_exports as t, mmkv as u, reanimated as v, VitestNativeOptions as x, Preset as y };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { b as
|
|
1
|
+
import { b as ReactNativeMock, t as index_d_exports, x as VitestNativeOptions, y as Preset } from "./index-CrMeE8rd.mjs";
|
|
2
2
|
import { Plugin } from "vite";
|
|
3
|
-
|
|
4
3
|
//#region src/plugin.d.ts
|
|
5
4
|
/**
|
|
6
5
|
* Preset names switched off by the object form of the `presets` option.
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { t as VitestNativeError } from "./errors-
|
|
2
|
-
import { a as
|
|
3
|
-
import { t as AUTO_DETECT_PRESETS } from "./preset-map-
|
|
4
|
-
import { t as presets_exports } from "./presets-
|
|
1
|
+
import { t as VitestNativeError } from "./errors-CxoBUfVJ.mjs";
|
|
2
|
+
import { a as packageDirOf, c as validateOptions, i as containsPath, l as validatePeerDependency, n as detectEngine, o as packagePatterns, r as REACT_NATIVE_PATH, s as PEER_REQUIREMENTS, t as detectEcosystemPackages, u as warnUnknownOptions } from "./ecosystem-DJMGR2YY.mjs";
|
|
3
|
+
import { t as AUTO_DETECT_PRESETS } from "./preset-map-DuX0ocdn.mjs";
|
|
4
|
+
import { t as presets_exports } from "./presets-IStcJEOa.mjs";
|
|
5
5
|
import { createRequire } from "node:module";
|
|
6
6
|
import fs from "node:fs";
|
|
7
7
|
import path from "node:path";
|
|
@@ -55,41 +55,19 @@ function getPlatformExtensions(platform) {
|
|
|
55
55
|
//#endregion
|
|
56
56
|
//#region src/native/apply.ts
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* The on-disk directory a package resolves to, or null. Used alongside the
|
|
68
|
-
* node_modules-anchored patterns so workspace and `file:` dependencies — which
|
|
69
|
-
* resolve to a real path with no node_modules segment — are matched too.
|
|
58
|
+
* Files outside any installed package that Vitest may be running as an ENTRY rather
|
|
59
|
+
* than importing as a module: the two naming conventions test runners use.
|
|
60
|
+
*
|
|
61
|
+
* Neither is exhaustive — a project can point `test.include` anywhere — but a file
|
|
62
|
+
* that matches one of these is a test by convention and never library source, which
|
|
63
|
+
* is what makes it safe to keep out of Node's graph unconditionally.
|
|
70
64
|
*/
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
function nativeEngineConfig(setupFilePath, env, extensions, transformPkgs = [], hot, jsxTransform = { esbuild: { jsx: "automatic" } }, userPool, inlinePkgs = [], projectRoot = process.cwd()) {
|
|
80
|
-
const extraExternal = transformPkgs.flatMap((p) => {
|
|
81
|
-
const patterns = [new RegExp(`[\\\\/]node_modules[\\\\/]${escapeRe(p)}[\\\\/]`)];
|
|
82
|
-
const dir = resolvePackageDir(p, projectRoot);
|
|
83
|
-
if (dir) patterns.push(new RegExp(`^${escapeRe(dir.replace(/\\/g, "/"))}[\\\\/]`));
|
|
84
|
-
return patterns;
|
|
85
|
-
});
|
|
86
|
-
const ecosystemExternal = inlinePkgs.flatMap((p) => {
|
|
87
|
-
const patterns = [new RegExp(`[\\\\/]node_modules[\\\\/]${escapeRe(p)}[\\\\/]`)];
|
|
88
|
-
const dir = resolvePackageDir(p, projectRoot);
|
|
89
|
-
if (dir) patterns.push(new RegExp(`^${escapeRe(dir.replace(/\\/g, "/"))}[\\\\/]`));
|
|
90
|
-
return patterns;
|
|
91
|
-
});
|
|
92
|
-
const nodeTransformed = [...new Set([...transformPkgs, ...inlinePkgs])];
|
|
65
|
+
const OUTSIDE_NODE_MODULES = String.raw`^(?!.*[\\/]node_modules[\\/])`;
|
|
66
|
+
const TEST_ENTRY_PATTERNS = [new RegExp(String.raw`${OUTSIDE_NODE_MODULES}.*\.(?:test|spec)\.[cm]?[jt]sx?$`), new RegExp(String.raw`${OUTSIDE_NODE_MODULES}.*[\\/]__tests__[\\/].*\.[cm]?[jt]sx?$`)];
|
|
67
|
+
function nativeEngineConfig(setupFilePath, env, extensions, transformPkgs = [], hot, jsxTransform = { esbuild: { jsx: "automatic" } }, userPool, inlinePkgs = [], projectRoot = process.cwd(), userInlinesEverything = false) {
|
|
68
|
+
const extraExternal = transformPkgs.flatMap((p) => packagePatterns(p, projectRoot));
|
|
69
|
+
const ecosystemExternal = inlinePkgs.flatMap((p) => packagePatterns(p, projectRoot));
|
|
70
|
+
const nodeTransformed = [.../* @__PURE__ */ new Set([...transformPkgs, ...inlinePkgs])];
|
|
93
71
|
const fullEnv = { ...env };
|
|
94
72
|
if (nodeTransformed.length > 0) fullEnv.VITEST_NATIVE_TRANSFORM = JSON.stringify(nodeTransformed);
|
|
95
73
|
return {
|
|
@@ -127,12 +105,14 @@ function nativeEngineConfig(setupFilePath, env, extensions, transformPkgs = [],
|
|
|
127
105
|
isolate: false,
|
|
128
106
|
runner: hot.runnerPath
|
|
129
107
|
} : { pool: userPool ?? "threads" },
|
|
130
|
-
server: { deps: {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
108
|
+
server: { deps: {
|
|
109
|
+
...userInlinesEverything ? {} : { inline: TEST_ENTRY_PATTERNS },
|
|
110
|
+
external: [
|
|
111
|
+
REACT_NATIVE_PATH,
|
|
112
|
+
...extraExternal,
|
|
113
|
+
...ecosystemExternal
|
|
114
|
+
]
|
|
115
|
+
} }
|
|
136
116
|
}
|
|
137
117
|
};
|
|
138
118
|
}
|
|
@@ -188,6 +168,51 @@ async function autoDetectPresets(diagnostics, projectRoot) {
|
|
|
188
168
|
}
|
|
189
169
|
return detected;
|
|
190
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* The directories a set of `test.include` globs point into, resolved against the run
|
|
173
|
+
* root — the literal part of each pattern, up to its first wildcard.
|
|
174
|
+
*
|
|
175
|
+
* `packages/ui/src/**\/*.test.ts` names `packages/ui/src`, which is enough to know
|
|
176
|
+
* the run's tests live in that package. `**\/*.test.ts` names nothing above the root
|
|
177
|
+
* and is dropped: treating it as a hint would mark every workspace member as the
|
|
178
|
+
* project and undo their detection entirely.
|
|
179
|
+
*/
|
|
180
|
+
function testIncludeRoots(include, projectRoot) {
|
|
181
|
+
if (!Array.isArray(include)) return [];
|
|
182
|
+
const dirs = /* @__PURE__ */ new Set();
|
|
183
|
+
for (const pattern of include) {
|
|
184
|
+
if (typeof pattern !== "string" || pattern.startsWith("!")) continue;
|
|
185
|
+
const literal = pattern.slice(0, firstWildcard(pattern));
|
|
186
|
+
const slash = literal.replace(/\\/g, "/").lastIndexOf("/");
|
|
187
|
+
if (slash <= 0) continue;
|
|
188
|
+
dirs.add(path.resolve(projectRoot, literal.slice(0, slash)));
|
|
189
|
+
}
|
|
190
|
+
return [...dirs];
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Where a glob stops being a literal path.
|
|
194
|
+
*
|
|
195
|
+
* `!`, `+` and `@` only introduce a pattern as part of an extglob — `@(a|b)` — and
|
|
196
|
+
* are ordinary characters otherwise. Treating them as wildcards on their own cut
|
|
197
|
+
* `packages/@scope/ui/**\/*.test.ts` down to `packages/`, which would have named the
|
|
198
|
+
* whole workspace as the project and switched detection off for every member of it.
|
|
199
|
+
*/
|
|
200
|
+
function firstWildcard(pattern) {
|
|
201
|
+
const plain = pattern.search(/[*?[{]/);
|
|
202
|
+
const extglob = pattern.search(/[!+@]\(/);
|
|
203
|
+
if (plain === -1) return extglob === -1 ? pattern.length : extglob;
|
|
204
|
+
return extglob === -1 ? plain : Math.min(plain, extglob);
|
|
205
|
+
}
|
|
206
|
+
/** The nearest directory at or above `from` holding a package.json, or null. */
|
|
207
|
+
function nearestPackageDir(from) {
|
|
208
|
+
let dir = path.resolve(from);
|
|
209
|
+
for (;;) {
|
|
210
|
+
if (fs.existsSync(path.join(dir, "package.json"))) return dir;
|
|
211
|
+
const parent = path.dirname(dir);
|
|
212
|
+
if (parent === dir) return null;
|
|
213
|
+
dir = parent;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
191
216
|
function resolvePackageVersion(packageName, projectRoot) {
|
|
192
217
|
const req = createRequire(path.join(projectRoot, "package.json"));
|
|
193
218
|
try {
|
|
@@ -487,7 +512,7 @@ function subpathLeafOf(specifier) {
|
|
|
487
512
|
* be shadowed (test utilities and tooling entry points). Mirrors
|
|
488
513
|
* native/match.mjs.
|
|
489
514
|
*/
|
|
490
|
-
const UTILITY_SUBPATH_LEAVES = new Set([
|
|
515
|
+
const UTILITY_SUBPATH_LEAVES = /* @__PURE__ */ new Set([
|
|
491
516
|
"jest-utils",
|
|
492
517
|
"jestSetup",
|
|
493
518
|
"mock",
|
|
@@ -702,7 +727,11 @@ function reactNative(options) {
|
|
|
702
727
|
for (const p of options.presets) if (p.config && Object.keys(p.config).length > 0) presetConfig[p.name] = p.config;
|
|
703
728
|
if (Object.keys(presetConfig).length > 0) env.VITEST_NATIVE_PRESET_CONFIG = JSON.stringify(presetConfig);
|
|
704
729
|
}
|
|
705
|
-
const
|
|
730
|
+
const includeRoots = testIncludeRoots(userConfig.test?.include, resolvedRoot);
|
|
731
|
+
const ecosystem = detectEcosystemPackages([resolvedRoot, ...includeRoots], transformPkgs, includeRoots);
|
|
732
|
+
const linkedDetectedDirs = ecosystem.map((name) => packageDirOf(name, resolvedRoot)).filter((dir) => dir !== null && !/[\\/]node_modules[\\/]/.test(dir));
|
|
733
|
+
const projectDirs = [...new Set([resolvedRoot, ...includeRoots].map((dir) => nearestPackageDir(dir)).filter((dir) => dir !== null))].filter((dir) => !linkedDetectedDirs.some((detected) => containsPath(dir, detected)));
|
|
734
|
+
if (projectDirs.length > 0) env.VITEST_NATIVE_PROJECT_DIRS = JSON.stringify(projectDirs);
|
|
706
735
|
if (ecosystem.length > 0) {
|
|
707
736
|
ecosystemRoot = resolvedRoot;
|
|
708
737
|
ecosystemPattern = new RegExp(`[\\\\/]node_modules[\\\\/](?:${ecosystem.map((p) => p.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})[\\\\/]`);
|
|
@@ -718,7 +747,7 @@ function reactNative(options) {
|
|
|
718
747
|
if (registryFile) env.VITEST_NATIVE_RN_REGISTRY = registryFile;
|
|
719
748
|
let hot;
|
|
720
749
|
if (hotRuntime) {
|
|
721
|
-
const { nativePool, defaultHotMemoryLimit } = await import("./pool-
|
|
750
|
+
const { nativePool, defaultHotMemoryLimit } = await import("./pool-Bcxn9a3I.mjs");
|
|
722
751
|
const memoryLimit = hotRecycle.memoryLimit ?? (hotRecycle.recycleAfterFiles == null ? defaultHotMemoryLimit() : void 0);
|
|
723
752
|
hot = {
|
|
724
753
|
pool: nativePool({
|
|
@@ -732,9 +761,10 @@ function reactNative(options) {
|
|
|
732
761
|
};
|
|
733
762
|
}
|
|
734
763
|
const userPool = userConfig.test?.pool;
|
|
764
|
+
const userInlinesEverything = userConfig.test?.server?.deps?.inline === true;
|
|
735
765
|
if (userPool === "vmThreads" || userPool === "vmForks") throw new VitestNativeError("UNSUPPORTED_POOL", `engine:'native' cannot run on the '${userPool}' pool. React Native is loaded through Node's module hooks, which a VM pool's context does not use — React Native fails to resolve its platform files there. Use 'threads' (the default) or 'forks', or switch to engine:'mock', which needs no hooks.`);
|
|
736
766
|
if (hot && userPool) console.warn(`[vitest-native] 'hotRuntime' supplies its own pool, overriding the configured pool '${typeof userPool === "string" ? userPool : "(custom)"}'.`);
|
|
737
|
-
return asCompatibleViteConfig(nativeEngineConfig(nativeSetupPath, env, extensions, transformPkgs, hot, jsxTransform, userPool, ecosystem, resolvedRoot));
|
|
767
|
+
return asCompatibleViteConfig(nativeEngineConfig(nativeSetupPath, env, extensions, transformPkgs, hot, jsxTransform, userPool, ecosystem, resolvedRoot, userInlinesEverything));
|
|
738
768
|
}
|
|
739
769
|
if (hotRuntime) console.warn(`[vitest-native] 'hotRuntime' only applies to engine:'native' (resolved engine: '${engine}'); ignoring.`);
|
|
740
770
|
if (options?.mocks && Object.keys(options.mocks).length > 0) env.VITEST_NATIVE_MOCKS = JSON.stringify(options.mocks);
|
package/dist/jest-compat.cjs
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
2
|
+
const require_rolldown_runtime = require("./rolldown-runtime-C0BPl7ul.cjs");
|
|
3
3
|
let magic_string = require("magic-string");
|
|
4
|
-
magic_string =
|
|
4
|
+
magic_string = require_rolldown_runtime.__toESM(magic_string, 1);
|
|
5
5
|
//#region src/jest-compat/transform.ts
|
|
6
|
-
const HOISTABLE = new Set([
|
|
6
|
+
const HOISTABLE = /* @__PURE__ */ new Set([
|
|
7
7
|
"mock",
|
|
8
8
|
"unmock",
|
|
9
9
|
"doMock",
|
|
10
10
|
"doUnmock"
|
|
11
11
|
]);
|
|
12
|
-
const WITH_FACTORY = new Set(["mock", "doMock"]);
|
|
12
|
+
const WITH_FACTORY = /* @__PURE__ */ new Set(["mock", "doMock"]);
|
|
13
13
|
const TRANSFORMABLE = /\.(?:[cm]?[jt]sx?)$/;
|
|
14
14
|
const HAS_JEST_MOCK = /\bjest\s*\.\s*(?:mock|unmock|doMock|doUnmock)\s*\(/;
|
|
15
15
|
/** Visit every node in an ESTree AST (depth-first), calling `fn` on each. */
|
package/dist/jest-compat.d.cts
CHANGED
package/dist/jest-compat.d.mts
CHANGED
package/dist/jest-compat.mjs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import MagicString from "magic-string";
|
|
2
2
|
//#region src/jest-compat/transform.ts
|
|
3
|
-
const HOISTABLE = new Set([
|
|
3
|
+
const HOISTABLE = /* @__PURE__ */ new Set([
|
|
4
4
|
"mock",
|
|
5
5
|
"unmock",
|
|
6
6
|
"doMock",
|
|
7
7
|
"doUnmock"
|
|
8
8
|
]);
|
|
9
|
-
const WITH_FACTORY = new Set(["mock", "doMock"]);
|
|
9
|
+
const WITH_FACTORY = /* @__PURE__ */ new Set(["mock", "doMock"]);
|
|
10
10
|
const TRANSFORMABLE = /\.(?:[cm]?[jt]sx?)$/;
|
|
11
11
|
const HAS_JEST_MOCK = /\bjest\s*\.\s*(?:mock|unmock|doMock|doUnmock)\s*\(/;
|
|
12
12
|
/** Visit every node in an ESTree AST (depth-first), calling `fn` on each. */
|
package/dist/native/hooks.mjs
CHANGED
|
@@ -6,16 +6,16 @@ import fs from "node:fs";
|
|
|
6
6
|
import { transformRN, isFlow } from "./transform.mjs";
|
|
7
7
|
import { boundarySourceFor } from "./boundary.mjs";
|
|
8
8
|
import { resolvePlatformFile } from "./resolve.mjs";
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
NODE_MODULES_PATH,
|
|
11
|
+
REACT_NATIVE_PATH,
|
|
12
|
+
buildPkgMatcher,
|
|
13
|
+
isUtilitySubpath,
|
|
14
|
+
packageNameOf,
|
|
15
|
+
subpathLeafOf,
|
|
16
|
+
} from "./match.mjs";
|
|
10
17
|
import { explainUntransformedSyntaxError } from "./explain.mjs";
|
|
11
18
|
|
|
12
|
-
const RN_PATH = /[\\/]node_modules[\\/](react-native|@react-native)[\\/]/;
|
|
13
|
-
// Any file under a node_modules directory. Platform-extension resolution
|
|
14
|
-
// (`.native.js` etc.) applies to every node_modules package, not just RN — matching
|
|
15
|
-
// Metro, which resolves platform variants project-wide. See loader.mjs for the
|
|
16
|
-
// ESM-path counterpart and the @react-navigation silent-failure this prevents.
|
|
17
|
-
const NODE_MODULES = /[\\/]node_modules[\\/]/;
|
|
18
|
-
|
|
19
19
|
// Guarded via globalThis, not module scope: under the hot runtime this module
|
|
20
20
|
// can be evaluated twice in one worker (once by the worker entry through Node's
|
|
21
21
|
// loader, once through Vitest's module runner when the setup file is inlined),
|
|
@@ -37,10 +37,74 @@ const NODE_MODULES = /[\\/]node_modules[\\/]/;
|
|
|
37
37
|
const VITE_MAIN_FIELDS = ["react-native", "module", "jsnext:main", "jsnext"];
|
|
38
38
|
|
|
39
39
|
const reportedDuplicates = new Set();
|
|
40
|
+
const reportedProjectLoads = new Set();
|
|
40
41
|
|
|
41
42
|
/** Test seam: the warning is once per package for the life of the worker. */
|
|
42
43
|
export function _resetDuplicateReports() {
|
|
43
44
|
reportedDuplicates.clear();
|
|
45
|
+
reportedProjectLoads.clear();
|
|
46
|
+
cachedProjectDirs = undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Directories Vite owns outright: the package the run lives in, and any package a
|
|
51
|
+
* `test.include` pattern points into (see plugin.ts). Read from the environment
|
|
52
|
+
* because it has to cross into the worker.
|
|
53
|
+
*
|
|
54
|
+
* Parsed once. This is consulted on every module resolution in the worker, so
|
|
55
|
+
* re-reading and re-parsing the variable per call put a JSON.parse in the hot path.
|
|
56
|
+
*/
|
|
57
|
+
let cachedProjectDirs;
|
|
58
|
+
function projectDirs() {
|
|
59
|
+
if (cachedProjectDirs) return cachedProjectDirs;
|
|
60
|
+
try {
|
|
61
|
+
cachedProjectDirs = JSON.parse(process.env.VITEST_NATIVE_PROJECT_DIRS || "[]").map(
|
|
62
|
+
(dir) => dir.replace(/\\/g, "/").replace(/\/+$/, "") + "/",
|
|
63
|
+
);
|
|
64
|
+
} catch {
|
|
65
|
+
cachedProjectDirs = [];
|
|
66
|
+
}
|
|
67
|
+
return cachedProjectDirs;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Say so when Node loads the project's own source.
|
|
72
|
+
*
|
|
73
|
+
* The package under test is Vite's (see the ownership rule in apply.ts). If an
|
|
74
|
+
* installed package requires it as well, it exists twice — once in each graph — and
|
|
75
|
+
* the symptom is silence: a store configured through the test's copy reads back unset
|
|
76
|
+
* through the copy the library sees, so an assertion compares "" against the expected
|
|
77
|
+
* text with nothing pointing at the cause. It is the same failure the
|
|
78
|
+
* duplicate-instance warning above covers, arrived at from the other direction: there
|
|
79
|
+
* the two graphs disagree about which FILE the package is, here they agree on the file
|
|
80
|
+
* and still hold separate instances of it.
|
|
81
|
+
*
|
|
82
|
+
* Only reported when the requirer is an installed package. A test reaching into its
|
|
83
|
+
* own source deliberately — `jest.requireActual('./src/thing')` — is Node loading
|
|
84
|
+
* project files on purpose, and is not this.
|
|
85
|
+
*/
|
|
86
|
+
export function checkProjectSourceLoadedByNode(resolved, parent, dirs = projectDirs()) {
|
|
87
|
+
if (dirs.length === 0 || typeof resolved !== "string") return;
|
|
88
|
+
const file = resolved.replace(/\\/g, "/");
|
|
89
|
+
// Project source never lives under node_modules, and the project directory
|
|
90
|
+
// contains its own node_modules — so this has to be excluded explicitly.
|
|
91
|
+
if (file.includes("/node_modules/")) return;
|
|
92
|
+
if (!dirs.some((dir) => file.startsWith(dir))) return;
|
|
93
|
+
const from = parent && parent.filename ? parent.filename.replace(/\\/g, "/") : "";
|
|
94
|
+
if (!NODE_MODULES_PATH.test(from) || from.includes("/vitest-native/dist/")) return;
|
|
95
|
+
if (reportedProjectLoads.has(file)) return;
|
|
96
|
+
reportedProjectLoads.add(file);
|
|
97
|
+
console.warn(
|
|
98
|
+
`[vitest-native] Node loaded a file from the package under test.\n` +
|
|
99
|
+
` file -> ${resolved}\n` +
|
|
100
|
+
` required by -> ${parent.filename}\n` +
|
|
101
|
+
" The package under test belongs to Vite's graph, so it now exists twice — once\n" +
|
|
102
|
+
" in each module system — and module-level state is not shared between the copies.\n" +
|
|
103
|
+
" Nothing throws: writes through one are invisible to the other, so values read back\n" +
|
|
104
|
+
" unset. This happens when an installed React Native package depends on the very\n" +
|
|
105
|
+
" package whose tests are running. Import it from one side only, or test it from a\n" +
|
|
106
|
+
" package that does not sit underneath the dependency.",
|
|
107
|
+
);
|
|
44
108
|
}
|
|
45
109
|
function reportDuplicateInstance(pkg, nodeFile, viteFile, field) {
|
|
46
110
|
reportedDuplicates.add(pkg);
|
|
@@ -197,8 +261,8 @@ export function installRequireHooks(
|
|
|
197
261
|
if (
|
|
198
262
|
parent &&
|
|
199
263
|
parent.filename &&
|
|
200
|
-
(
|
|
201
|
-
|
|
264
|
+
(NODE_MODULES_PATH.test(parent.filename) ||
|
|
265
|
+
REACT_NATIVE_PATH.test(parent.filename) ||
|
|
202
266
|
isExtra(parent.filename)) &&
|
|
203
267
|
request.startsWith(".") &&
|
|
204
268
|
!path.extname(request)
|
|
@@ -211,6 +275,7 @@ export function installRequireHooks(
|
|
|
211
275
|
}
|
|
212
276
|
const resolved = origResolve.call(this, request, parent, ...rest);
|
|
213
277
|
checkResolverAgreement(request, resolved);
|
|
278
|
+
checkProjectSourceLoadedByNode(resolved, parent);
|
|
214
279
|
return resolved;
|
|
215
280
|
};
|
|
216
281
|
|
|
@@ -219,7 +284,7 @@ export function installRequireHooks(
|
|
|
219
284
|
const norm = filename.replace(/\\/g, "/");
|
|
220
285
|
const boundary = boundarySourceFor(norm, platform, reactNativeVersion);
|
|
221
286
|
if (boundary != null) return mod._compile(boundary, filename);
|
|
222
|
-
if (
|
|
287
|
+
if (REACT_NATIVE_PATH.test(norm)) {
|
|
223
288
|
const src = fs.readFileSync(filename, "utf8");
|
|
224
289
|
if (isFlow(src))
|
|
225
290
|
return mod._compile(transformRN(filename, src, projectRoot, platform), filename);
|
|
@@ -229,7 +294,7 @@ export function installRequireHooks(
|
|
|
229
294
|
const src = fs.readFileSync(filename, "utf8");
|
|
230
295
|
return mod._compile(transformRN(filename, src, projectRoot, platform), filename);
|
|
231
296
|
}
|
|
232
|
-
if (
|
|
297
|
+
if (NODE_MODULES_PATH.test(norm)) {
|
|
233
298
|
// A node_modules package we did NOT transform: when Node's compile throws a
|
|
234
299
|
// SyntaxError that fingerprints as untranspiled JSX/Flow/TS, explain the
|
|
235
300
|
// real fix (add the package to `transform: [...]`) instead of leaving a
|
package/dist/native/loader.mjs
CHANGED
|
@@ -6,15 +6,20 @@ import fs from "node:fs";
|
|
|
6
6
|
import { transformRN, isFlow, cjsExportNames } from "./transform.mjs";
|
|
7
7
|
import { boundarySourceFor } from "./boundary.mjs";
|
|
8
8
|
import { resolvePlatformFile } from "./resolve.mjs";
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
NODE_MODULES_PATH,
|
|
11
|
+
REACT_NATIVE_PATH,
|
|
12
|
+
buildPkgMatcher,
|
|
13
|
+
isUtilitySubpath,
|
|
14
|
+
packageNameOf,
|
|
15
|
+
subpathLeafOf,
|
|
16
|
+
} from "./match.mjs";
|
|
10
17
|
|
|
11
|
-
const RN_PATH = /[\\/]node_modules[\\/](react-native|@react-native)[\\/]/;
|
|
12
18
|
// Any file living under a node_modules directory. Platform-extension resolution
|
|
13
19
|
// (`.native.js` etc.) applies to every node_modules package, not just RN, matching
|
|
14
20
|
// Metro — which resolves platform variants project-wide. (Without this, e.g.
|
|
15
21
|
// `@react-navigation/native` silently loads its `.js`/web variant instead of
|
|
16
22
|
// `.native.js`, breaking the navigation lifecycle with no error.)
|
|
17
|
-
const NODE_MODULES = /[\\/]node_modules[\\/]/;
|
|
18
23
|
// React Native's main entry (`react-native/index.js`).
|
|
19
24
|
const RN_INDEX = /[\\/]react-native[\\/]index\.js$/;
|
|
20
25
|
const TRANSFORMABLE = /\.(jsx?|tsx?|mjs|cjs)$/;
|
|
@@ -90,7 +95,7 @@ export async function resolve(specifier, context, nextResolve) {
|
|
|
90
95
|
let resolved;
|
|
91
96
|
if (
|
|
92
97
|
parent &&
|
|
93
|
-
(
|
|
98
|
+
(NODE_MODULES_PATH.test(parent) || REACT_NATIVE_PATH.test(parent) || isExtra(parent)) &&
|
|
94
99
|
specifier.startsWith(".") &&
|
|
95
100
|
!path.extname(specifier)
|
|
96
101
|
) {
|
|
@@ -183,7 +188,7 @@ export async function load(url, context, nextLoad) {
|
|
|
183
188
|
};
|
|
184
189
|
}
|
|
185
190
|
|
|
186
|
-
const isRN =
|
|
191
|
+
const isRN = REACT_NATIVE_PATH.test(norm);
|
|
187
192
|
if (!isRN && !isExtra(norm)) return nextLoad(url, context);
|
|
188
193
|
|
|
189
194
|
if (isRN) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** Any file under a node_modules directory. */
|
|
2
|
+
export declare const NODE_MODULES_PATH: RegExp;
|
|
3
|
+
|
|
4
|
+
/** React Native's own source, including the `@react-native/*` packages. */
|
|
5
|
+
export declare const REACT_NATIVE_PATH: RegExp;
|
|
6
|
+
|
|
7
|
+
/** Does `dir` contain `target` (or equal it)? Case-insensitive on Windows. */
|
|
8
|
+
export declare function containsPath(dir: string, target: string): boolean;
|
|
9
|
+
|
|
10
|
+
/** The on-disk directory a package resolves to from `projectRoot`, or null. */
|
|
11
|
+
export declare function packageDirOf(name: string, projectRoot: string): string | null;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The patterns identifying one package's files: under `node_modules/<name>/`, and
|
|
15
|
+
* inside the directory it resolves to. The second is omitted when that directory
|
|
16
|
+
* contains `projectRoot` — the project is not one of its own dependencies.
|
|
17
|
+
*/
|
|
18
|
+
export declare function packagePatterns(name: string, projectRoot?: string): RegExp[];
|
|
19
|
+
|
|
20
|
+
/** A predicate over `packagePatterns` for every named package. */
|
|
21
|
+
export declare function buildPkgMatcher(
|
|
22
|
+
pkgs: string[] | undefined,
|
|
23
|
+
projectRoot?: string,
|
|
24
|
+
): (file: string) => boolean;
|
|
25
|
+
|
|
26
|
+
/** The bare package name of an import specifier. */
|
|
27
|
+
export declare function packageNameOf(specifier: string): string;
|
|
28
|
+
|
|
29
|
+
/** The leaf module name a subpath import points at, or null. */
|
|
30
|
+
export declare function subpathLeafOf(specifier: string): string | null;
|
|
31
|
+
|
|
32
|
+
/** Deep entries of preset packages that must not be shadowed by the preset mock. */
|
|
33
|
+
export declare function isUtilitySubpath(specifier: string): boolean;
|