vitest-native 0.16
0.16 makes a cold native-engine run wait for React Native's precompiled modules again, lets the hot runtime size its workers from the machine, and serves one copy of React to installed packages under the native engine. jest-compat's fake timers now fake queueMicrotask, as Jest does, and the navigation, screens, bottom-sheet and keyboard-controller presets match their libraries more closely.
This page covers what changes for a project on 0.15. Every entry, with the detail behind it, is in the changelog. The supported versions are the same as in 0.15.
Before you upgrade
Seven changes can change a run, a test or a snapshot. Each has a setting that keeps the 0.15 behaviour, or a one-time step:
- A cold cache builds React Native's precompiled modules before the first test file, as
registryBuildnow defaults to'wait'. Under'background', the first files compiled React Native inside their tests and could reachtestTimeout. Warm runs, CI runs and the hot runtime are unchanged. To keep the old behaviour, setreactNative({ registryBuild: 'background' }). - The hot runtime picks its worker count from the machine when
maxWorkersis not set: one worker per performance core on Apple silicon, at most 6, and four elsewhere, as before. The memory plan still bounds the count, and the startup line says how it was chosen. To keep four workers, settest.maxWorkers: 4. See Workers and memory. - Under the native engine, installed packages share the React your tests render with. A package that nests its own copy of React, such as a
use-sync-external-storeshim, now gets the copyreact-nativeresolves, as Vite already gave it. If the run now fails withPRESET_REACT_MISMATCH, install one copy ofreact. - jest-compat's
jest.useFakeTimers()fakesqueueMicrotask, as Jest does, so a queued callback runs when the timers run.process.nextTickstays real, and a project whosetest.fakeTimerssetstoFakeortoNotFakekeeps its own list. To keep it real, calljest.useFakeTimers({ doNotFake: ['queueMicrotask'] }). - The navigation preset's
useLinkProps()returnsrole: "link"instead ofaccessibilityRole, so snapshots of links change. Check the diff, then runvitest -uto re-record them. - The navigation preset's
usePreventRemoveContext()returns an empty context, notnull, and header heights are the library's default (44 on an iOS phone, 56 on Android) instead of 64. Update tests that expectednullor 64 from these. - The screens preset's
useTransitionProgress()returnsAnimated.Values, andprogressstarts at 0, not 1, as in react-native-screens. Snapshots that printed it change, and a test that read these values as numbers needs updating. Check the diff, then runvitest -uto re-record the snapshots.
Presets closer to their libraries
- Navigation matches React Navigation 7:
usePreventRemoveContext()returns an empty context, notnull;useNavigationBuilder().navigationhasemitand stays the same object on every render; and native-stack'suseAnimatedHeaderHeight(),useHeaderHeight()andgetDefaultHeaderHeight()give the library's default height instead of 64. - Screens:
Tabsis{ Host, Screen }, andfeatureFlagsandcompatibilityFlagscarry the package's defaults. - Bottom sheet:
useBottomSheetScrollableCreator,useScrollHandler, the gesture and scroll handler hooks, and the library's constants exist under both engines, with the package's default props and initial states. - Keyboard controller:
useAnimatedKeyboard,KeyboardState,KeyboardContextand the missing constants exist under the native engine, with shared values in Reanimated 4's shape.
Closer to Jest
- A root
__mocks__mock reaches every installed copy of a package, andjest.mockandjest.unmockapply per copy, as under Jest. - A partial
jest.mockthat lacks an export a package uses fails by name, asJEST_MOCK_MISSING_EXPORT, naming the mock and the export. jest.mockunderjestMockTransform()works with Vitest's defaultglobals: false.jest.useFakeTimers()works in a project that setstest.fakeTimers.toNotFake.- A project that uses
vitest-native/jest-compatwithoutreactNative(), as a react-native-web suite does for the Jest API alone, runs its tests again. - Under the native engine, an Expo module's native-module mock that calls
jest.fnorjest.requireActualloads without the jest-compat layer. - Under jest-compat,
ref instanceof Viewworks, and a method added toView.prototypereaches View refs, as under React Native's Jest preset.
Native engine
- An installed React Native package imported without being listed in dependencies is one module, not two, and so is a workspace library that a React Native package imports unconditionally (a top-level
import,export … fromorrequire()). Vite and Node no longer each load their own copy of it. - A dependency of an installed React Native package that your project does not declare fails by name when Node cannot run it, as
UNDECLARED_DEPENDENCY_UNCOMPILED. Add it totransform, or declare the parent package in your dependencies. A package's optional peers no longer have their dependencies compiled along with its own, so such a dependency can now fail this way too. - The
SOURCE_EVALUATED_TWICEsummary prints one line per installed package, anddiagnosticslists every file. - Under the experimental
experimental.nodeMocksflag, installed packages' imports of a mocked ES-module package get the mock. - vitest-native no longer turns on Node's compile cache, so a cancelled run no longer stalls at exit. Delete
node_modules/.cache/vitest-native/v8, which is no longer used.
How 0.16.0 was built
The npm tarball is reproducible from the repository. Packing the v0.16.0 tag from its lockfile gives a tarball whose sha512 equals the one on npm. The release workflow checks this on every release: its publish job packs the tag again and refuses to publish unless the tarball is byte-identical to the one the test jobs ran against. The package is also published with npm provenance, which links it to the workflow run that built it.
To check it yourself:
git clone https://github.com/danfry1/vitest-native.git
cd vitest-native
git checkout v0.16.0
bun install --frozen-lockfile
bun run build
cd packages/vitest-native
npm pack --ignore-scripts
echo "sha512-$(openssl dgst -sha512 -binary vitest-native-0.16.0.tgz | base64)"
npm view vitest-native@0.16.0 dist.integrityThe last two lines print the same value.
Thank you
Thank you to @swey and @YevheniiKotyrlo for the issues and pull requests in this release.