Jest integration¶
AsyncStorage requires a native module that is not available in a Jest environment. The package ships a built-in
in-memory mock at @react-native-async-storage/async-storage/jest that replaces the native implementation during
tests.
Setup¶
Transform configuration¶
The package ships as ESM source, so Jest must be configured to transform it. Add it to the transformIgnorePatterns in
your Jest config:
Automatic mock file¶
Create a manual mock file that Jest will pick up automatically for every test:
With content:
Jest resolves files under __mocks__/ automatically when the module is imported, so no additional configuration is
needed.
Inline mock¶
To mock the module for a specific test file, call jest.mock at the top of the file:
jest.mock("@react-native-async-storage/async-storage", () =>
require("@react-native-async-storage/async-storage/jest")
);
What the mock provides¶
The mock is a full in-memory implementation of the AsyncStorage interface. Additionally, it exports
clearAllMockStorages to clear all in-memory storages.
Example usage: