簡體   English   中英

嘗試運行測試套件時如何解決玩笑錯誤?

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

嘗試使用 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>)

我試圖通過刪除 node_modules 並重新安裝來解決該問題,但無濟於事。

這是我的 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"
  }
}

這是 jest.config.js 文件的副本,也放在根目錄中:


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

這是我的 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() })

問題解決了。 問題是由在終端中使用“jest”命令運行測試引起的,這與使用 npm 測試腳本不同,后者也只運行“jest”命令。 不同之處在於前者使用的是我在全局安裝的更高版本的 Jest 並最終遇到了問題,而后者使用了與 package.json 中的版本相對應的 Jest 版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM