React native TypeError:无法读取属性';平台本地存储';未定义的
我正在react native上运行jest测试,并出现以下错误 测试套件无法运行React native TypeError:无法读取属性';平台本地存储';未定义的,react-native,jestjs,enzyme,enzyme-to-snapshot,React Native,Jestjs,Enzyme,Enzyme To Snapshot,我正在react native上运行jest测试,并出现以下错误 测试套件无法运行 TypeError: Cannot read property 'PlatformLocalStorage' of undefined 2 | import thunk from 'redux-thunk'; 3 | import { persistStore, persistReducer } from 'redux-persist'; > 4 | import AsyncStorage fro
TypeError: Cannot read property 'PlatformLocalStorage' of undefined
2 | import thunk from 'redux-thunk';
3 | import { persistStore, persistReducer } from 'redux-persist';
> 4 | import AsyncStorage from '@react-native-community/async-storage';
| ^
5 |
“依赖项”:{
“反应”:“16.8.3”,
“反应本机”:“0.59.9”,
},
“依赖性”:{
“笑话”:“^24.1.0”,
“酶”:“^3.9.0”,
“酶-适配器-反应-16”:“^1.10.0”,
},
“笑话”:{
“SetupFileAfterEnv”:[
“/\uuuu测试\uuuu/setup/test setup.js”
],
“设置文件”:[
“/\uuuu测试\uuuu/setup/test setup.js”
],
“transformIgnorePatterns”:[
“/node\u模块(?![\\/]rn fetch blob[\\/]\\\/]react native[\\/]\\/]react native config[\\/]\\\/]react native navigation[\\\/]react native status bar height[\\/]react native animatable[\\\/]react native animatable[\\\/]react native native重新启动[\\\/]react native线性渐变[\\/]react native[\\/]react native[\/]react native[\\\/]react native[\\/]react native[\/]反应本机平滑pincode输入[\\/]\\\/]反应本机滑动手势[\\/]\\\/]反应本机根toast[\\/]\\/]反应本机根同级[\\/]\\\/]反应本机进程[\\/]\\\/]静态容器[\\\/]\\\\/]反应本机元素[\\\/]\\/]反应本机分级[\\\\/]反应本机进程[\\\\/]反应本机对话框[\/\\/]react native webview[\/][\\/][\\/]react native复选框[\\/][\\\/]react native share[\\/][\\/]react native触觉反馈[\\/][\\/]react native responsive screen[\\/]react native键盘感知滚动视图[\\\/]他[\\\/]react-native-iphone x-helper[\/]react-native-iphone x-helper[\/]检查项目类型[\\\/][\\\\\/]进程[\\/]react-native主题化[\\/]\[\\/]@react-native社区/异步存储[\\/])/”
],
“testPathIgnorePatterns”:[
“/(构建|文档|节点|模块)/”,
“/\测试\设置”
],
“预设”:“本机反应”
},
“rnpm”:{
“资产”:[
“字体”
]
}
}
当我在react原生应用程序中运行jest测试时,有人能帮我解释为什么会出现这种错误吗。我已经更新了几个开发人员依赖项,但运气不佳。模拟异步存储库。如果像我一样,您已经从
react native
变体移植到react native社区
,您可能需要更新您的mock(如果您已经有了)
解决方案是模拟包
\uuuumocks\uuuu/@react本机异步存储目录
async storage.js
文件从“@react native async storage/async storage/jest/async storage mock”导出默认值
完整的说明在这里
"dependencies":{
"react": "16.8.3",
"react-native": "0.59.9",
},
"devDependencies": {
"jest": "^24.1.0",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.10.0",
},
"jest": {
"setupFilesAfterEnv": [
"<rootDir>/__tests__/setup/test-setup.js"
],
"setupFiles": [
"<rootDir>/__tests__/setup/test-setup.js"
],
"transformIgnorePatterns": [
"/node_modules(?![\\/]rn-fetch-blob[\\/]|[\\/]react-native[\\/]|[\\/]react-native-config[\\/]|[\\/]react-native-navigation[\\/]|[\\/]react-native-status-bar-height[\\/]|[\\/]react-native-animatable[\\/]|[\\/]react-native-restart[\\/]|[\\/]react-native-linear-gradient[\\/]|[\\/]react-native-i18n[\\/]|[\\/]react-native-modal[\\/]|[\\/]react-native-smooth-pincode-input[\\/]|[\\/]react-native-swipe-gestures[\\/]|[\\/]react-native-root-toast[\\/]|[\\/]react-native-root-siblings[\\/]|[\\/]static-container[\\/]|[\\/]react-native-elements[\\/]|[\\/]react-native-ratings[\\/]|[\\/]react-native-progress[\\/]|[\\/]react-native-dialog[\\/]|[\\/]react-native-pdf[\\/]|[\\/]react-native-webview[\\/]|[\\/]react-native-check-box[\\/]|[\\/]react-native-share[\\/]|[\\/]react-native-haptic-feedback[\\/]|[\\/]react-native-responsive-screen[\\/]|[\\/]react-native-keyboard-aware-scroll-view[\\/]|[\\/]he[\\/]|[\\/]react-native-iphone-x-helper[\\/]|[\\/]check-prop-types[\\/]|[\\/]react-native-image-progress[\\/]|[\\/]react-native-theming[\\/]|[\\/]@react-native-community/async-storage[\\/])/"
],
"testPathIgnorePatterns": [
"<rootDir>/(build|docs|node_modules)/",
"<rootDir>/__tests__/setup"
],
"preset": "react-native"
},
"rnpm": {
"assets": [
"fonts"
]
}
}
jest.mock("@react-native-community/async-storage", () => {
// a map/dict/kvs of types to return - the leaves of the def
// are jest functions
const apiMock = {
getItem: jest.fn(() => { // mock's AsyncStorage.getItem()
return JSON.stringify("some mock data");
}),
};
return apiMock;
});