ReferenceError:找不到变量:require。 [业力,网络包装]

时间:2017-06-21 13:05:42

标签: testing webpack vue.js mocha karma-runner

我正在尝试为我的Vue js应用程序的E2E测试设置业力,但是当我尝试运行npm测试时出现此错误

PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
ReferenceError: Can't find variable: require
at index.js:1

我正在关注测试的官方vue文档 https://vue-loader.vuejs.org/en/workflow/testing.html

karma.config.js

var webpackConfig = require('../webpack.test.config.js')

// karma.conf.js
module.exports = function (config) {
  config.set({
    browsers: ['PhantomJS'],
    frameworks: ['jasmine'],
    // this is the entry file for all our tests.
    files: ['./index.js'],
    // we will pass the entry file to webpack for bundling.
    preprocessors: {
      './test/index.js': ['webpack']
    },
    // use the webpack config
    webpack: webpackConfig,
    // avoid walls of useless text
    webpackMiddleware: {
      noInfo: true
    },
    singleRun: true
  })
}

//index.js referenced in above karma config
var testsContext = require.context('.', true, /\.spec$/)    
testsContext.keys().forEach(testsContext)

//webpack.test.config.js
const webpack = require('webpack');
const path = require('path');
const projectRoot = path.resolve(__dirname, '../');

module.exports = {

  module: {

    loaders: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        query: {
          presets: ['es2015']
        },
        include: [
          projectRoot,
        ],
        exclude: /node_modules/,
      },
    ],
  },
  plugins: [
   new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery"
    })
  ],
  resolve: {
    extensions: ['.js', '.vue'],
  },
};

1 个答案:

答案 0 :(得分:3)

好的,所以这个问题是我在预处理器中给出index.js路径时犯的一个非常愚蠢的错误。

在我提供./index.js时,

路径应该是test/index.js