Vue Unit Test Error: vuex requires a Promise polyfill in this browser

Vue Unit Test Error: vuex requires a Promise polyfill in this browser

我使用 vue-cli 创建了一个项目,并在其中添加了 vuexvue-router。我正在尝试为其设置单元测试,但出现以下错误。没有 Vuex,它曾经可以工作。

PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR

Error: [vuex] vuex requires a Promise polyfill in this browser.

at webpack:///~/vuex/dist/vuex.js:145:0 <- index.js:9871

以下是相关的包版本:

"babel-core": "^6.0.0",
"babel-eslint": "^7.0.0",
"babel-loader": "^6.0.0",
"vue": "^2.1.0",
"vue-router": "^2.0.3",
"vuex": "^2.0.0",
"vuex-router-sync": "^3.0.0"
"karma": "^1.3.0",
"karma-coverage": "^1.1.1",
"karma-mocha": "^1.2.0",
"karma-phantomjs-launcher": "^1.0.0",
"karma-sinon-chai": "^1.2.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "0.0.26",
"karma-webpack": "^1.7.0",
"webpack": "^1.13.2",
"webpack-dev-middleware": "^1.8.3",
"webpack-hot-middleware": "^2.12.2",
"webpack-merge": "^0.14.1"

以下为karma.conf.js:

// This is a karma config file. For more details see
//   http://karma-runner.github.io/0.13/config/configuration-file.html
// we are also using it with karma-webpack
//   https://github.com/webpack/karma-webpack

var path = require('path')
var merge = require('webpack-merge')
var baseConfig = require('../../build/webpack.base.conf')
var utils = require('../../build/utils')
var webpack = require('webpack')
var projectRoot = path.resolve(__dirname, '../../')

var webpackConfig = merge(baseConfig, {
  // use inline sourcemap for karma-sourcemap-loader
  module: {
    loaders: utils.styleLoaders()
  },
  devtool: '#inline-source-map',
  vue: {
    loaders: {
      js: 'isparta'
    }
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': require('../../config/test.env')
    })
  ]
})

// no need for app entry during tests
delete webpackConfig.entry

// make sure isparta loader is applied before eslint
webpackConfig.module.preLoaders = webpackConfig.module.preLoaders || []
webpackConfig.module.preLoaders.unshift({
  test: /\.js$/,
  loader: 'isparta',
  include: path.resolve(projectRoot, 'src'),
  exclude: /test\/unit|node_modules/
})

// only apply babel for test files when using isparta
webpackConfig.module.loaders.some(function (loader, i) {
  if (loader.loader === 'babel') {
    loader.include = path.resolve(projectRoot, 'test/unit')
    return true
  }
})

module.exports = function (config) {
  config.set({
    // to run in additional browsers:
    // 1. install corresponding karma launcher
    //    http://karma-runner.github.io/0.13/config/browsers.html
    // 2. add it to the `browsers` array below.
    browsers: ['Chrome'],
    frameworks: ['mocha', 'sinon-chai'],
    reporters: ['spec', 'coverage'],
    files: ['./index.js'],
    preprocessors: {
      './index.js': ['webpack', 'sourcemap']
    },
    webpack: webpackConfig,
    webpackMiddleware: {
      noInfo: true
    },
    coverageReporter: {
      dir: './coverage',
      reporters: [
        { type: 'lcov', subdir: '.' },
        { type: 'text-summary' }
      ]
    }
  })
}

使用 Babel polyfill 解决了这个问题。以下是我所做的步骤:

正在安装 Babel Polyfill

npm install --save-dev babel-polyfill

然后在 karma.conf.jsfiles 部分中的源文件和测试文件之前包含 polyfill 文件:

files: [
  '../node_modules/babel-polyfill/dist/polyfill.js',
  'index.js'
],

如果您认为 babel-polyfill 太大,您可以只包含 es6-promise polyfill:

files: [
  '../node_modules/es6-promise/dist/es6-promise.auto.js',
  'index.js'
],

另一方面,如果您不确定您的网站访问者的浏览器是否内置 Promise 支持,您可以在您的条目填写中包含 polyfill,main.js:

import 'es6-promise/auto'

编辑:

好消息! Chrome can run in headless mode since version 59。因此,您现在可以 运行 在无头 Chrome 中而不是 PhantomJS 中进行单元测试。

对于vue-cli/webpack生成的项目,您可以按照以下步骤操作:

  • 通过 npm 或 yarn 安装 karma-chrome-launcher
  • 您还可以删除 karma-phantomjs-launcherkarma-phantomjs-shimphantomjs-prebuilt 来自你的项目。它们适用于 PhantomJS。
  • test/unit/karma.conf.js 中,将 browsers 字段更改为 ['ChromeHeadless'],并从中删除 'phantomjs-shim' frameworks 字段。

这是我的 karma。conf.js,不再使用 polyfill:

var webpackConfig = require('../../build/webpack.test.conf')

module.exports = function(config) {
  config.set({
    // to run in additional browsers:
    // 1. install corresponding karma launcher
    //    http://karma-runner.github.io/0.13/config/browsers.html
    // 2. add it to the `browsers` array below.
    browsers: ['ChromeHeadless'],
    frameworks: ['mocha', 'sinon-chai'],
    reporters: ['spec', 'coverage'],
    files: ['./index.js'],
    preprocessors: {
      './index.js': ['webpack', 'sourcemap']
    },
    webpack: webpackConfig,
    webpackMiddleware: {
      noInfo: true
    },
    coverageReporter: {
      dir: './coverage',
      reporters: [
        { type: 'lcov', subdir: '.' },
        { type: 'text-summary' }
      ]
    }
  })
}

这样做的原因:

  1. Chrome 59 是最新的稳定版本,它支持大多数 ES6 特性,甚至是 ES7/8 的一些特性,没有 polyfill。
  2. PhantomJS 自大约 18 个月前以来就没有更新过。它不支持许多新功能,因为 ES 规范发展如此之快。
  3. PhantomJS 作者宣布停产

我的应用程序是用 webpack 创建的,我发现在 Internet Explorer 中加载我的应用程序并停止此错误的唯一方法是将此脚本放在我的 index.html

的头部

    <!-- Script for polyfilling Promises on IE9 and 10 -->
    <script src='https://cdn.polyfill.io/v2/polyfill.min.js'></script>

希望能帮到你