Lau*_*uez 7 jestjs react-native
我是测试 React Native 应用程序的新手。我从 Jest 开始,我运行了一个名为 App-test.js 的测试,它通过了,但我收到了参考错误:您正在尝试import在 Jest 环境被拆除后创建一个文件。
我试过了:
jest.useFakeTimers();
Run Code Online (Sandbox Code Playgroud)
和
jest.useFakeTimers();
Date.now = jest.fn(() => 1503187200000);
Run Code Online (Sandbox Code Playgroud)
jestSetupFile.js
import mockAsyncStorage from '@react-native-community/async-
storage/jest/async-storage-mock';
import { NativeModules } from 'react-native';
jest.mock('@react-native-community/async-storage', () =>
mockAsyncStorage);
jest.useFakeTimers();
Date.now = jest.fn(() => 1503187200000);
Object.assign(NativeModules, {
RNGestureHandlerModule: {
attachGestureHandler: jest.fn(),
createGestureHandler: jest.fn(),
dropGestureHandler: jest.fn(),
updateGestureHandler: jest.fn(),
State: {},
Directions: {},
},
PlatformConstants: {
forceTouchAvailable: false,
},
});
Run Code Online (Sandbox Code Playgroud)
包.json
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-clone-referenced-element|@react-native-community|react-navigation-header-buttons|react-navigation-stack|@react-navigation)"
],
"setupFiles": [
"./jestSetupFile.js"
]
}
Run Code Online (Sandbox Code Playgroud)
尝试在您的文件中添加setup.js用于测试此行的所有设置:
// this should be at the top of the file
import { cleanup } from '@testing-library/react-native';`
// and also this line (probably) at the end of the file or just bellow all imports:
afterEach(cleanup);
Run Code Online (Sandbox Code Playgroud)
这将确保运行测试后组件将被卸载
| 归档时间: |
|
| 查看次数: |
4045 次 |
| 最近记录: |