stub async-storage for vitest to fix unhandled window error

This commit is contained in:
Samuel Newman
2026-06-29 18:39:07 +03:00
parent 56b29e21ee
commit 15b4bdc810
2 changed files with 36 additions and 0 deletions
+9
View File
@@ -120,6 +120,15 @@ export default defineConfig({
*/
{find: /^expo$/, replacement: r('./vitest/expo-stub.ts')},
/*
* async-storage references `window` at module load, undefined under
* node. Stubbed with an in-memory implementation.
*/
{
find: /^@react-native-async-storage\/async-storage$/,
replacement: r('./vitest/async-storage-stub.ts'),
},
/*
* Former Jest moduleNameMapper entries. Vite's ESM resolution may make
* some of these unnecessary, but they're harmless and proven, so keep
+27
View File
@@ -0,0 +1,27 @@
/*
* Stub for @react-native-async-storage/async-storage. The real package
* references `window` at module load, which is undefined in Vitest's node
* environment. The package ships an official jest mock, but it relies on the
* `jest` global, so we provide a small in-memory implementation instead.
*
* Aliased in place of @react-native-async-storage/async-storage via
* resolve.alias in vitest.config.ts. Test-reachable code only uses
* getItem/setItem/removeItem.
*/
const store = new Map<string, string>()
const AsyncStorage = {
getItem(key: string): Promise<string | null> {
return Promise.resolve(store.has(key) ? store.get(key)! : null)
},
setItem(key: string, value: string): Promise<void> {
store.set(key, value)
return Promise.resolve()
},
removeItem(key: string): Promise<void> {
store.delete(key)
return Promise.resolve()
},
}
export default AsyncStorage