Význam přírodních kamenů: průvodce krystaly a jejich symbolikou
@react-native-async-storage/async-storage can be challenging. This is because AsyncStorage is a native module, and running tests in a Node.js environment (like Jest) doesn't have access to native device APIs.
To overcome this, we need to mock AsyncStorage. Mocking allows us to replace the actual native implementation with a JavaScript-based fake implementation that behaves similarly, enabling us to test our components in isolation.
In this guide, we'll walk through how to mock @react-native-async-storage/async-storage in your React Native project using Jest.
@react-native-async-storage/async-storage
jest-setup.js (or jest.setup.js) in the root of your project.
2. If you already have a setup file (often specified in your package.json or jest.config.js under setupFilesAfterEnv or setupFiles), you can use that one.
@react-native-async-storage/async-storage library provides a built-in mock that you can easily use. Add the following line to your Jest setup file:
import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock';
jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage);
This code imports the mock implementation provided by the library and tells Jest to use it whenever a component or test file imports @react-native-async-storage/async-storage.
package.json or jest.config.js file and add the path to your setup file under the setupFiles or setupFilesAfterEnv key.
Example (package.json):
{
"jest": {
"preset": "react-native",
"setupFiles": [
"./jest-setup.js"
]
}
}
Example (jest.config.js):
module.exports = {
preset: 'react-native',
setupFiles: ['./jest-setup.js'],
};
import AsyncStorage from '@react-native-async-storage/async-storage';
export const saveToken = async (token) => {
try {
await AsyncStorage.setItem('userToken', token);
} catch (e) {
console.error('Failed to save token', e);
}
};
export const getToken = async () => {
try {
return await AsyncStorage.getItem('userToken');
} catch (e) {
console.error('Failed to fetch token', e);
return null;
}
};
auth.test.js (Test File):
import AsyncStorage from '@react-native-async-storage/async-storage';
import { saveToken, getToken } from './auth';
describe('Auth Helper', () => {
beforeEach(async () => {
// Clear AsyncStorage before each test to ensure a clean state
await AsyncStorage.clear();
});
it('should save and retrieve the token', async () => {
const token = 'test-token-123';
await saveToken(token);
const retrievedToken = await getToken();
expect(retrievedToken).toBe(token);
});
it('should return null if no token is saved', async () => {
const retrievedToken = await getToken();
expect(retrievedToken).toBeNull();
});
});
@react-native-async-storage/async-storage is essential for writing reliable unit tests in React Native. By using the built-in mock provided by the library and configuring Jest to use it, you can easily test components and functions that rely on local storage without worrying about native device APIs.