Webpack + Karma + Angular 1 TypeError:模块不是函数

问题描述

我正在使用旧版代码来更新测试和构建过程,以适应我们较新的Webpack过程。

我们有一个旧的角度应用程序(v1.7.9),我已经能够使用webpack完美地构建它,但是在运行其测试套件(karma +茉莉花)时遇到了一些问题。

到目前为止,我的karama配置看起来像:

module.exports = function (config) {
  config.set({
    frameworks: ["jasmine"],plugins: [
      "karma-jasmine","karma-webpack","karma-chrome-launcher","karma-coverage",],files: ["tests/index.test.js"],preprocessors: {
      "tests/index.tests.js": ["webpack"],"tests/**/*.test.js": ["webpack"],},reporters: ["progress","coverage"],webpack: { mode: "development" },coverageReporter: {
      dir: "tests/results/",reporters: [
        { type: "html",subdir: "report-html" },{ type: "lcov",subdir: "report-lcov" },port: 9876,colors: true,logLevel: config.LOG_INFO,autoWatch: false,browsers: ["ChromeHeadless"],singleRun: true,});
};

我的测试入口点文件看起来像

//tests/index.test.js
import "angular";
import "angular-mocks";

const testsContext = require.context(".",true,/\.test$/);
testsContext.keys().forEach(testsContext);

测试示例如下:

/* global module,inject,SampleController */

describe("SampleController",function() {
  beforeEach(module("MyAngularapp"));

  var controller;

  beforeEach(inject(function(_$controller_,_$rootScope_) {
    controller = _$controller_(SampleController,{});
  }));

  describe("changeQuantity",() => {
    it("should reduce the quantity when the step is negative",() => {
  ...

我也在babel配置中使用plugins: ["angularjs-annotate"]

我收到的错误是:

"message": "An error was thrown in afterall\nTypeError: module is not a function\n    at Suite.eval (webpack:///./tests/common/mySampleService.test.js?:4:14)\n    at <Jasmine>\n    at Object.eval (webpack:///./tests/common/mySampleService.test.js?:3:1)\n    at eval (webpack:///./tests/common/mySampleService.test.js?:34:30)\n    at Object../tests/common/mySampleService.test.js (tests/index.test.js:305:1)\n    at __webpack_require__ (tests/index.test.js:20:30)\n    at webpackContext (webpack:///./tests_sync_\\.test$?:34:9)\n    at <Jasmine>",

要使这些测试正常工作,我是否缺少某些明显的东西?有很多测试,因此必须进行更改将是一场噩梦。我正在查看我的webpack,业力配置或babel中是否存在可以修复的配置,以使它们起作用。

我怀疑它必须在每个测试文件的顶部带有尖角注释,并且与那些在运行测试时丢失的注释(即/* global module,SampleController */)有关。

注意:我没有编写任何这些测试或任何角度代码

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)