gpt4 book ai didi

testing - React Native Test with Jest 无法模拟地理位置

转载 作者:行者123 更新时间:2023-11-28 20:37:15 28 4
gpt4 key购买 nike

我试图在网上找到解决方案,我找到了这个 Stackoverflow answer ,我按照说明操作但被阻止了:

TypeError: Cannot set property 'geolocation' of undefined

所以当我删除解决方案时,我的错误是:

ReferenceError: navigator is not defined

我的 package.json 看起来像这样:

  "dependencies": {
"eslint-plugin-react-native": "^3.2.0",
"prop-types": "^15.6.0",
"react": "16.0.0",
"react-native": "0.51.0",
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-eslint": "^8.1.2",
"babel-jest": "^22.4.1",
"babel-preset-react-native": "4.0.0",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"eslint": "^4.14.0",
"eslint-plugin-react": "^7.5.1",
"jest": "^22.4.2",
"jest-enzyme": "^6.0.0",
"react-dom": "^16.2.0",
"react-scripts": "^1.1.1",
"react-test-renderer": "16.0.0-beta.5"
},
"jest": {
"preset": "react-native",
"moduleFileExtensions": [
"js",
"jsx"
],
"moduleDirectories": [
"node_modules"
],
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-navigation)"
],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/assetsTransformer.js",
"\\.(css|less)$": "<rootDir>/assetsTransformer.js"
},
"verbose": true
},
"plugins": [
"react",
"react-native"
],
"ecmaFeatures": {
"jsx": true
},
"env": {
"react-native/react-native": true
}

我的测试是:

test('Map Component', () => {
// create the snapshot
const tree = renderer.create(<Map/>).toJSON();
// test the snapshot
expect(tree).toMatchSnapshot();
});

错误在这一行,在navigator:

navigator.geolocation.getCurrentPosition((position) => {

目前我没有使用导航器测试我的组件。我希望你们中的一些人有解决方案。

最佳答案

在你的 Jest setup.js 中添加:

global.navigator = {
geolocation: {
clearWatch: jest.fn(),
getCurrentPosition: jest.fn((success, failure, options) => {
success({
coords: {
longitude: 60,
latitude: 60,
},
});
}),
stopObserving: jest.fn(),
watchPosition: jest.fn(),
},
};

setup.js 文件是您在 package.json 文件中指定的文件。即:

"jest": {
"setupFiles": [
"<rootDir>/__tests__/setup.js"
],
...

关于testing - React Native Test with Jest 无法模拟地理位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49385500/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com