How to use Jest with React Native

后端 未结 3 1654
情书的邮戳
情书的邮戳 2020-12-14 08:17

The testing section of the docs for React Native suggest that Jest is the official way to do unit tests. However, they don\'t explain how to get it setup. Running Jest witho

3条回答
  •  悲&欢浪女
    2020-12-14 08:57

    For newer versions of jest, setting it up for react-native is very easy.

    Install jest using

    npm install --save-dev jest-cli babel-jest
    

    In package.json, add this

    "scripts": {
      "start": "babel-node ./server/server.js",
      "import-data": "babel-node ./scripts/import-data-from-parse.js",
      "update-schema": "babel-node ./server/schema/updateSchema.js",
      "test": "jest",
      "lint": "eslint ."
    },
    "jest": {
      "haste": {
        "defaultPlatform": "ios",
        "platforms": [
          "ios",
          "android"
        ],
        "providesModuleNodeModules": [
          "react-native"
        ]
      }
    },
    

    You might only need to add lines related to jest

    Now you can use

    npm test
    

    to run jest.

    Refer to f8app's github repo to find more about this.

提交回复
热议问题