笑话进出口失败

时间:2019-01-11 16:38:04

标签: reactjs jestjs babel

为CRA应用程序运行测试时,所有组件均收到以下错误消息:

ERROR: 'import' and 'export' may appear only with 'sourceType: "module"'

我正在使用create-react-app,具有react 16.7.0-alpha.2和react-dom 16.7.0-alpha.2。

react应用程序嵌套在根目录中的一个目录中。

我在运行npm脚本的根目录中运行玩笑: "test": "jest --config jest.local.config.json --coverage"

我的笑话配置如下:

{
  "testEnvironment": "node",
  "moduleFileExtensions": [
    "js"
  ],
  "coverageDirectory": "coverage",
  "testResultsProcessor": "./node_modules/jest-junit-reporter",
  "collectCoverageFrom": [
    "react-app/!src/__tests__/**/*.js"
  ],
  "testMatch": [
    "**/react-app/src/__tests__/**/?(*.)+(spec|test).js?(x)"
  ]
}

eslintrc看起来像:

{
  "extends": [
    "airbnb",
    "prettier",
    "prettier/react"
  ],
  "env": {
    "browser": true,
    "node": true,
    "jest": true
  },
  "globals": {
    "React": true
  },
  "parser": "babel-eslint",
  "parserOptions": {
    "sourceType": "module",
    "allowImportExportEverywhere": true,
    "ecmaVersion": 8,
    "ecmaFeatures": {
      "experimentalObjectRestSpread": true,
      "impliedStrict": true,
      "classes": true,
      "jsx": true
    }
  },
  "plugins": [
    "prettier"
  ],
  "rules": {
    "no-unused-vars": [
      1,
      {
        "argsIgnorePattern": "res|next|^err"
      }
    ],
    "no-unused-expressions": [
      2,
      {
        "allowTaggedTemplates": true
      }
    ],
    "arrow-body-style": [
      2,
      "as-needed"
    ],
    "no-param-reassign": [
      2,
      {
        "props": false
      }
    ],
    "no-console": 0,
    "import/prefer-default-export": 0,
    "import": 0,
    "func-names": 0,
    "space-before-function-paren": 0,
    "comma-dangle": 0,
    "max-len": 0,
    "import/extensions": 0,
    "no-underscore-dangle": 0,
    "consistent-return": 0,
    "react/display-name": 1,
    "react/react-in-jsx-scope": 0,
    "react/forbid-prop-types": 0,
    "react/no-unescaped-entities": 0,
    "jsx-a11y/accessible-emoji": 0,
    "react/jsx-filename-extension": [
      1,
      {
        "extensions": [
          ".js",
          ".jsx"
        ]
      }
    ],
    "radix": 0,
    "no-shadow": [
      2,
      {
        "hoist": "all",
        "allow": [
          "resolve",
          "reject",
          "done",
          "next",
          "err",
          "error"
        ]
      }
    ],
    "quotes": [
      2,
      "single",
      {
        "avoidEscape": true,
        "allowTemplateLiterals": true
      }
    ],
    "prettier/prettier": [
      "error",
      {
        "trailingComma": "es5",
        "singleQuote": true,
        "printWidth": 120
      }
    ],
    "no-return-assign": 0,
    "no-plusplus": [
      "error",
      {
        "allowForLoopAfterthoughts": true
      }
    ],
    "jsx-a11y/href-no-hash": "off",
    "jsx-a11y/anchor-is-valid": [
      "warn",
      {
        "aspects": [
          "invalidHref"
        ]
      }
    ],
    "react/prefer-stateless-function": 0,
    "react/prop-types": [
      2,
      {
        "ignore": [
          "children"
        ]
      }
    ],
    "jsx-a11y/label-has-for": 0
  }
}

在根目录中,我具有以下依赖项和devDependencies:

"dependencies": {
    "bluebird": "^3.5.3",
    "jest": "^23.6.0",
    "jest-junit-reporter": "^1.1.0",
    "jest-mock-console": "^0.4.2",
    "prettier": "^1.15.3",
  },
  "devDependencies": {
    "babel-eslint": "9.0.0",
    "eslint": "5.6.0",
    "eslint-config-airbnb": "^17.1.0",
    "eslint-config-prettier": "^3.3.0",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-jsx-a11y": "^6.1.2",
    "eslint-plugin-prettier": "^3.0.0",
    "eslint-plugin-react": "^7.11.1"
  }

在react应用中,我具有以下依赖性:

"dependencies": {
    "babel-plugin-import": "^1.11.0",
    "connected-react-router": "^6.0.0",
    "history": "^4.7.2",
    "node-sass": "^4.11.0",
    "prop-types": "^15.6.2",
    "react": "^16.7.0-alpha.2",
    "react-dom": "^16.7.0-alpha.2",
    "react-redux": "^6.0.0",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.1.1",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0"
  }

我不希望从CRA中退出,因为该应用程序的大小不适合它退出。

如果我在react-app目录中运行npx react-scripts test,则能够正确执行测试,但是通过调用jest从父目录运行测试,总是会返回导入/导出错误。我是否缺少配置?

1 个答案:

答案 0 :(得分:0)

使用ES5导入。

例如

const axios = require('axios');