vitest-native 0.14.0-rc.3 → 0.14.0-rc.5

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 (50) hide show
  1. package/CHANGELOG.md +161 -0
  2. package/README.md +31 -24
  3. package/dist/cli.cjs +1309 -123
  4. package/dist/cli.mjs +1309 -123
  5. package/dist/{ecosystem-DI1AhK_k.cjs → ecosystem-Bg7DgFeG.cjs} +4 -2
  6. package/dist/{ecosystem-DFbU_MPe.mjs → ecosystem-lBGWAeOw.mjs} +4 -2
  7. package/dist/errors.d.mts +4 -0
  8. package/dist/{index-BxzW2D7K.d.cts → index-B40lg28R.d.cts} +15 -1
  9. package/dist/{index-CEbwa4QU.d.mts → index-D7-7BtcM.d.mts} +15 -1
  10. package/dist/index.d.mts +1 -1
  11. package/dist/index.mjs +439 -51
  12. package/dist/jest-compat/interop.mjs +78 -4
  13. package/dist/jest-compat/jest-globals.mjs +5 -2
  14. package/dist/jest-compat/jest-object.mjs +203 -0
  15. package/dist/jest-compat/node-registry.mjs +434 -0
  16. package/dist/jest-compat/setup.mjs +112 -73
  17. package/dist/jest-compat.cjs +34 -2
  18. package/dist/jest-compat.d.cts +5 -0
  19. package/dist/jest-compat.d.mts +5 -0
  20. package/dist/jest-compat.mjs +34 -2
  21. package/dist/native/assets.d.mts +39 -0
  22. package/dist/native/assets.mjs +424 -0
  23. package/dist/native/boundary.mjs +46 -4
  24. package/dist/native/hooks.mjs +110 -41
  25. package/dist/native/loader.mjs +193 -41
  26. package/dist/native/match.d.mts +10 -0
  27. package/dist/native/match.mjs +89 -0
  28. package/dist/native/module-reset.mjs +29 -0
  29. package/dist/native/native-modules.d.mts +18 -0
  30. package/dist/native/native-modules.mjs +245 -0
  31. package/dist/native/nitro.mjs +60 -0
  32. package/dist/native/registry.mjs +36 -12
  33. package/dist/native/reset.mjs +29 -0
  34. package/dist/native/resolve.mjs +74 -8
  35. package/dist/native/setup.mjs +54 -13
  36. package/dist/native/state-manifest.mjs +2 -0
  37. package/dist/native/transform.mjs +157 -17
  38. package/dist/native/tsconfig-paths.d.mts +5 -0
  39. package/dist/native/tsconfig-paths.mjs +163 -0
  40. package/dist/native/worker.mjs +6 -10
  41. package/dist/preset-map-mriqY254.mjs +164 -0
  42. package/dist/preset-map-mzfu_YUy.cjs +202 -0
  43. package/dist/presets.d.mts +1 -1
  44. package/dist/setup.mjs +65 -7
  45. package/docs/migrating-from-jest.md +5 -3
  46. package/docs/release-readiness.md +11 -0
  47. package/docs/versioning.md +1 -1
  48. package/package.json +3 -2
  49. package/dist/preset-map-BoG19dLa.cjs +0 -35
  50. package/dist/preset-map-DuX0ocdn.mjs +0 -30
package/CHANGELOG.md CHANGED
@@ -15,11 +15,98 @@
15
15
  - **Expo path aliases resolve.** In an Expo project on Vite 8, imports through tsconfig `paths` (the
16
16
  template's `@/…`) now resolve as Metro resolves them; a `vite-tsconfig-paths` plugin or a hand-written
17
17
  alias for them can go.
18
+ - **Vitest 5 keeps transformed modules on disk.** The plugin turns on Vitest's `fsModuleCache` unless
19
+ your config sets it, keyed so that upgrading vitest-native or any dependency invalidates it. Set
20
+ `test.fsModuleCache: false` to opt out.
21
+ - **react-native-mmkv 3 and later run as themselves.** They detect Vitest and use their own in-memory
22
+ backend, so the `mmkv` preset now applies to mmkv 2 only. Tests get mmkv's real API (v4's `remove`)
23
+ and change listeners instead of the preset's approximation.
24
+ - **jest-compat follows Jest more closely.** A name a `jest.mock` factory leaves out is `undefined`
25
+ instead of an error, `new` on a `jest.fn` with an arrow implementation works, and
26
+ `process.env.JEST_WORKER_ID` is set, so code that checks for Jest takes its test branch.
18
27
  - **`hotRuntime: true` is stricter.** Combined with a configured pool, `test.isolate`, or a conflicting
19
28
  CLI flag, it now fails at startup with `HOT_RUNTIME_OVERRIDDEN` instead of warning and overriding.
29
+ - **Native modules answer as on a device.** Under the native engine, `NativeModules[name]` and
30
+ `TurboModuleRegistry.get(name)` are `undefined`/`null` for a module no app registers, and a React
31
+ Native module's stub has only the methods its codegen spec declares. Register a module a test needs
32
+ with `mockNativeModule(name, impl)`; `nativeModules: 'permissive'` restores the earlier behaviour.
33
+ - **Assets are registered ids.** `require('./logo.png')` evaluates to the number React Native's asset
34
+ registry assigned, as with Metro, instead of the file name. Snapshots that contained an asset's name
35
+ change; re-record them with `-u`.
36
+ - **`jest.mock` reaches `require()`.** With jest-compat, a `jest.mock` factory now applies to
37
+ `require()`, `jest.requireActual`'s dependencies and React Native's own requires of a mocked internal;
38
+ `jest.isolateModules` and `jest.isolateModulesAsync` work. TypeScript app files that Node loads are
39
+ compiled with the project's Babel config, as babel-jest compiles them.
20
40
 
21
41
  ### Minor Changes
22
42
 
43
+ - 6724815: Native engine: native module lookups answer the way a device does
44
+
45
+ `NativeModules[name]` and `TurboModuleRegistry.get(name)` returned a stub for every name, so a module that no app registers looked present. Library feature detection then took the wrong branch: expo-constants checks `NativeModules.EXDevLauncher` and, finding it, threw at import while parsing the stub's `manifestString` as JSON.
46
+
47
+ React Native returns `undefined` from `NativeModules[name]` and `null` from `TurboModuleRegistry.get(name)` for an unregistered module, and Jest's React Native preset does the same. The native engine now does too:
48
+
49
+ - React Native's own native modules stay present for every lookup. The set is read from the installed `react-native`: every name its JavaScript passes to `TurboModuleRegistry.get` or `getEnforcing`, so it follows the project's React Native version.
50
+ - A React Native module's stub has only the members of its codegen spec, read from the same installed copy. Any other property is `undefined`, as on a device. A stub that answered every name with a function broke code that probes objects for optional properties: Node's `EventEmitter` read `captureRejections` from `NativeKeyboardObserver` when a test mocked `NativeEventEmitter` with it.
51
+ - `TurboModuleRegistry.getEnforcing(name)` still returns a stub for any name, where React Native would throw, so code that requires a native module keeps running without setup.
52
+ - A module registered with `mockNativeModule()` is present for all three lookups. Under the hot runtime, a registration a test file leaves behind is removed before the next file runs.
53
+
54
+ `nativeModules: 'permissive'` restores the earlier behaviour (every name present, every member a method) for a suite that depends on it.
55
+
56
+ If a test relied on an unregistered module being present through `NativeModules` or `TurboModuleRegistry.get`, or on a method a React Native module's spec does not declare, register it with `mockNativeModule(name, impl)` from `vitest-native/helpers`, in the test or a setup file.
57
+
58
+ - 3d20592: jest-compat: one module registry per test file, as in Jest. A `jest.mock(spec, factory)` (and `jest.doMock`, `jest.setMock`) now applies to `require()` from a test, to the modules `jest.requireActual` loads, and to React Native's own requires of a mocked internal such as `react-native/Libraries/AppState/AppState`, with the same mock instance the file's imports see. `jest.requireActual` unmocks only the module requested, `jest.requireMock` returns the registered mock instead of the real module, and `jest.resetModules()` gives `require()` fresh project modules and re-runs mock factories while React Native stays loaded. `jest.isolateModules` and `jest.isolateModulesAsync`, which previously threw, are implemented with Jest's semantics. A project module the test has imported is the instance `require()` returns. Registrations last for one test file, including in a reused worker. Factory-less mocks (automock and `__mocks__`) still apply to imports only.
59
+
60
+ TypeScript project files that Node loads are now compiled as babel-jest compiles them, with the options `@babel/core` loads for each file (any config format, `overrides`, and env- or caller-dependent configs). In a project with Expo, Babel sees jest-expo's Metro caller for the configured platform. The transform cache is keyed on those options, and a file the config ignores fails with "Babel ignores …", as under babel-jest. Relative requires from any file Node loads resolve as Metro resolves them: in its platform-extension order, with a trailing `/` naming the directory, and through a directory's `package.json` `react-native`, `browser` or `main` field. `jest.isolateModules` nesting throws Jest's exact error. Without `jestCompatSetup`, a `jest.mock` factory now fails with an error naming it (`JEST_COMPAT_SETUP_MISSING`).
61
+
62
+ - 12d413a: Asset imports and requires evaluate to what Metro gives an app: a registered asset id
63
+
64
+ `require('./logo.png')`, `import font from './Icon.ttf'` and every other asset now evaluate to a
65
+ number — the id React Native's asset registry assigned to the asset — instead of the file's name.
66
+ The module is the one Metro generates (`module.exports = require(assetRegistryPath).registerAsset({…})`),
67
+ with the descriptor Metro's `getAssetData` produces: `name`, `type`, `scales`, `hash`,
68
+ `httpServerLocation` and, for images, `width` and `height` in points. React Native code that consumes
69
+ assets therefore behaves as on device. `Image.resolveAssetSource(require('./splash.png'))` used to
70
+ return `null`, because a file name is not a registered id, so code such as
71
+ `Image.resolveAssetSource(source)!.uri` threw.
72
+
73
+ - **Native engine.** Assets register with React Native's own registry — the module Metro's
74
+ `assetRegistryPath` names (`react-native/asset-registry` on 0.87 and later,
75
+ `react-native/Libraries/Image/AssetRegistry` before) — on every load path: Vite-graph imports,
76
+ requires and imports through Node, and React Native's own image assets. `resolveAssetSource`,
77
+ `<Image source={require(…)}>` and `AssetRegistry.getAssetByID` resolve them.
78
+ - **Mock engine.** Assets register with the mock `AssetRegistry`, and the mock
79
+ `Image.resolveAssetSource` resolves a registered id as React Native does (the same URI the native
80
+ engine produces, on iOS and Android). The mock `AssetRegistry` now keeps its entries across
81
+ `resetAllMocks()`: React Native's registry has no reset, and an asset module's id must stay valid
82
+ for every test in the file.
83
+ - **Expo.** In a project `expo` resolves from, assets carry what Expo's bundler adds: `fileHashes`
84
+ and the Expo CLI dev server's asset location. expo-asset then resolves them through Expo, as in an
85
+ Expo app. The descriptor is checked against `getUniversalAssetData` from the installed Expo.
86
+ - **Scales and platforms.** `@2x`/`@3x` variants group as in Metro: `scales` lists them, the
87
+ dimensions are the smallest variant's divided by its scale, and resolution picks the variant for
88
+ the device's pixel ratio. A `.ios`/`.android` variant is preferred for the configured platform.
89
+ - **Dimensions** are read from PNG, JPEG, GIF, BMP and WebP headers, with no new dependency. SVG,
90
+ TIFF, PSD and KTX images, which Metro also measures, and files that are not valid images register
91
+ without `width` and `height`; Metro would fail the build on the latter.
92
+
93
+ ### Upgrading
94
+
95
+ Snapshots that contained an asset's file name now contain its registered id (a number), and under
96
+ the native engine a rendered `<Image source={require('./logo.png')} />` snapshots the resolved
97
+ source (`uri`, `width`, `height`, `scale`). Ids are assigned in registration order within a test
98
+ file, so they are stable for a given file. Update affected snapshots with `vitest -u`. Assertions
99
+ that compared an asset to its file name can compare `Image.resolveAssetSource(asset).uri` instead.
100
+
101
+ - 52e80aa: Turn on Vitest 5's persistent transform cache, with a complete cache key
102
+
103
+ On Vitest 5 the plugin turns on Vitest's persistent transform cache (`fsModuleCache`) unless the
104
+ config sets it, and contributes its own part of the cache key through Vitest's cache key
105
+ generator: the vitest-native version, the plugin's resolved options, and a digest of the project's
106
+ lockfile. Vitest's own key does not cover plugin versions or what a plugin reads from disk, so
107
+ without this an upgrade could serve modules transformed by the previous version. Warm runs of a
108
+ 104-file production Expo suite were about 11% faster. Set `test.fsModuleCache: false` to opt out.
109
+
23
110
  - 87d8dd0: Use the hot runtime for suites migrated from Jest under `hotRuntime: 'auto'`
24
111
 
25
112
  `'auto'`, the default, no longer keeps per-file isolation for suites set up with
@@ -58,6 +145,80 @@
58
145
 
59
146
  ### Patch Changes
60
147
 
148
+ - dc2543b: `migrate` writes Jest's mock-clearing behaviour correctly:
149
+
150
+ - Jest's `resetMocks` becomes `test.mockReset`, Vitest's name for it. It was written as `test.resetMocks`, which Vitest ignores.
151
+ - When the Jest config does not set `clearMocks`, the generated config sets `clearMocks: false`, Jest's default. Vitest 5 defaults it to `true`, which clears calls mocks received while modules loaded before each test.
152
+
153
+ - b10cce3: `migrate` no longer says the test script's flags were "mapped above" when the script has no flags.
154
+ - 3d20592: Native engine: a module the require hooks resolve themselves (Metro's platform-extension scan, the project's aliases, React Native's deep paths) is now keyed by its real path, as Node keys its own resolutions. A file reached through a symlink, or on Windows an alias target written with `/`, loaded a second time and gave two instances of one module.
155
+
156
+ - 041fd77: Fix native-engine gaps found running a production Expo app's existing suite
157
+
158
+ - React Native's setup now runs before the project's own setup files. A setup file that imported `react-native` or `@testing-library/react-native` failed on React Native's Flow source whenever the hot runtime was not in use.
159
+ - Importing `expo` (directly, or through packages such as `expo-location`) works: Expo's dev-server message socket is stubbed on every load path, and packages a compiled CommonJS module requires internally stay shadowed by their presets instead of loading the real native packages.
160
+ - `require('#/…')` and `jest.requireActual('#/…')` resolve the project's `resolve.alias` entries and, when Vite resolves them for imports, its tsconfig `paths`.
161
+ - App source required through Node compiles `export * as ns from '…'` when the project's toolchain provides the Babel plugin, as Expo's does.
162
+ - `react-native-nitro-modules` imports under the native engine: its native install step provides a proxy with no hybrid objects, and creating one throws `NITRO_HYBRID_OBJECT_UNAVAILABLE`.
163
+ - `react-native-mmkv` 3 and later run their own built-in test mode, with their real API and change listeners; the `mmkv` preset now applies to mmkv 2 only.
164
+
165
+ - dbb9c74: Match Jest for four behaviours migrated suites depend on in jest-compat
166
+
167
+ - **Getters in a `jest.mock` factory are read lazily.** The CommonJS interop copied the factory's return with an object spread, which ran every getter while the factory was evaluated. A factory such as `() => ({ get IS_WEB() { return mockIsWeb } })` runs during the hoisted imports, before the test file's `let mockIsWeb` is initialised, so it threw `Cannot access 'mockIsWeb' before initialization`, and later changes to the variable were never seen. Accessors are now read from the module on each access, as an importer reads them in Jest.
168
+ - **An export a `jest.mock` factory leaves out is `undefined`.** Jest reads named imports as properties of `module.exports`; Vitest throws `No "x" export is defined on the "m" mock`. Factories passed through `jestMockTransform` now follow Jest. A plain `vi.mock` factory keeps Vitest's strict check.
169
+ - **`new` on a `jest.fn` with an arrow implementation returns the implementation's result.** Jest applies a mock's implementation whether or not it is called with `new`, so `jest.fn().mockImplementation(() => ({ fetch }))` works as a class mock; Vitest constructs the implementation and throws `… is not a constructor`. Mocks created by `jest.fn` and `jest.spyOn` now follow Jest, including `mockReturnValue` and `mockResolvedValue` under `new`, and a `jest.spyOn` spy with no implementation set calls an arrow-function original under `new` (also after `mockReset()`). A mock that already existed, which `jest.spyOn` returns as it is, is left unchanged, and `vi.fn` is unchanged.
170
+ - **`process.env.JEST_WORKER_ID` is set.** The compat setup sets it from Vitest's 1-based `VITEST_POOL_ID` when it is not already set, so code that detects a Jest run (for example `__DEV__ && !process.env.JEST_WORKER_ID`) takes its test branch.
171
+
172
+ - 52e80aa: Run the native engine's ESM loader hooks in-thread where Node supports it
173
+
174
+ The native engine's ESM loader hooks now run in-thread through `module.registerHooks()` where Node
175
+ provides it (22.15+, 23.5+), instead of on a separate loader thread through `module.register()`.
176
+ Every resolve and load of an externalized module previously waited on a synchronous cross-thread
177
+ request; profiled on a 104-file production Expo suite, that wait was 18% of worker CPU time. Older
178
+ Node versions keep the threaded hooks, and `VITEST_NATIVE_LOADER_THREAD=1` forces them.
179
+
180
+ - 52e80aa: Reuse externalized resolutions across test files under the hot runtime
181
+
182
+ Under the hot runtime, externalized imports are no longer re-resolved from scratch for every test
183
+ file. The per-file generation stamp on parent URLs defeated Node's resolve cache; the loader now
184
+ keeps successful resolutions per unstamped parent, so Node stops re-reading package.json scopes to
185
+ decide module formats on every file.
186
+
187
+ - 6100732: Make `migrate` reproduce Jest's effective test set and settings, and report Babel plugins
188
+
189
+ `migrate` translated only an explicit `testMatch`, so a project that took its patterns from a
190
+ preset or from Jest's defaults was migrated to Vitest's default include instead. On a jest-expo
191
+ app this collected about twice as many files as Jest ran and missed some of Jest's own tests.
192
+ The suggested config now uses Jest's effective patterns: the config's `testMatch`, otherwise the
193
+ installed preset's (read from it the way Jest loads it), otherwise Jest's defaults for the
194
+ installed major. `testPathIgnorePatterns` and `modulePathIgnorePatterns` become `test.exclude`
195
+ globs where the regex has an exact glob form (and are reported where it does not), and
196
+ extensions missing from `moduleFileExtensions` are excluded, since Jest never sees those files.
197
+
198
+ Other changes:
199
+
200
+ - Flags that the `test` script passes to Jest are applied over the config as Jest applies them:
201
+ `--testTimeout`, `--maxWorkers`, `--runInBand` and `--config`. `--bail` is reported rather than
202
+ mapped because Vitest's `bail` counts failed tests, not suites.
203
+ - Anchored `moduleNameMapper` keys (`^name$`) become exact `resolve.alias` entries. A mapper whose
204
+ target is a file of the same package, for a specifier that package's `exports` already serves, is
205
+ dropped; a redirect to another package is kept.
206
+ - `transformIgnorePatterns` allowlists with nested and optional groups are now parsed. A package
207
+ goes into `transform` only when it ships a `.js` file under its entry point that is not standard
208
+ JavaScript (JSX or Flow) and the engine does not already compile it; the report names that file.
209
+ - Packages that run their own test mode under Vitest (react-native-mmkv 3 and later) are not
210
+ credited to a preset by either command, and the engine-detection baseline both commands use
211
+ is computed with the presets the plugin would actually enable.
212
+ - The project's Babel config is read, and its plugins are sorted into required (macro plugins),
213
+ mapped (`module-resolver` aliases become `resolve.alias`), not needed (worklets/reanimated
214
+ plugins, React Compiler) and unknown. `migrate` adds required plugins through
215
+ `@rolldown/plugin-babel` when Vite 8 and that package are installed, and otherwise prints the
216
+ snippet to add. `doctor` warns about the same plugins.
217
+ - The CLI no longer claims more than the presets do. The report and `doctor` name the modules the
218
+ expo preset shadows instead of saying Expo modules are covered. A manual `__mocks__` file is
219
+ called redundant only when an active preset shadows that module. Asset mappers are compared with
220
+ the extensions the plugin stubs by default.
221
+
61
222
  - 2233866: Stop a circular-dependency warning on an Expo project's first run
62
223
 
63
224
  Babel loads its presets and plugins with `require` during a transform, and those requires went
package/README.md CHANGED
@@ -31,7 +31,7 @@ Run your React Native tests under Vitest, against **real React Native** — the
31
31
  - [Animated Matchers (Reanimated)](#animated-matchers-reanimated)
32
32
  - [Snapshot Serializer](#snapshot-serializer)
33
33
  - [Platform Extensions](#platform-extensions)
34
- - [Asset Stubs](#asset-stubs)
34
+ - [Assets](#assets)
35
35
  - [Diagnostics](#diagnostics)
36
36
  - [Support Policy](#support-policy)
37
37
  - [Requirements](#requirements)
@@ -536,24 +536,25 @@ export default defineConfig({
536
536
 
537
537
  ### Available Presets
538
538
 
539
- | Import | Function |
540
- | ------------------------------ | ----------------------------------------- |
541
- | `presets.reanimated()` | react-native-reanimated |
542
- | `presets.gestureHandler()` | react-native-gesture-handler |
543
- | `presets.safeAreaContext()` | react-native-safe-area-context |
544
- | `presets.navigation()` | @react-navigation/native |
545
- | `presets.asyncStorage()` | @react-native-async-storage/async-storage |
546
- | `presets.screens()` | react-native-screens |
547
- | `presets.expo()` | Expo modules |
548
- | `presets.deviceInfo()` | react-native-device-info |
549
- | `presets.mmkv()` | react-native-mmkv |
550
- | `presets.netInfo()` | @react-native-community/netinfo |
551
- | `presets.svg()` | react-native-svg |
552
- | `presets.webview()` | react-native-webview |
553
- | `presets.vectorIcons()` | react-native-vector-icons |
554
- | `presets.flashList()` | @shopify/flash-list |
555
- | `presets.bottomSheet()` | @gorhom/bottom-sheet |
556
- | `presets.keyboardController()` | react-native-keyboard-controller |
539
+ | Preset | Library | What's mocked |
540
+ |--------|---------|---------------|
541
+ | `presets.reanimated()` | `react-native-reanimated` | `useSharedValue`, `useAnimatedStyle`, `withTiming`, `withSpring`, `withDelay`, `withSequence`, `withRepeat`, layout animations (`FadeIn`, `FadeOut`, `SlideInRight`), `Easing`, `interpolate`, `createAnimatedComponent` |
542
+ | `presets.worklets()` | `react-native-worklets` | `runOnJS`, `runOnUI`, `scheduleOnRN`, `scheduleOnUI`, `createWorkletRuntime`, … run synchronously on the JS thread |
543
+ | `presets.gestureHandler()` | `react-native-gesture-handler` | `GestureHandlerRootView`, gesture handlers (Pan, Tap, LongPress, Pinch, Rotation, Fling), `Gesture` API (v2), `GestureDetector`, `Swipeable`, touchable wrappers, state constants |
544
+ | `presets.safeAreaContext()` | `react-native-safe-area-context` | `SafeAreaProvider`, `SafeAreaView`, `useSafeAreaInsets`, `useSafeAreaFrame`, `initialWindowMetrics`, `withSafeAreaInsets` |
545
+ | `presets.navigation()` | `@react-navigation/native`, `@react-navigation/native-stack`, `@react-navigation/bottom-tabs`, `@react-navigation/drawer`, `@react-navigation/elements` | `NavigationContainer`, `useNavigation`, `useRoute`, `useFocusEffect`, `useIsFocused`, `CommonActions`, `StackActions`, `TabActions`, `DrawerActions`, navigators |
546
+ | `presets.screens()` | `react-native-screens` | `enableScreens`, `Screen`, `ScreenContainer`, `ScreenStack` |
547
+ | `presets.asyncStorage()` | `@react-native-async-storage/async-storage` | in-memory store (`getItem`/`setItem`/`multiGet`/`mergeItem`/…) |
548
+ | `presets.expo()` | `expo-constants`, `expo-font`, `expo-asset`, `expo-splash-screen`, `expo-linking`, `expo-status-bar` | constants, fonts, assets, splash screen, linking, status bar |
549
+ | `presets.deviceInfo()` | `react-native-device-info` | string/bool/number getters with sync + async variants |
550
+ | `presets.mmkv()` | `react-native-mmkv` | in-memory `MMKV` + `useMMKV*` hooks, for mmkv 2 only: the library tests itself from v3 (its own in-memory backend under Vitest), so the preset steps aside |
551
+ | `presets.netInfo()` | `@react-native-community/netinfo` | connected-wifi state, `fetch`/`refresh`/`addEventListener`/`useNetInfo`, state-type enums |
552
+ | `presets.svg()` | `react-native-svg` | `Svg`, `Path`, `Circle`, `Rect`, `G`, … as host components |
553
+ | `presets.webview()` | `react-native-webview` | `WebView` (default + named) host component |
554
+ | `presets.vectorIcons()` | `@react-native-vector-icons/common` | `createIconSet` and the dynamic font loader shared by the v10+ scoped icon sets (not the legacy unscoped `react-native-vector-icons`) |
555
+ | `presets.flashList()` | `@shopify/flash-list` | `FlashList` rendering its data through `renderItem`, the ref surface, v2 recycler hooks |
556
+ | `presets.bottomSheet()` | `@gorhom/bottom-sheet` | `BottomSheet`, `BottomSheetModal` + provider, sheet views, scroll/list variants, `BottomSheetTextInput`, backdrop, footer |
557
+ | `presets.keyboardController()` | `react-native-keyboard-controller` | `KeyboardProvider`, `KeyboardAvoidingView`, `KeyboardAwareScrollView`, `KeyboardStickyView`, `KeyboardToolbar`; the imperative `KeyboardController` is inert |
557
558
 
558
559
  Presets apply under **both** engines: the mock engine and `engine: 'native'` (where they shadow each
559
560
  library's native runtime — worklets, native modules — exactly as Jest does, while the surrounding tree
@@ -913,15 +914,21 @@ falling back to the default (often web) file.
913
914
 
914
915
  ---
915
916
 
916
- ## Asset Stubs
917
+ ## Assets
917
918
 
918
- Image, font, video, and audio imports are automatically stubbed. The import resolves to the filename string, so code that passes asset paths through continues to work without errors.
919
+ Image, font, video, and audio imports evaluate to what Metro gives an app: a number, the id under which the asset is registered with React Native's asset registry. The registered descriptor is the one Metro builds — `name`, `type`, `scales`, `hash`, and for images `width` and `height` in points — so React Native's own asset handling works as on device:
919
920
 
920
- ```ts
921
- import logo from "./logo.png";
922
- // logo === 'logo.png'
921
+ ```tsx
922
+ const logo = require("./logo.png"); // a number
923
+ Image.resolveAssetSource(logo); // { uri, width, height, scale, __packager_asset: true }
924
+ <Image source={logo} />; // renders the resolved asset
923
925
  ```
924
926
 
927
+ - `@2x`/`@3x` variants are grouped as Metro groups them, and a `.ios`/`.android` variant is preferred for the configured platform.
928
+ - Dimensions are read from PNG, JPEG, GIF, BMP and WebP headers; other formats, and files that are not valid images, register without them.
929
+ - The native engine registers with React Native's real registry; the mock engine registers with the mock `AssetRegistry`, which its `Image.resolveAssetSource` reads.
930
+ - A snapshot shows the id (a number) where it used to show the file name. Ids follow registration order within a test file, so they are stable for that file.
931
+
925
932
  Built-in extensions include common formats like `.png`, `.jpg`, `.gif`, `.svg`, `.mp4`, `.ttf`, `.otf`, and more. To add additional extensions:
926
933
 
927
934
  ```ts