vitest-native 0.14.0-rc.8 → 0.14.0

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 (99) hide show
  1. package/README.md +91 -72
  2. package/dist/cli.d.mts +1 -3
  3. package/dist/cli.mjs +215 -76
  4. package/dist/context.d.mts +45 -0
  5. package/dist/context.mjs +243 -0
  6. package/dist/errors.d.mts +3 -0
  7. package/dist/helpers.cjs +53 -4
  8. package/dist/helpers.d.cts +1 -3
  9. package/dist/helpers.d.mts +1 -3
  10. package/dist/helpers.mjs +2 -3
  11. package/dist/host-5o48Ujzs.mjs +939 -0
  12. package/dist/{index-BSyUeoga.d.mts → index-DqU4YO3D.d.mts} +32 -53
  13. package/dist/index.d.mts +1 -3
  14. package/dist/index.mjs +464 -596
  15. package/dist/jest-compat/error-equality.mjs +127 -0
  16. package/dist/jest-compat/jest-object.mjs +149 -4
  17. package/dist/jest-compat/node-registry.mjs +233 -58
  18. package/dist/jest-compat/setup.mjs +34 -4
  19. package/dist/jest-compat.cjs +227 -52
  20. package/dist/jest-compat.d.cts +5 -6
  21. package/dist/jest-compat.d.mts +5 -6
  22. package/dist/jest-compat.mjs +204 -52
  23. package/dist/matchers.cjs +1 -2
  24. package/dist/matchers.d.cts +0 -2
  25. package/dist/matchers.d.mts +0 -2
  26. package/dist/matchers.mjs +1 -2
  27. package/dist/{memory-FYbP0_gg.mjs → memory-Couw0Esx.mjs} +31 -6
  28. package/dist/native/assets.d.mts +2 -0
  29. package/dist/native/assets.mjs +34 -0
  30. package/dist/native/babel-toolchain.mjs +190 -0
  31. package/dist/native/babel-worker.mjs +47 -0
  32. package/dist/native/boundary.mjs +23 -8
  33. package/dist/native/compile-cache.mjs +21 -9
  34. package/dist/native/coverage-v8.mjs +287 -0
  35. package/dist/native/esm-interop.mjs +216 -0
  36. package/dist/native/expo-native-modules.mjs +7 -1
  37. package/dist/native/globals.mjs +104 -6
  38. package/dist/native/hooks.mjs +271 -60
  39. package/dist/native/loader.mjs +409 -20
  40. package/dist/native/match.d.mts +24 -2
  41. package/dist/native/match.mjs +117 -12
  42. package/dist/native/metro-profile-compiler.mjs +2 -31
  43. package/dist/native/module-reset.mjs +37 -5
  44. package/dist/native/native-modules.d.mts +2 -3
  45. package/dist/native/native-modules.mjs +22 -21
  46. package/dist/native/ownership.d.mts +10 -0
  47. package/dist/native/ownership.mjs +96 -9
  48. package/dist/native/registry-compiler.mjs +6 -0
  49. package/dist/native/registry.mjs +138 -119
  50. package/dist/native/resident-health.mjs +133 -0
  51. package/dist/native/resolve.d.mts +6 -0
  52. package/dist/native/resolve.mjs +100 -9
  53. package/dist/native/rntl-hooks.mjs +157 -6
  54. package/dist/native/rntl-text-host.mjs +64 -0
  55. package/dist/native/runner.mjs +106 -1
  56. package/dist/native/setup.mjs +105 -77
  57. package/dist/native/state-manifest.mjs +14 -8
  58. package/dist/native/transform.d.mts +2 -0
  59. package/dist/native/transform.mjs +476 -59
  60. package/dist/native/worker.mjs +151 -46
  61. package/dist/{pool-DjpOjOf3.mjs → pool-CFSpNfyw.mjs} +37 -11
  62. package/dist/{preset-map-BlBriTA0.mjs → preset-map-CogjYmSw.mjs} +4 -19
  63. package/dist/preset-modules.mjs +2 -11
  64. package/dist/{presets--zrEWQC2.mjs → presets-CXDyg51l.mjs} +65 -82
  65. package/dist/presets.d.mts +1 -1
  66. package/dist/presets.mjs +1 -1
  67. package/dist/report-error.d.mts +20 -0
  68. package/dist/report-error.mjs +145 -0
  69. package/dist/rntl-support.d.mts +12 -0
  70. package/dist/rntl-support.mjs +189 -0
  71. package/dist/{rolldown-runtime-BhDjJH2R.mjs → rolldown-runtime-D70C1uOS.mjs} +1 -2
  72. package/dist/serializer.cjs +1 -2
  73. package/dist/serializer.d.cts +1 -3
  74. package/dist/serializer.d.mts +1 -3
  75. package/dist/serializer.mjs +1 -2
  76. package/dist/setup.mjs +95 -156
  77. package/dist/{worker-vitest-BGPo0ek6.mjs → worker-vitest-CILPFeRX.mjs} +2 -3
  78. package/docs/migrating-from-jest.md +76 -12
  79. package/docs/migrating-from-vitest-react-native.md +1 -1
  80. package/docs/release-readiness.md +6 -5
  81. package/docs/versioning.md +1 -1
  82. package/package.json +121 -103
  83. package/CHANGELOG.md +0 -2475
  84. package/dist/cli.cjs +0 -1924
  85. package/dist/cli.d.cts +0 -3
  86. package/dist/ecosystem-D6e1OVOg.mjs +0 -1019
  87. package/dist/ecosystem-MFwkbnaC.cjs +0 -1099
  88. package/dist/errors-B3lJK46O.cjs +0 -75
  89. package/dist/errors-CxoBUfVJ.mjs +0 -64
  90. package/dist/index-bZ7AHqLc.d.cts +0 -482
  91. package/dist/memory-6EPHrJA1.cjs +0 -144
  92. package/dist/pool-B4yesGhI.cjs +0 -121
  93. package/dist/preset-map-jSFsNy1Q.cjs +0 -241
  94. package/dist/preset-modules-BoLkaeEN.cjs +0 -53
  95. package/dist/preset-modules-CG8-G82P.mjs +0 -36
  96. package/dist/presets-9f83H0WW.cjs +0 -3939
  97. package/dist/rolldown-runtime-C0BPl7ul.cjs +0 -43
  98. package/dist/worker-vitest-BfiQB8ND.cjs +0 -62
  99. package/docs/hot-default-design.md +0 -216
@@ -0,0 +1,189 @@
1
+ // React Native Testing Library behaviour that RNTL only provides under Jest-style
2
+ // globals, installed by both engines' setup files. Plain .mjs, shipped verbatim: the
3
+ // native runtime (native/setup.mjs, native/rntl-hooks.mjs) imports it by relative path,
4
+ // and the mock engine's bundled setup.ts imports the same source. The two copies share
5
+ // state only through the global symbol below, never through module identity.
6
+ import fs from "node:fs";
7
+ import Module from "node:module";
8
+ import { setImmediate as realSetImmediate } from "node:timers";
9
+
10
+ /** RNTL's package entry: `build/index.js` (12, 13) or `dist/index.js` (14). */
11
+ export const RNTL_ENTRY =
12
+ /[\\/]node_modules[\\/]@testing-library[\\/]react-native[\\/](?:build|dist)[\\/]index\.js$/;
13
+
14
+ // Per-file auto-cleanup state, created by installRntlAutoCleanup. When files share a
15
+ // worker, native/rntl-hooks.mjs and native/hooks.mjs add the RNTL entries a file loads
16
+ // to `imported`.
17
+ const AUTO_CLEANUP = Symbol.for("vitest-native.rntl-auto-cleanup");
18
+
19
+ // The key Node's module cache holds `file` under, if it is cached.
20
+ function cacheKeyOf(file) {
21
+ if (Module._cache[file]) return file;
22
+ try {
23
+ const real = fs.realpathSync(file);
24
+ return Module._cache[real] ? real : null;
25
+ } catch {
26
+ return null;
27
+ }
28
+ }
29
+
30
+ /**
31
+ * The RNTL entries the running test file's own module graph imports, read from
32
+ * Vitest's evaluated-module graph: the modules Vite evaluates, and for each module it
33
+ * externalizes to Node, that module and the modules it required directly (a shared
34
+ * test-utils that re-exports RNTL). With `isolate: false` and no hot runtime both the
35
+ * graph and Node's module cache persist across files, so a later file's import loads
36
+ * nothing again; the graph still records that the file imports it.
37
+ */
38
+ function rntlEntriesInFileGraph() {
39
+ const worker = globalThis.__vitest_worker__;
40
+ const modules = worker?.evaluatedModules;
41
+ if (typeof worker?.filepath !== "string" || typeof modules?.getModuleById !== "function") {
42
+ return [];
43
+ }
44
+ const found = [];
45
+ const seen = new Set();
46
+ const queue = [...(modules.getModulesByFile?.(worker.filepath) ?? [])];
47
+ while (queue.length > 0) {
48
+ const node = queue.pop();
49
+ if (!node || seen.has(node)) continue;
50
+ seen.add(node);
51
+ const external = node.meta?.externalize;
52
+ if (typeof external === "string") {
53
+ const key = cacheKeyOf(external);
54
+ if (!key) continue;
55
+ if (RNTL_ENTRY.test(key)) found.push(key);
56
+ for (const child of Module._cache[key].children ?? []) {
57
+ if (RNTL_ENTRY.test(child.id)) found.push(child.id);
58
+ }
59
+ continue;
60
+ }
61
+ for (const id of node.imports ?? []) queue.push(modules.getModuleById(id));
62
+ }
63
+ return found;
64
+ }
65
+
66
+ /**
67
+ * Register RNTL's automatic cleanup through Vitest's `afterEach` when RNTL cannot.
68
+ *
69
+ * RNTL's entry registers it only if `afterEach` is a global (12.9 build/index.js:25,
70
+ * 13.3 build/index.js:27, 14.1 dist/index.js:26: `if (typeof afterEach === 'function')`).
71
+ * Vitest defines its globals only with `test.globals: true`, so with Vitest's default
72
+ * every rendered tree stayed mounted for the rest of the file: `screen` and later
73
+ * queries saw earlier tests' screens, and their effects kept running. With globals on,
74
+ * RNTL registers its own hook and nothing is added here.
75
+ *
76
+ * The hook does what RNTL's would, and only when RNTL's would exist:
77
+ * - only if this file loaded RNTL's main entry. Importing
78
+ * `@testing-library/react-native/pure` is RNTL's documented opt-out, so the setup
79
+ * files never load the entry themselves and it is detected lazily, when the hook runs;
80
+ * - not when RNTL_SKIP_AUTO_CLEANUP is set, tested by truthiness as RNTL does
81
+ * (12.9 build/index.js:19, 13.3 :21, 14.1 dist/index.js:20); its
82
+ * `dont-cleanup-after-each` module sets it;
83
+ * - the cleanup each version's own hook calls: 12's synchronous `cleanup`
84
+ * (12.9 build/index.js:29), 13's `cleanupAsync` (13.3 build/index.js:30; its
85
+ * `cleanup` is the synchronous one), and 14's `cleanup`, which is async and has no
86
+ * `cleanupAsync` (14.1 dist/index.js:29, dist/cleanup.js:11);
87
+ * - after one macrotask turn, as RNTL's `flushMicroTasks` (13.3/14.1
88
+ * flush-micro-tasks.js:9) or 12's `flushMicroTasksLegacy` (12.9 :27) wait. RNTL uses
89
+ * the `setImmediate` it captured when it loaded (12.9/13.3 build/helpers/timers.js:67,
90
+ * 14.1 dist/helpers/timers.js:74), not the current one, so a test that ends with fake
91
+ * timers still installed does not hang its cleanup. This hook's own wait uses
92
+ * `node:timers`' `setImmediate`, which Vitest's fake timers never replace: a capture
93
+ * of the global when the setup file runs is not enough, because under
94
+ * `isolate: false` Vitest leaves the previous file's fake timers installed, so the
95
+ * setup file of a later file would capture a fake one and every cleanup would hang. (That RNTL's capture is the
96
+ * real one even when fake timers are on at load is native/rntl-hooks.mjs's `jest`.)
97
+ *
98
+ * Order: Vitest runs `afterEach` hooks in reverse order of registration (its default
99
+ * `sequence.hooks: 'stack'`), and a setup file registers before the test file, so this
100
+ * cleanup runs after the file's own `afterEach` hooks. That is where RNTL's own hook
101
+ * runs under Vitest with `test.globals: true` too, since the entry registers it on
102
+ * import. Jest runs RNTL's cleanup before the file's hooks, so an `afterEach` that
103
+ * expects the tree to be unmounted already sees it still mounted here.
104
+ *
105
+ * RNTL's entry also pairs `beforeAll`/`afterAll` to set React's act environment for the
106
+ * file (12.9 build/index.js:32). Both engines set IS_REACT_ACT_ENVIRONMENT for every
107
+ * file already, so that pair is not reproduced.
108
+ *
109
+ * @param {(fn: () => Promise<void>) => void} afterEach Vitest's `afterEach`.
110
+ * @param {{ trackImports?: boolean }} [options] `trackImports`: set when files share a
111
+ * worker (the hot runtime, or `isolate: false`). RNTL then stays in Node's module
112
+ * cache across files, so the cache cannot tell whether this file loaded it, and a
113
+ * `/pure` file after one that imported the entry would be cleaned up. Instead the
114
+ * entries this file loads are recorded (native/rntl-hooks.mjs for its imports, and
115
+ * noteIfRntlEntryLoaded for Node's requires) and its module graph is read
116
+ * (rntlEntriesInFileGraph).
117
+ */
118
+ export function installRntlAutoCleanup(afterEach, { trackImports = false } = {}) {
119
+ if (typeof globalThis.afterEach === "function") return;
120
+ const state = { imported: trackImports ? new Set() : null };
121
+ Object.defineProperty(globalThis, AUTO_CLEANUP, {
122
+ configurable: true,
123
+ enumerable: false,
124
+ value: state,
125
+ writable: true,
126
+ });
127
+ afterEach(async () => {
128
+ if (process.env.RNTL_SKIP_AUTO_CLEANUP) return;
129
+ const entries = [];
130
+ const files = state.imported
131
+ ? new Set([...state.imported, ...rntlEntriesInFileGraph()])
132
+ : Object.keys(Module._cache);
133
+ for (const file of files) {
134
+ if (!file.endsWith("index.js") || !RNTL_ENTRY.test(file)) continue;
135
+ const rntl = Module._cache[file]?.exports;
136
+ const cleanup = rntl?.cleanupAsync ?? rntl?.cleanup;
137
+ if (typeof cleanup === "function") entries.push(cleanup);
138
+ }
139
+ if (entries.length === 0) return;
140
+ await new Promise((resolve) => realSetImmediate(resolve));
141
+ for (const cleanup of entries) await cleanup();
142
+ });
143
+ }
144
+
145
+ /**
146
+ * Record the RNTL entry whose `exports` a `require` just returned, cached or not, if
147
+ * installRntlAutoCleanup is tracking loads for this file. Called with every require's
148
+ * result, so anything that is not an RNTL entry's exports leaves after two property
149
+ * checks; the cache is searched only for an object with RNTL's `render` and `cleanup`.
150
+ * @param {unknown} exports
151
+ */
152
+ export function noteIfRntlEntryLoaded(exports) {
153
+ if (exports === null || typeof exports !== "object") return;
154
+ if (!globalThis[AUTO_CLEANUP]?.imported) return;
155
+ if (typeof exports.render !== "function" || typeof exports.cleanup !== "function") return;
156
+ for (const key of Object.keys(Module._cache)) {
157
+ if (RNTL_ENTRY.test(key) && Module._cache[key].exports === exports) {
158
+ noteRntlEntryImported(key);
159
+ }
160
+ }
161
+ }
162
+
163
+ const LOAD_TRACKED = Symbol.for("vitest-native.rntl-load-tracking");
164
+
165
+ /**
166
+ * Report every require's result to noteIfRntlEntryLoaded, for an engine without its
167
+ * own require hook (the mock engine; the native engine calls it from hooks.mjs). Wraps
168
+ * `Module._load` once per worker.
169
+ */
170
+ export function installRntlLoadTracking() {
171
+ if (Module._load[LOAD_TRACKED]) return;
172
+ const load = Module._load;
173
+ const tracked = function (...args) {
174
+ const exports = load.apply(this, args);
175
+ noteIfRntlEntryLoaded(exports);
176
+ return exports;
177
+ };
178
+ tracked[LOAD_TRACKED] = true;
179
+ Module._load = tracked;
180
+ }
181
+
182
+ /**
183
+ * Record that the current file imported the RNTL entry cached under `file`. A no-op
184
+ * unless installRntlAutoCleanup is tracking imports for this file.
185
+ * @param {string} file
186
+ */
187
+ export function noteRntlEntryImported(file) {
188
+ globalThis[AUTO_CLEANUP]?.imported?.add(file);
189
+ }
@@ -1,5 +1,5 @@
1
1
  import { createRequire } from "node:module";
2
- //#region \0rolldown/runtime.js
2
+ // \0rolldown/runtime.js
3
3
  var __defProp = Object.defineProperty;
4
4
  var __exportAll = (all, no_symbols) => {
5
5
  let target = {};
@@ -11,5 +11,4 @@ var __exportAll = (all, no_symbols) => {
11
11
  return target;
12
12
  };
13
13
  var __require = /* #__PURE__ */ (() => createRequire(import.meta.url))();
14
- //#endregion
15
14
  export { __require as n, __exportAll as t };
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- //#region src/serializer.ts
2
+ // src/serializer.ts
3
3
  /**
4
4
  * Vitest snapshot serializer for React Native components.
5
5
  *
@@ -146,5 +146,4 @@ const serializer = {
146
146
  return result;
147
147
  }
148
148
  };
149
- //#endregion
150
149
  exports.serializer = serializer;
@@ -1,4 +1,3 @@
1
- //#region src/serializer.d.ts
2
1
  /**
3
2
  * Vitest snapshot serializer for React Native components.
4
3
  *
@@ -11,5 +10,4 @@ export interface SnapshotSerializer {
11
10
  serialize(val: any, config: any, indentation: string, depth: number, refs: any[], printer: Function): string;
12
11
  test(val: any): boolean;
13
12
  }
14
- export declare const serializer: SnapshotSerializer;
15
- //#endregion
13
+ export declare const serializer: SnapshotSerializer;
@@ -1,4 +1,3 @@
1
- //#region src/serializer.d.ts
2
1
  /**
3
2
  * Vitest snapshot serializer for React Native components.
4
3
  *
@@ -11,5 +10,4 @@ export interface SnapshotSerializer {
11
10
  serialize(val: any, config: any, indentation: string, depth: number, refs: any[], printer: Function): string;
12
11
  test(val: any): boolean;
13
12
  }
14
- export declare const serializer: SnapshotSerializer;
15
- //#endregion
13
+ export declare const serializer: SnapshotSerializer;
@@ -1,4 +1,4 @@
1
- //#region src/serializer.ts
1
+ // src/serializer.ts
2
2
  /**
3
3
  * Vitest snapshot serializer for React Native components.
4
4
  *
@@ -145,5 +145,4 @@ const serializer = {
145
145
  return result;
146
146
  }
147
147
  };
148
- //#endregion
149
148
  export { serializer };