且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何让Karma + Webpack找到模块?

更新时间:2023-09-27 23:36:34

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"
}