ReferenceError:找不到变量:require。 [因果报应,的WebPack]
问题描述:
我试图建立因果报应我Vue公司的js应用的端到端测试,但是当我尝试运行NPM测试,我得到这个错误ReferenceError:找不到变量:require。 [因果报应,的WebPack]
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'],
},
};
答
好了,所以这个问题是一个非常愚蠢的错误,同时给予在预处理index.js的路径我做了。
路径应该是./index.js
而我已经给test/index.js
。
您是否安装了[karma-webpack](https://www.npmjs.com/package/karma-webpack)? –
@craig_h是的,我确实安装了karma-webpack – Vishal
它看起来像是由于某种原因webpack预处理器没有被应用。你可以看一下我用于我的一个项目的一个简单的因果配置,看看能否帮助你找出问题:https://github.com/craigh411/vue-star-rating/blob/master/karma.conf .js –