简体   繁体   English

尝试运行测试套件时如何解决玩笑错误?

[英]How to resolve jest error when attempting to run test suite?

I am receiving the following error when attempting to run a test suite using jest:尝试使用 jest 运行测试套件时收到以下错误:

  ● Test suite failed to run

    TypeError: Cannot read property '1' of undefined

      at Plugin.manipulateOptions (node_modules/babel-plugin-module-resolver/lib/index.js:88:9)
          at run.next (<anonymous>)
          at transform.next (<anonymous>)

I have attempted to resolve the issue by removing node_modules and reinstalling to no avail.我试图通过删除 node_modules 并重新安装来解决该问题,但无济于事。

Here is a copy of my package.json which is placed in the root directory:这是我的 package.json 的副本,它位于根目录中:

{
  "name": "obfuscate",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest",
    "test:coverage": "jest --coverage",
    "test:snapshots": "jest --updateSnapshot",
    "test:watch": "jest --watch -o",
    "serve": "node -r babel-register node_modules/webpack-dev-server/bin/webpack-dev-server --config config/webpack/environments/serve/index.js",
    "build:dev": "node -r babel-register node_modules/webpack/bin/webpack --config config/webpack/environments/dev/index.js",
    "build:prod": "node -r babel-register node_modules/webpack/bin/webpack --config config/webpack/environments/prod/index.js",
    "start": "npm run build:dev",
    "lint:js": "eslint \"src/**/*.js\"",
    "lint:styles": "stylelint \"src/**/*.scss\"",
    "prepush": "npm run lint:styles && npm run lint:js",
    "browsersync": "npx browser-sync start --config bs-config.js",
    "bsync": "concurrently \"npm run start \" \"npx browser-sync start --config bs-config.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-eslint": "^8.0.2",
    "babel-jest": "^22.0.4",
    "babel-loader": "^7.1.2",
    "babel-plugin-module-resolver": "^2.7.1",
    "babel-plugin-syntax-async-functions": "^6.13.0",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-decorators": "^6.24.1",
    "babel-plugin-transform-do-expressions": "^6.22.0",
    "babel-plugin-transform-export-extensions": "^6.22.0",
    "babel-plugin-transform-function-bind": "^6.22.0",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-plugin-transform-react-constant-elements": "^6.23.0",
    "babel-plugin-transform-react-jsx": "^6.24.1",
    "babel-plugin-transform-react-remove-prop-types": "^0.4.24",
    "babel-plugin-transform-regenerator": "^6.26.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.6.0",
    "babel-register": "^6.26.0",
    "cross-env": "^5.0.5",
    "css-loader": "^0.28.7",
    "del-webpack-plugin": "^1.0.6",
    "domready": "^1.0.8",
    "enzyme": "^3.8.0",
    "enzyme-adapter-react-16": "^1.9.1",
    "extract-text-webpack-plugin": "^3.0.0",
    "file-loader": "^1.1.6",
    "friendly-errors-webpack-plugin": "^1.6.1",
    "html-loader": "^0.5.1",
    "husky": "^0.14.3",
    "identity-obj-proxy": "^3.0.0",
    "img-loader": "^2.0.0",
    "jest": "^22.0.4",
    "jsdom": "^16.2.0",
    "node-sass": "^4.11.0",
    "optimize-css-assets-webpack-plugin": "^3.2.0",
    "postcss-loader": "^2.0.6",
    "react": "^16.8.2",
    "react-collapsible": "^2.1.0",
    "react-countup": "^4.0.0",
    "react-dom": "^16.8.2",
    "react-equalizer": "^1.3.0",
    "react-router-dom": "^4.3.1",
    "react-scroll": "^1.7.8",
    "react-slick": "^0.23.1",
    "react-spinners": "^0.3.2",
    "react-tabs": "^2.1.1",
    "react-text-mask": "^5.1.0",
    "react-transition-group": "^2.5.0",
    "react-widgets": "^4.1.2",
    "sass-loader": "^6.0.6",
    "sass-resources-loader": "^1.3.5",
    "standard-loader": "^6.0.1",
    "stylelint": "^8.4.0",
    "stylelint-config-standard": "^18.0.0",
    "stylelint-webpack-plugin": "^0.9.0",
    "uglifyjs-webpack-plugin": "^1.3.0",
    "url-loader": "^0.6.1",
    "webpack": "^3.6.0",
    "webpack-bundle-analyzer": "^2.13.1",
    "webpack-cli": "^3.2.3",
    "webpack-dev-server": "^2.0.0",
    "webpack-manifest-plugin": "^2.0.0-rc.1",
    "webpack-merge": "^4.2.1"
  },
  "dependencies": {
    "@obfuscate/components": "^1.0.125",
    "@obfuscate/react-ranger": "^1.0.0",
    "axios": "^0.19.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-polyfill": "^6.26.0",
    "blazy": "^1.8.2",
    "browser-sync": "^2.26.7",
    "classlist-polyfill": "^1.2.0",
    "concurrently": "^4.1.2",
    "core-js": "^3.6.0",
    "eslint": "^6.7.2",
    "eslint-config-standard": "^14.1.0",
    "eslint-loader": "^3.0.3",
    "formdata-polyfill": "^3.0.19",
    "isomorphic-fetch": "^2.2.1",
    "js-match-height": "^1.0.7",
    "mobx": "^4.9.2",
    "mobx-react": "^5.4.3",
    "mobx-react-form": "^1.36.3",
    "nodelist-foreach-polyfill": "^1.2.0",
    "regenerator-runtime": "^0.13.3",
    "scroll-lock": "^2.1.2",
    "smoothscroll": "^0.4.0",
    "swiper": "^5.2.1",
    "url-search-params": "^1.1.0",
    "validatorjs": "^3.15.1"
  },
  "stylelint": {
    "extends": "stylelint-config-standard",
    "ignoreFiles": [
      "src/styles/vendor/**/*.scss"
    ],
    "ignoreProperties": [
      "composes"
    ],
    "rules": {
      "no-descending-specificity": null,
      "max-empty-lines": 4,
      "string-quotes": "single",
      "function-url-quotes": "always",
      "color-hex-length": "long",
      "declaration-colon-newline-after": null,
      "declaration-block-no-redundant-longhand-properties": null,
      "font-family-name-quotes": null,
      "number-leading-zero": "never",
      "value-list-comma-newline-after": null,
      "max-nesting-depth": 4,
      "selector-pseudo-element-colon-notation": "single",
      "at-rule-name-space-after": null,
      "declaration-empty-line-before": null,
      "at-rule-empty-line-before": null,
      "at-rule-no-unknown": [
        true,
        {
          "ignoreAtRules": [
            "function",
            "extend",
            "if",
            "else",
            "each",
            "mixin",
            "include",
            "return",
            "media",
            "at-root",
            "debug",
            "warn",
            "error",
            "for",
            "content"
          ]
        }
      ],
      "property-no-unknown": [
        true,
        {
          "ignoreProperties": [
            "composes"
          ]
        }
      ]
    }
  },
  "jest": {
    "testURL": "http://localhost",
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|html)$": "<rootDir>config/jest/jest.filemock.js",
      "\\.(css|scss)$": "identity-obj-proxy"
    },
    "setupFiles": [
      "./config/jest/jest.setup.js"
    ],
    "modulePaths": [
      "<rootDir>/src"
    ],
    "collectCoverageFrom": [
      "src/**/*.js",
      "!src/base.js",
      "!src/index-react.js",
      "!src/index.js"
    ],
    "transform": {
      "^.+\\.js?$": "babel-jest"
    },
    "verbose": true
  },
  "standard": {
    "globals": [
      "jest",
      "describe",
      "it",
      "expect",
      "shallow",
      "snapshot",
      "test",
      "enzyme",
      "mount",
      "after",
      "afterEach",
      "beforeEach",
      "parent",
      "fetch"
    ],
    "ignore": [
      "node_modules/**",
      "*.test.js"
    ],
    "parser": "babel-eslint"
  }
}

Here is a copy of the jest.config.js file also placed in the root directory:这是 jest.config.js 文件的副本,也放在根目录中:


module.exports = {
  clearMocks: true,
  coverageDirectory: "coverage",
  testURL: "http://localhost",
};

Here is a copy of my jest.setup.js file which is placed in config/jest:这是我的 jest.setup.js 文件的副本,它位于 config/jest 中:

import { configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'

window.matchMedia = window.matchMedia || function () {
    return {
        matches: false,
        addListener: function () { },
        removeListener: function () { }
    }
}

configure({ adapter: new Adapter() })

Issue resolved.问题解决了。 Issue was caused by running the tests by using the "jest" command in the terminal which is different from using the npm test script which also only runs the "jest" command.问题是由在终端中使用“jest”命令运行测试引起的,这与使用 npm 测试脚本不同,后者也只运行“jest”命令。 The difference is the former was using a higher version of Jest which I had installed globally and ended up running into issues whereas the latter used the version of jest that corresponds with the version in the package.json.不同之处在于前者使用的是我在全局安装的更高版本的 Jest 并最终遇到了问题,而后者使用了与 package.json 中的版本相对应的 Jest 版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM