【问题标题】:Karma + Webpack Angular controller not found找不到 Karma + Webpack Angular 控制器
【发布时间】:2016-06-14 08:27:14
【问题描述】:

我已经使用 webpack with help from this blog post 设置了一个 Angular 项目,它具有 an associated boilerplate on Github

应用程序本身运行良好,但是当我运行 karma 时,它说找不到它正在尝试测试的控制器。

// karma.config.js
module.exports = config => config.set({
  files: ['test.webpack.js'],
  preprocessors: {
    'test.webpack.js': ['webpack', 'sourcemap']
  },
  browsers: ['PhantomJS2'],
  webpack: require('./webpack.config'),
  webpackMiddleware: {
    noInfo: 'errors-only',
  },
});

// test.webpack.js
import 'angular';
import 'angular-mocks/angular-mocks';

const testContext = require.context('./', true, /\.spec\.js$/);
testContext.keys().forEach(testContext);

我现在只有一个 MainCtrl.spec.js 的规范文件。 Karma 会运行它,但我得到了错误:

参数 'MainCtrl' 不是函数,未定义

当我实际运行应用程序时,MainCtrl 似乎加载得很好,所以我不确定为什么 Karma 无法获取它。

我也尝试将 \.spec\.js 更改为 \.js 但这会导致更多错误。

【问题讨论】:

    标签: javascript angularjs karma-runner webpack


    【解决方案1】:

    我能够通过重新考虑工作流程来解决这个问题。两个主要问题是module 没有从ngMocks 全局导出(与节点导入系统存在冲突)。另一个是测试还必须首先加载所有模块文件,这对于 Karma 来说非常典型。

    // karma.config.js
    files: ['app/index.js', 'test.webpack.js']
    preprocessors: {
      'app/index.js': ['webpack'],
      'test.webpack.js': ['webpack'],
    }
    
    
    // test.config.js
    /* Angular is already imported by app/index.js */
    import 'angular-mocks/angular-mocks';
    
    /* MUST use `angular.mock.module`. `module` alone won't work */    
    beforeEach(angular.mock.module('ngculator'));
    const testContext = require.context('.', true, /\.spec\.js$/);
    testContext.keys().forEach(testContext);
    

    【讨论】:

      猜你喜欢
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      • 2018-08-16
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多