stub async-storage for vitest to fix unhandled window error
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user