问题描述
我希望在webpack通过Karma测试运行器将它们组合在一起后,在一堆模块上运行我的测试,但每当我运行我的测试时,Karma说,
I have a spec file:
var a = require('hello.js');
describe("a test test", function() {
it("humperdink test", function() {
expect(a).toEqual('humperdink');
}); //end it
}); //end describe
hello.js is this:
var a = 'humperdink';
module.exports = a;
Both of these files are in the same folder.
My karma.conf.js is:
module.exports = function (config) {
config.set({
frameworks: ['jasmine'],
files: [
'src/**/*.js',
'tests/**/*.spec.js'
],
preprocessors: {
'tests/**/*.spec.js': ['webpack'],
'src/**/*.js' : ['webpack']
},
browsers: ['PhantomJS'],
webpack: {
entry: './src/hello.spec.js',
output: {
filename: 'bundle.js'
}
},
webpackMiddleware: {
noInfo: true
}
})
};
Currently my devDependencies installed are
"devDependencies": {
"jasmine-core": "^2.3.4",
"jshint": "^2.8.0",
"karma": "^0.13.15",
"karma-jasmine": "^0.3.6",
"karma-jshint-preprocessor": "0.0.6",
"karma-phantomjs-launcher": "^0.2.1",
"karma-webpack": "^1.7.0",
"phantomjs": "^1.9.19",
"sinon": "^1.17.2",
"webpack": "^1.12.9"
How do I get Karma to find the hello.js module?
I've tried changing the spec file's first line to things like
require('hello.js');
or
require('./hello.js');
or
require('hello');
on the advice of Karma Webpack - Error: Cannot find module "./test/utilities.js"
I don't think there's anything too complicated going on here like, Cannot find module error when using karma-webpack.
I have checked to make sure that the Karma test runner is working otherwise. If I run a really simple test in its own file it works just fine.
How do I solve this problem?
I have replicate your project and fix it. Following the https://github.com/webpack/karma-webpack
In the spec:
var a = require('../src/hello.js');
karma.conf.js:
module.exports = function (config) {
config.set({
frameworks: ['jasmine'],
files: [
//'src/**/*.js', <-------- Remove or comment this
'tests/**/*.spec.js'
],
preprocessors: {
'tests/**/*.spec.js': ['webpack'],
'src/**/*.js' : ['webpack']
},
browsers: ['PhantomJS'],
webpack: {
entry: './tests/hello.spec.js',
output: {
filename: 'bundle.js'
}
},
webpackMiddleware: {
noInfo: true
}
})
};
And additionally for npm test command:in package.json:
"scripts": {
"test": "./node_modules/karma/bin/karma start"
}
这篇关于如何让Karma + Webpack找到模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!