How can I get Karma + Webpack to search for a module?

I want to run my tests on a bunch of modules after webpack has combined them together through a Karma test drive, 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:

 var a = 'humperdink'; module.exports = a; 

Both of these files are in the same folder.

My karma.conf.js:

 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 } }) }; 

My devDependencies are currently installed

 "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 tried changing the first line of the file specification to things like

 require('hello.js'); 

or

 require('./hello.js'); 

or

 require('hello'); 

as recommended by Karma Webpack - Error: Cannot find the module. /test/utilities.js "

I don’t think that something is too complicated here. It is impossible to find a module error when using a karma web package .

I checked to make sure the Karma testing runner works differently. If I run a very simple test in my own file, it works fine.

How to solve this problem?

+5
source share
1 answer

I am replicating your project and fixing it. Following https://github.com/webpack/karma-webpack

In the specification:

 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 } }) }; 

karma parameters lead to the terminal

Also, for the npm test : command in package.json:

 "scripts": { "test": "./node_modules/karma/bin/karma start" } 
+9
source

Source: https://habr.com/ru/post/1236880/


All Articles