vitest-native 0.14.0 → 0.15.0-rc.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.
Files changed (59) hide show
  1. package/README.md +94 -976
  2. package/dist/cli.d.mts +9 -1
  3. package/dist/cli.mjs +2607 -303
  4. package/dist/context.d.mts +15 -0
  5. package/dist/context.mjs +39 -1
  6. package/dist/errors.d.mts +20 -1
  7. package/dist/helpers.cjs +22 -1
  8. package/dist/helpers.mjs +22 -1
  9. package/dist/{host-5o48Ujzs.mjs → host-BAMNg80P.mjs} +269 -103
  10. package/dist/{index-DqU4YO3D.d.mts → index-CrCsGULN.d.mts} +84 -8
  11. package/dist/index.d.mts +2 -1
  12. package/dist/index.mjs +624 -145
  13. package/dist/jest-compat/interop.mjs +1 -1
  14. package/dist/jest-compat/jest-object.mjs +3 -4
  15. package/dist/jest-compat/node-registry.mjs +791 -67
  16. package/dist/jest-compat/setup.mjs +4 -4
  17. package/dist/matchers.d.cts +0 -1
  18. package/dist/matchers.d.mts +0 -1
  19. package/dist/native/bounded-process.mjs +63 -5
  20. package/dist/native/bridge-client.mjs +133 -0
  21. package/dist/native/bridge-server.mjs +215 -0
  22. package/dist/native/bridge-thread.mjs +79 -0
  23. package/dist/native/bridge.mjs +512 -0
  24. package/dist/native/explain.mjs +1 -2
  25. package/dist/native/export-condition-recovery.mjs +20 -99
  26. package/dist/native/hooks.mjs +254 -123
  27. package/dist/native/jest-resolvers.mjs +94 -0
  28. package/dist/native/library-jest.mjs +346 -0
  29. package/dist/native/loader.mjs +167 -70
  30. package/dist/native/match.mjs +30 -1
  31. package/dist/native/module-reset.mjs +44 -49
  32. package/dist/native/ownership-ledger.d.mts +77 -0
  33. package/dist/native/ownership-ledger.mjs +467 -0
  34. package/dist/native/ownership.d.mts +5 -0
  35. package/dist/native/ownership.mjs +30 -0
  36. package/dist/native/registry-compiler.mjs +39 -5
  37. package/dist/native/registry-process.mjs +327 -28
  38. package/dist/native/registry.mjs +750 -200
  39. package/dist/native/resolve.mjs +110 -4
  40. package/dist/native/rntl-hooks.mjs +8 -5
  41. package/dist/native/rntl-text-host.mjs +5 -15
  42. package/dist/native/setup.mjs +117 -36
  43. package/dist/native/shared-resolver.d.mts +141 -0
  44. package/dist/native/shared-resolver.mjs +719 -0
  45. package/dist/native/transform.mjs +4 -5
  46. package/dist/native/worker.mjs +9 -4
  47. package/dist/node-dispatch.mjs +291 -0
  48. package/dist/{preset-map-CogjYmSw.mjs → preset-map-DdAexIwz.mjs} +113 -2
  49. package/dist/{presets-CXDyg51l.mjs → presets-CsQynQ0A.mjs} +620 -78
  50. package/dist/presets.d.mts +1 -1
  51. package/dist/presets.mjs +1 -1
  52. package/dist/rntl-matchers.d.ts +11 -15
  53. package/dist/rntl-support.mjs +27 -13
  54. package/dist/setup.mjs +4 -4
  55. package/docs/migrating-from-jest.md +9 -230
  56. package/docs/migrating-from-vitest-react-native.md +4 -4
  57. package/docs/release-readiness.md +9 -11
  58. package/docs/versioning.md +38 -9
  59. package/package.json +33 -13
@@ -17,19 +17,31 @@ export interface ProjectContextFields {
17
17
  sourceMaps?: boolean;
18
18
  hotReleaseGenerations?: boolean;
19
19
  registry?: string;
20
+ /** A registry compiling in the background on a cache miss (see activeRegistry). */
21
+ registryPending?: { file: string; since: number };
22
+ /** The library registry layered on `registry` (registry.mjs, buildLibraryRegistry). */
23
+ libraryRegistry?: string;
20
24
  ownership?: Record<string, unknown>;
25
+ ownershipGuard?: { mode: "warn" | "error"; allow: { path: string; reason: string }[] };
21
26
  projectDirs?: string[];
22
27
  requireAliases?: [string, string][];
23
28
  requireAliasesSkipped?: string[];
24
29
  presetNames?: string[];
30
+ /** experimental.nodeMocks: a `vi.mock` applies to the modules Node loads. */
31
+ nodeMocks?: true;
25
32
  presetConfig?: Record<string, Record<string, unknown>>;
26
33
  presetExports?: Record<string, string[] | null>;
27
34
  presetModules?: string[];
28
35
  presetDisabled?: string[];
36
+ librarySetupFiles?: string[];
29
37
  mocks?: unknown;
30
38
  knownNativeModules?: string[];
31
39
  nativeModuleSpecs?: Record<string, string[]>;
32
40
  nativeModules?: "permissive";
41
+ /** experimental.ownershipModel (ADR-0004/0005): absent unless the project opts in. */
42
+ ownershipModel?: "resolver" | "resolution";
43
+ resolverInputs?: import("./native/shared-resolver.mjs").ResolverInputs;
44
+ resolverInputsHash?: string;
33
45
  /** The supported Expo SDKs, `[minimum, maximum]` (checkLoadedExpoSdk). */
34
46
  expoSdks?: [number, number];
35
47
  }
@@ -41,5 +53,8 @@ export declare function writeProjectContext(
41
53
  export declare function ensureProjectContext(ref: string, fields: ProjectContextFields): string;
42
54
  export declare function projectContext(ref?: string): Readonly<ProjectContextFields> | null;
43
55
  export declare function requireProjectContext(ref?: string): Readonly<ProjectContextFields>;
56
+ export declare function activeRegistry(
57
+ context: Readonly<ProjectContextFields> | null,
58
+ ): string | null;
44
59
  export declare function markBootContext(ref?: string): void;
45
60
  export declare function assertBootContext(ref?: string): void;
package/dist/context.mjs CHANGED
@@ -14,7 +14,6 @@ export const CONTEXT_VERSION = 1;
14
14
 
15
15
  /** Hand-set diagnostic switches that stay environment variables. */
16
16
  export const ESCAPE_HATCHES = Object.freeze([
17
- "VITEST_NATIVE_LOADER_THREAD",
18
17
  "VITEST_NATIVE_NO_REGISTRY",
19
18
  "VITEST_NATIVE_HOT_ESM_GEN",
20
19
  "VITEST_NATIVE_HOT_STATE_MUTATION",
@@ -38,7 +37,12 @@ const SCHEMA = {
38
37
  sourceMaps: { valid: (v) => typeof v === "boolean" },
39
38
  hotReleaseGenerations: { valid: (v) => typeof v === "boolean" },
40
39
  registry: { valid: absolute },
40
+ registryPending: {
41
+ valid: (v) => object(v) && absolute(v.file) && typeof v.since === "number",
42
+ },
43
+ libraryRegistry: { valid: absolute },
41
44
  ownership: { valid: object },
45
+ ownershipGuard: { valid: object },
42
46
  projectDirs: { valid: strings },
43
47
  requireAliases: {
44
48
  valid: (v) =>
@@ -46,14 +50,20 @@ const SCHEMA = {
46
50
  },
47
51
  requireAliasesSkipped: { valid: strings },
48
52
  presetNames: { valid: strings },
53
+ nodeMocks: { valid: (v) => v === true },
49
54
  presetConfig: { valid: object },
50
55
  presetExports: { valid: object },
51
56
  presetModules: { valid: strings },
52
57
  presetDisabled: { valid: strings },
58
+ librarySetupFiles: { valid: strings },
53
59
  mocks: { valid: (v) => v !== undefined },
54
60
  knownNativeModules: { valid: strings },
55
61
  nativeModuleSpecs: { valid: object },
56
62
  nativeModules: { valid: (v) => v === "permissive" },
63
+ // experimental.ownershipModel (ADR-0004/0005): absent unless the project opts in.
64
+ ownershipModel: { valid: (v) => v === "resolver" || v === "resolution" },
65
+ resolverInputs: { valid: object },
66
+ resolverInputsHash: { valid: (v) => typeof v === "string" },
57
67
  expoSdks: {
58
68
  valid: (v) => Array.isArray(v) && v.length === 2 && v.every((n) => Number.isInteger(n)),
59
69
  },
@@ -221,6 +231,34 @@ export function requireProjectContext(ref = process.env[CONTEXT_ENV]) {
221
231
 
222
232
  const HANDLE = Symbol.for("vitest-native");
223
233
 
234
+ /**
235
+ * The precompiled React Native registry this realm uses, or null for per-file loading.
236
+ *
237
+ * `registry` is a registry the plugin found warm. `registryPending` is one it started
238
+ * compiling in the background on a cache miss (registry-process.mjs): a realm that
239
+ * starts after the compiler published it uses it, one that starts before keeps loading
240
+ * React Native file by file. The metadata file is published last, so a change in its
241
+ * mtime from `since` means a complete registry is in place; the worker loads it like
242
+ * any other and falls back to per-file loading if it cannot.
243
+ *
244
+ * Decided once per realm and shared by every module instance in it, so the setup file
245
+ * and the jest-compat registry agree, and a realm that loaded React Native file by file
246
+ * never switches to the registry's second copy of it partway through.
247
+ */
248
+ export function activeRegistry(context) {
249
+ if (!context) return null;
250
+ if (context.registry) return context.registry;
251
+ const pending = context.registryPending;
252
+ if (!pending) return null;
253
+ const decisions = ((globalThis[HANDLE] ??= {}).registryDecisions ??= new Map());
254
+ if (decisions.has(pending.file)) return decisions.get(pending.file);
255
+ const meta = fs.statSync(`${pending.file}.json`, { throwIfNoEntry: false });
256
+ const file =
257
+ meta && meta.mtimeMs !== pending.since && fs.existsSync(pending.file) ? pending.file : null;
258
+ decisions.set(pending.file, file);
259
+ return file;
260
+ }
261
+
224
262
  /** Hot worker boot: record the project this worker serves. */
225
263
  export function markBootContext(ref = process.env[CONTEXT_ENV]) {
226
264
  (globalThis[HANDLE] ??= {}).bootContext = ref;
package/dist/errors.d.mts CHANGED
@@ -11,11 +11,24 @@ export type VitestNativeErrorCode =
11
11
  | "INVALID_OPTION"
12
12
  | "UNKNOWN_OPTION"
13
13
  | "UNSUPPORTED_PEER"
14
+ | "UNSUPPORTED_NODE"
14
15
  | "VITEST_TWIN_INSTANCE"
15
16
  | "CONTEXT_INVALID"
17
+ | "RESOLVER_INPUT_MISMATCH"
18
+ | "RESOLVER_INPUTS_MISSING"
19
+ | "RESOLVER_ERROR"
20
+ | "BRIDGE_UNAVAILABLE"
21
+ | "BRIDGE_FILE_REFUSED"
22
+ | "VITE_FETCH_TIMEOUT"
23
+ | "VITE_FETCH_FAILED"
24
+ | "BRIDGE_CONTRACT_CHANGED"
25
+ | "REQUIRE_OF_ASYNC_VITE_MODULE"
26
+ | "REQUIRE_OF_EVALUATING_VITE_MODULE"
27
+ | "BRIDGED_EVALUATION_FAILED"
16
28
  | "UNSUPPORTED_POOL"
17
29
  | "INLINE_BREAKS_OWNERSHIP"
18
30
  | "MODULE_OWNER_CONFLICT"
31
+ | "SOURCE_EVALUATED_TWICE"
19
32
  | "ENGINE_REQUIRES_BABEL"
20
33
  | "MOCKS_REQUIRE_MOCK_ENGINE"
21
34
  | "METRO_CONFIG_LOAD_FAILED"
@@ -33,6 +46,8 @@ export type VitestNativeErrorCode =
33
46
  | "HOT_STATE_CAPTURE_FAILED"
34
47
  | "HOT_STATE_RESTORE_FAILED"
35
48
  | "HOT_CJS_CACHE_RESET"
49
+ | "HOT_RESIDENT_UNUSABLE"
50
+ | "HOT_FILE_NOT_RUN"
36
51
  | "PRESET_UNAVAILABLE"
37
52
  | "PRESET_NOT_PREPARED"
38
53
  | "WRONG_ENGINE_FOR_HELPER"
@@ -42,8 +57,12 @@ export type VitestNativeErrorCode =
42
57
  | "JEST_MOCK_FACTORY_CYCLE"
43
58
  | "JEST_COMPAT_SETUP_MISSING"
44
59
  | "BABEL_IGNORES_FILE"
60
+ | "LIBRARY_REGISTRY_FAILED"
61
+ | "UNKNOWN_DISPATCH_STEP"
62
+ | "MOCK_NOT_SEEN_BY_NODE"
45
63
  | "IMPORT_META_IN_NODE_OWNED"
46
- | "MATCHER_BAD_RECEIVER";
64
+ | "MATCHER_BAD_RECEIVER"
65
+ | "COMPARE_REPORT_INVALID";
47
66
 
48
67
  export interface VitestNativeErrorOptions {
49
68
  cause?: unknown;
package/dist/helpers.cjs CHANGED
@@ -94,9 +94,28 @@ function setColorScheme(scheme) {
94
94
  }
95
95
  function setInsets(insets) {
96
96
  const safeArea = globalThis.__vitest_native_preset_mocks?.["react-native-safe-area-context"];
97
- if (safeArea?._setInsets) safeArea._setInsets(insets);
97
+ if (safeArea?._setInsets) {
98
+ safeArea._setInsets(insets);
99
+ return;
100
+ }
101
+ const hook = safeArea?.useSafeAreaInsets;
102
+ if (typeof hook?.mockImplementation === "function" && typeof hook.getMockImplementation === "function") {
103
+ const current = hook.getMockImplementation();
104
+ presetMockUndos.push(() => hook.mockImplementation(current));
105
+ hook.mockImplementation((...args) => ({
106
+ ...current ? current(...args) : {
107
+ top: 0,
108
+ right: 0,
109
+ bottom: 0,
110
+ left: 0
111
+ },
112
+ ...insets
113
+ }));
114
+ }
98
115
  }
99
116
  let presetMockOverrides = [];
117
+ /** Undos of what a helper changed on a library's own mock (setInsets), replayed by resetAllMocks(). */
118
+ let presetMockUndos = [];
100
119
  /**
101
120
  * Merge overrides into a preset's module mock (and its `default`, when that is
102
121
  * an object) — e.g. to give expo-constants a real `expoConfig` or add an export
@@ -126,6 +145,8 @@ function extendPresetMock(pkg, overrides) {
126
145
  return true;
127
146
  }
128
147
  function restorePresetMockOverrides() {
148
+ for (let i = presetMockUndos.length - 1; i >= 0; i--) presetMockUndos[i]();
149
+ presetMockUndos = [];
129
150
  for (let i = presetMockOverrides.length - 1; i >= 0; i--) {
130
151
  const { target, key, had, previous } = presetMockOverrides[i];
131
152
  if (key === "__proto__" || key === "constructor" || key === "prototype") continue;
package/dist/helpers.mjs CHANGED
@@ -43,9 +43,28 @@ function setColorScheme(scheme) {
43
43
  }
44
44
  function setInsets(insets) {
45
45
  const safeArea = globalThis.__vitest_native_preset_mocks?.["react-native-safe-area-context"];
46
- if (safeArea?._setInsets) safeArea._setInsets(insets);
46
+ if (safeArea?._setInsets) {
47
+ safeArea._setInsets(insets);
48
+ return;
49
+ }
50
+ const hook = safeArea?.useSafeAreaInsets;
51
+ if (typeof hook?.mockImplementation === "function" && typeof hook.getMockImplementation === "function") {
52
+ const current = hook.getMockImplementation();
53
+ presetMockUndos.push(() => hook.mockImplementation(current));
54
+ hook.mockImplementation((...args) => ({
55
+ ...current ? current(...args) : {
56
+ top: 0,
57
+ right: 0,
58
+ bottom: 0,
59
+ left: 0
60
+ },
61
+ ...insets
62
+ }));
63
+ }
47
64
  }
48
65
  let presetMockOverrides = [];
66
+ /** Undos of what a helper changed on a library's own mock (setInsets), replayed by resetAllMocks(). */
67
+ let presetMockUndos = [];
49
68
  /**
50
69
  * Merge overrides into a preset's module mock (and its `default`, when that is
51
70
  * an object) — e.g. to give expo-constants a real `expoConfig` or add an export
@@ -75,6 +94,8 @@ function extendPresetMock(pkg, overrides) {
75
94
  return true;
76
95
  }
77
96
  function restorePresetMockOverrides() {
97
+ for (let i = presetMockUndos.length - 1; i >= 0; i--) presetMockUndos[i]();
98
+ presetMockUndos = [];
78
99
  for (let i = presetMockOverrides.length - 1; i >= 0; i--) {
79
100
  const { target, key, had, previous } = presetMockOverrides[i];
80
101
  if (key === "__proto__" || key === "constructor" || key === "prototype") continue;