React native 开玩笑在我的本地工作,但不是在CircleCI

React native 开玩笑在我的本地工作,但不是在CircleCI,react-native,jestjs,circleci,React Native,Jestjs,Circleci,当我在本地运行npm测试时,我的测试都运行良好,没有问题。然而,当我尝试在CircleCI上运行测试时,我得到了错误 import React from 'react'; ^^^^^ SyntaxError: Unexpected identifier at Runtime._execModule (node_modules/jest-runtime/build/index.js:988:58) 我不确定两者之间的差异在哪里。这是我的package.json { "m

当我在本地运行npm测试时,我的测试都运行良好,没有问题。然而,当我尝试在CircleCI上运行测试时,我得到了错误

import React from 'react';
       ^^^^^

SyntaxError: Unexpected identifier

  at Runtime._execModule (node_modules/jest-runtime/build/index.js:988:58)
我不确定两者之间的差异在哪里。这是我的package.json

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject",
    "test": "jest"
  },
  "dependencies": {
    "@aws-amplify/api": "^1.2.4",
    "@aws-amplify/pubsub": "^1.2.4",
    "aws-amplify": "^2.2.2",
    "aws-amplify-react": "^2.5.4",
    "aws-amplify-react-native": "^3.2.0",
    "expo": "^35.0.0",
    "react": "16.8.3",
    "react-native": "https://github.com/expo/react-native/archive/sdk-35.0.0.tar.gz",
    "react-native-gesture-handler": "~1.3.0",
    "react-native-modal-dropdown": "^0.7.0",
    "react-native-reanimated": "~1.2.0",
    "react-native-screens": "^2.0.0-alpha.32",
    "react-native-svg": "9.9.5",
    "react-native-vector-icons": "^6.6.0",
    "react-native-web": "^0.11.7",
    "react-navigation": "^4.0.10",
    "react-navigation-stack": "^1.10.3",
    "react-navigation-tabs": "^2.6.0",
    "victory-native": "^33.0.0"
  },
  "devDependencies": {
    "babel-preset-expo": "^7.1.0",
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.2",
    "enzyme-react-16-adapter-setup": "^0.1.0",
    "enzyme-to-json": "^3.4.3",
    "jest": "^24.9.0",
    "jest-enzyme": "^7.1.2",
    "react-dom": "^16.8.3"
  },
  "private": true,
  "jest": {
    "preset": "react-native",
    "collectCoverage": true,
    "moduleDirectories": [
      "node_modules",
      "src"
    ],
    "transform": {
      "^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js" 
    },
    "setupFiles": [
      "<rootDir>/jest/setup.js"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!(jest-)?react-native|victory-.*)"
    ],
    "coveragePathIgnorePatterns": [
      "/node_modules/",
      "/jest"
    ]
  }
}

似乎因为我安装了npm,所以所有React和Jest元素都可以正常工作。关于我的配置是否出现问题,你有什么想法吗?

我认为有两件事可能是问题的原因,但这一评论更多的是作为一个提示,而不是一个解决方案

首先,必须确保运行与circleci 10.15相同的节点版本。您可以通过nvm实现这一点

然后,您应该缓存package-lock.json,因为它包含项目依赖关系的更精细的格兰德表示

最后删除节点\模块/并再次安装依赖项

version: 2.2
jobs:
  build:
    docker:
      - image: circleci/node:10.15

    working_directory: ~/repo/my-app

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
            - v2.2-dependencies-{{ checksum "package.json" }}
            # fallback to using the latest cache if no exact match is found
            - v2.2-dependencies-

      - run: npm install

      - save_cache:
          paths:
            - node_modules
          key: v2.2-dependencies-{{ checksum "package.json" }}

      # run tests!
      - run: npm test