【问题标题】:Error running npm run unit in Vue.js (babel-preset-stage-2)在 Vue.js (babel-preset-stage-2) 中运行 npm 运行单元时出错
【发布时间】:2019-10-23 09:47:32
【问题描述】:

我是 Vue.js 的初学者,我试图用 Jest 进行一些简单的测试,但是自从使用 Webpack 安装 Vue(第一件事我只是尝试运行脚本),我无法运行npm test 脚本。

我尝试了一些教程(thisthis)让它运行,但我仍然遇到错误。

我在 Github 上搜索了一些问题,看起来像是依赖错误,所以我尝试安装很多 babel 依赖项,更改为我在答案中可以找到的所有内容,删除 node_modules 并重新安装几次,但似乎没有任何效果。

控制台日志错误:

Test suite failed to run

    Plugin/Preset files are not allowed to export objects, only functions. In /home/.../node_modules/babel-preset-stage-2/lib/index.js

      at createDescriptor (node_modules/@babel/core/lib/config/config-descriptors.js:178:11)
      at items.map (node_modules/@babel/core/lib/config/config-descriptors.js:109:50)
          at Array.map (<anonymous>)
      at createDescriptors (node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
      at createPresetDescriptors (node_modules/@babel/core/lib/config/config-descriptors.js:101:10)
      at presets (node_modules/@babel/core/lib/config/config-descriptors.js:47:19)
      at mergeChainOpts (node_modules/@babel/core/lib/config/config-chain.js:320:26)
      at node_modules/@babel/core/lib/config/config-chain.js:283:7
      at buildRootChain (node_modules/@babel/core/lib/config/config-chain.js:120:22)
      at loadPrivatePartialConfig (node_modules/@babel/core/lib/config/partial.js:85:55)

Running coverage on untested files...Failed to collect coverage from /home/.../src/utils/ma
ps.js
ERROR: Plugin/Preset files are not allowed to export objects, only functions. In /home/.../node_modules/babel-preset-stage-2/lib/index.js
STACK: Error: Plugin/Preset files are not allowed to export objects, only functions. In /home/.../node_modules/babel-preset-stage-2/lib/index.js
    at createDescriptor (/home/.../node_modules/@babel/core/lib/config/config-descriptors.js:178:11)
    at items.map (/home/.../node_modules/@babel/core/lib/config/config-descriptors.js:109:50)
    at Array.map (<anonymous>)
    at createDescriptors (/home/.../node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
    at createPresetDescriptors (/home/.../node_modules/@babel/core/lib/config/config-descriptors.js:101:10)
    at presets (/home/.../node_modules/@babel/core/lib/config/config-descriptors.js:47:19)
    at mergeChainOpts (/home/.../node_modules/@babel/core/lib/config/config-chain.js:320:26)
    at /home/.../node_modules/@babel/core/lib/config/config-chain.js:283:7
    at buildRootChain (/home/.../node_modules/@babel/core/lib/config/config-chain.js:120:22)
    at loadPrivatePartialConfig (/home/.../node_modules/@babel/core/lib/config/partial.js:85:55)

我的package.json 文件:

{
  "name": "luiza-challenge",
  "version": "1.0.0",
  "description": "This app makes calls from a API to request address info.",
  "author": "~",
  "private": true,
  "scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "unit": "jest --config test/unit/jest.conf.js --coverage",
    "test": "npm run unit",
    "lint": "eslint --ext .js,.vue src test/unit",
    "build": "node build/build.js"
  },
  "dependencies": {
    "react-scripts": "^3.0.1",
    "vue": "^2.5.2",
    "vue-resource": "^1.5.1"
  },
  "devDependencies": {
    "autoprefixer": "^7.1.2",
    "babel-core": "^6.22.1",
    "babel-eslint": "^8.2.1",
    "babel-helper-vue-jsx-merge-props": "^2.0.3",
    "babel-jest": "^24.8.0",
    "babel-loader": "^7.1.1",
    "babel-plugin-dynamic-import-node": "^1.2.0",
    "babel-plugin-syntax-jsx": "^6.18.0",
    "babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-plugin-transform-vue-jsx": "^3.5.0",
    "babel-preset-env": "^1.3.2",
    "babel-preset-stage-2": "^6.22.0",
    "chalk": "^2.0.1",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^2.1.1",
    "eslint": "^4.15.0",
    "eslint-config-standard": "^10.2.1",
    "eslint-friendly-formatter": "^3.0.0",
    "eslint-loader": "^1.7.1",
    "eslint-plugin-import": "^2.7.0",
    "eslint-plugin-node": "^5.2.0",
    "eslint-plugin-promise": "^3.4.0",
    "eslint-plugin-standard": "^3.0.1",
    "eslint-plugin-vue": "^4.0.0",
    "extract-text-webpack-plugin": "^3.0.0",
    "file-loader": "^1.1.4",
    "friendly-errors-webpack-plugin": "^1.6.1",
    "html-webpack-plugin": "^2.30.1",
    "jest": "^24.8.0",
    "jest-serializer-vue": "^0.3.0",
    "node-notifier": "^5.1.2",
    "optimize-css-assets-webpack-plugin": "^3.2.0",
    "ora": "^1.2.0",
    "portfinder": "^1.0.13",
    "postcss-import": "^11.0.0",
    "postcss-loader": "^2.0.8",
    "postcss-url": "^7.2.1",
    "rimraf": "^2.6.0",
    "semver": "^5.3.0",
    "shelljs": "^0.7.6",
    "uglifyjs-webpack-plugin": "^1.1.1",
    "url-loader": "^2.0.0",
    "vue-jest": "^1.0.2",
    "vue-loader": "^13.3.0",
    "vue-style-loader": "^3.0.1",
    "vue-template-compiler": "^2.5.2",
    "webpack": "^3.6.0",
    "webpack-bundle-analyzer": "^3.3.2",
    "webpack-cli": "^3.3.3",
    "webpack-dev-server": "^2.9.7",
    "webpack-merge": "^4.1.0"
  },
  "engines": {
    "node": ">= 6.0.0",
    "npm": ">= 3.0.0"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}

还有我的.babelrc 文件:

{
  "presets": [
    ["env", {
      "modules": false,
      "targets": {
        "browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
      }
    }],
    "stage-2"
  ],
  "plugins": ["transform-vue-jsx", "transform-runtime"],
  "env": {
    "test": {
      "presets": ["env", "stage-2"],
      "plugins": ["transform-vue-jsx", "transform-es2015-modules-commonjs", "dynamic-import-node"]
    }
  }
}

【问题讨论】:

    标签: javascript vue.js jestjs


    【解决方案1】:

    查看 vue-cli 并查看测试是否适用于新安装,看起来您使用旧 Babel 'babel-core' 时遇到了 Babel 问题。如果您想快速启动并运行 Vue,请尝试 codesandbox.io。

    您可能还想编辑您的帖子,因为您已在 package.json 文件中发布了您的电子邮件

    【讨论】:

      【解决方案2】:

      看起来你没有正确安装 babel。您必须删除 node_modules 文件夹并安装运行 npm installyarn install 的最新 babel 版本。然后尝试再次运行npm run unit。最好的解决方案是在这个页面中使用 Vue CLI,你可以找到 vue babel 配置手册Vue Babel configBabel plugin on github

      【讨论】:

        【解决方案3】:

        经过几次尝试,我明白了。这确实是配置错误,我缺少一些参数。 package.json 中的这些更改一切正常:

        "jest": {
            "moduleFileExtensions": [
              "js",
              "jsx",
              "json",
              "vue"
            ],
            "transform": {
              "^.+\\.vue$": "vue-jest",
              ".+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$": "jest-transform-stub",
              "^.+\\.jsx?$": "babel-jest"
            },
            "transformIgnorePatterns": [
              "/node_modules/(?!jest-test)"
            ],
            "moduleNameMapper": {
              "^@/(.*)$": "<rootDir>/src/$1"
            },
            "snapshotSerializers": [
              "jest-serializer-vue"
            ],
            "testMatch": [
              "**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)"
            ],
            "testURL": "http://localhost/",
            "watchPlugins": [
              "jest-watch-typeahead/filename",
              "jest-watch-typeahead/testname"
            ]
          }
        

        如果您不使用 Vue,并且使用纯 JavaScript 进行测试,那么这些行就可以正常工作:

        "jest": {
            "moduleFileExtensions": [
              "js",
              "json"
            ],
            "transform": {
              ".+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$": "jest-transform-stub",
              "^.+\\.jsx?$": "babel-jest"
            },
            "transformIgnorePatterns": [
              "/node_modules/(?!jest-test)"
            ],
            "moduleNameMapper": {
              "^@/(.*)$": "<rootDir>/src/$1"
            },
            "testMatch": [
              "**/tests/**/*.test.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)"
            ],
            "testURL": "http://localhost/"
          }
        

        如果仍然无法正常工作,请尝试配置 Babel 并使用 Webpack 运行它。我在这两种情况下都使用了这个配置并且工作了。

        【讨论】:

          猜你喜欢
          • 2018-09-06
          • 2016-09-12
          • 2018-07-02
          • 2018-05-11
          • 1970-01-01
          • 2018-11-12
          • 2022-01-05
          • 1970-01-01
          • 2018-11-22
          相关资源
          最近更新 更多