Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/react-native-mmkv/src/isTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@ export function isTest(): boolean {
// In a WebBrowser/Electron the `process` variable does not exist
return false
}
return (
process.env.JEST_WORKER_ID != null || process.env.VITEST_WORKER_ID != null
)
// Read through a variable rather than as `process.env.JEST_WORKER_ID`.
//
// Metro parallelises transforms with jest-worker, which sets JEST_WORKER_ID
// in each worker process, and bundlers such as babel-preset-expo inline
// static `process.env.X` member expressions at transform time -- including
// inside node_modules. The transform worker's id therefore gets baked into
// the app bundle as a literal, isTest() returns true in a normal debug or
// release build, and createMMKV() silently returns the in-memory mock.
// Storage then appears to work in-session but is never written to disk.
//
// Only static member expressions are inlined, so indexing a variable sees
// the real runtime environment. Jest and Vitest still set these at runtime
// in their workers, so genuine test runs are unaffected.
const env: Record<string, string | undefined> = global.process.env || {}
return Boolean(env['JEST_WORKER_ID'] || env['VITEST_WORKER_ID'])
}