在Vue.js中运行npm运行单元时出错(babel-preset-stage-2)

时间:2019-06-07 22:35:03

标签: javascript vue.js jestjs

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

3 个答案:

答案 0 :(得分:0)

查看vue-cli并查看测试是否适用于新安装,看起来您正在使用旧的Babel“ babel-core”遇到Babel问题。如果您想要快速启动和运行Vue的方法,请尝试使用codeandbox.io。

您也可能希望编辑您的帖子,因为您已将电子邮件发布到package.json文件中

答案 1 :(得分:0)

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

答案 2 :(得分:0)

经过几次尝试,我明白了。这确实是配置错误,我缺少一些参数。通过 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 运行它。我在两种情况下都使用了此配置,并且可以正常工作。