Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module is not a function - Karma + jasmine + webpack + angular

I have a simple angular (1.2.24) app and I have my unit test in Jasmine and I use Karma and webpack.

When I execute karma I get issue with this line of code in my unit test

beforeEach(module('Test'));

The error I get is

TypeError: module is not a function
    at Suite.<anonymous> (tests.webpack.js:77:14)
    at Object.<anonymous> (tests.webpack.js:75:48)
    at Object.<anonymous> (tests.webpack.js:94:31)
    at __webpack_require__ (tests.webpack.js:20:30)
    at webpackContext (tests.webpack.js:58:10)
    at Array.forEach (native)
    at Object.<anonymous> (tests.webpack.js:48:17)

Here is my karma.config.js

var webpack = require('webpack');

module.exports = function (config) {
  config.set({
    browsers: [ 'Chrome' ], //run in Chrome
    plugins: [
            'karma-jasmine',
            'karma-webpack',
            'karma-chrome-launcher'

        ],
    singleRun: true, //just run once by default
    frameworks: [ 'jasmine' ], //use the mocha test framework
    files: [
      'tests.webpack.js', //just load this file
      'TestBuild/Scripts/angular.js',
      'TestBuild/Scripts/ui-router.js'
    ],
    preprocessors: {
      'tests.webpack.js': [ 'webpack' ] //preprocess with webpack and our sourcemap loader
    },
    reporters: [ 'dots' ], //report results in this format
    webpack: { //kind of a copy of your webpack config
      devtool: 'inline-source-map', //just do inline source maps instead of the default
      module: {
        loaders: [
          { test: /js-tests\/\.js$/ , loader: 'babel-loader'}
        ]
      }
    },
    webpackServer: {
      noInfo: true //please don't spam the console when running in karma!
    }
  });
};

tests.webpack.js

var context = require.context('./js-tests', true, /\.js$/); 
context.keys().forEach(context);

package.json

"main": "karma.conf.js",
  "dependencies": {
    "jasmine": "^2.5.0",
    "karma": "^1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-webpack": "^1.8.0",
    "webpack": "^1.13.2",
    "jasmine-core": "^2.5.0",
    "karma-jasmine": "^1.0.2"
  },
  "devDependencies": {
    "babel-core": "^6.14.0",
    "jasmine-core": "^2.5.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2"
  },

I've tried the solution at karma + jasmine + webpack: module is not a function and it doesn't help.

Kindly let me know how i can fix this issue. Thanks in advance.

like image 979
Frenz Avatar asked Nov 03 '25 18:11

Frenz


1 Answers

It looks like module variable in webpack context relates to its internal use.

Try to replace beforeEach(module('Test')); with beforeEach(angular.mock.module('Test'));

like image 130
Nikita_Kulazhenko Avatar answered Nov 05 '25 14:11

Nikita_Kulazhenko