mardi 20 mars 2018

React Native Test with Jest impossible to mock Geolocation

I tried to find a solution on the web and I found this one, I followed the instructions but I was blocked :

TypeError: Cannot set property 'geolocation' of undefined

So when I remove the solution my error is :

ReferenceError: navigator is not defined

My package JSON look like this:

      "dependencies": {
    "eslint-plugin-react-native": "^3.2.0",
    "prop-types": "^15.6.0",
    "react": "16.0.0",
    "react-native": "0.51.0",
    "react-native-autogrow-textinput": "^5.0.0",
    "react-native-blur": "^3.2.2",
    "react-native-camera": "^0.12.0",
    "react-native-i18n": "^2.0.9",
    "react-native-image-picker": "^0.26.7",
    "react-native-linear-gradient": "^2.4.0",
    "react-native-maps": "^0.19.0",
    "react-native-modal": "^4.1.1",
    "react-native-navigation": "^1.1.391",
    "react-native-vector-icons": "^4.4.2"
  },
  "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
  }

And my test is juste a :

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

And that crash on this line, especially on the navigator :

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

For the moment I just don't test my component with the navigator. I hope some of you have a solution please.

Aucun commentaire:

Enregistrer un commentaire