I want to run my tests on a bunch of modules after webpack has combined them together through the Karma test runner, but whenever I run my tests Karma says,
"Error: Cannot find module "hello.js" at http://localhost:9877/base/src/hello.spec.js?d301966ffc1330826574d9d8fff5a644c3390c68:47"
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"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With