Testing 引用错误:Can';找不到变量:require。[因果报应,网页]
我试图为我的Vue js应用程序的E2E测试设置karma,但是当我尝试运行npm测试时,我得到了这个错误Testing 引用错误:Can';找不到变量:require。[因果报应,网页],testing,webpack,vue.js,mocha.js,karma-runner,Testing,Webpack,Vue.js,Mocha.js,Karma Runner,我试图为我的Vue js应用程序的E2E测试设置karma,但是当我尝试运行npm测试时,我得到了这个错误 PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR ReferenceError: Can't find variable: require at index.js:1 我正在关注有关测试的官方vue文档 karma.config.js var webpackConfig = require('../webpack.test.config.js') // k
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
ReferenceError: Can't find variable: require
at index.js:1
我正在关注有关测试的官方vue文档
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
,而我给出的是测试/index.js
Ok,所以这个问题是我在预处理器中给出index.js路径时犯的一个非常愚蠢的错误
路径应该是
/index.js
,而我给出了test/index.js
你安装了吗?@craig\u h是的,我有karma webpack installedIt看起来出于某种原因没有应用webpack预处理器。你可以看一看我在我的一个项目中使用的一个简单的karma配置,看看这是否有助于你识别问题:谢谢@craig_h,将预处理器文件路径更改为“/**/*.js”是有效的,但真正的问题是预处理器中给出的index.js路径错误,应该是“/index.js”,我给出了“test/index.js”。谢谢你的帮助,你的评论让我很惊讶there@Vishal如果您已设法解决您的问题,请以书面形式回答您的问题!否则你可能会被否决,失去分数。你安装了吗?@craig_h是的,我有karma webpack installedIt看起来出于某种原因没有应用webpack预处理器。你可以看一看我在我的一个项目中使用的一个简单的karma配置,看看这是否有助于你识别问题:谢谢@craig_h,将预处理器文件路径更改为“/**/*.js”是有效的,但真正的问题是预处理器中给出的index.js路径错误,应该是“/index.js”,我给出了“test/index.js”。谢谢你的帮助,你的评论让我很惊讶there@Vishal如果您已设法解决您的问题,请以书面形式回答您的问题!否则你可能会被否决,失去分数。