【问题标题】:Module is not a function - Karma + jasmine + webpack + angular模块不是函数 - Karma + jasmine + webpack + angular
【发布时间】:2017-01-14 13:40:44
【问题描述】:

我有一个简单的 Angular (1.2.24) 应用程序,我在 Jasmine 中进行了单元测试,我使用 Karma 和 webpack。

当我执行 karma 时,我在单元测试中遇到了这行代码的问题

beforeEach(module('Test'));

我得到的错误是

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)

这是我的 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"
  },

我在karma + jasmine + webpack: module is not a function 尝试了解决方案,但没有帮助。

请告诉我如何解决此问题。提前致谢。

【问题讨论】:

    标签: angularjs webpack karma-jasmine


    【解决方案1】:

    看起来 webpack 上下文中的 module 变量与其内部使用有关。

    尝试用beforeEach(angular.mock.module('Test'));替换beforeEach(module('Test'));

    【讨论】:

    • 就我而言,我的单元测试中有一个模块调用。例如。 module('someAngularModuleName')。根据给出答案的@Nikita_Kulazhenko,这需要转换为angular.mock.module('someAngularModuleName')。我希望这可以使答案更加清晰。
    猜你喜欢
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多