2016-03-01 95 views
0

我已经使用webpack with help from this blog post设置了一个Angular项目,其中有an associated boilerplate on Github未找到Karma + Webpack角度控制器

该应用程序本身工作正常,但是当我运行业力它说它找不到它正在测试的控制器。

// 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。噶将运行它,但我得到的错误:

Argument 'MainCtrl' is not a function, got undefined

当我实际运行的应用程序,MainCtrl似乎加载就好了,所以我不知道为什么噶不能得到它。

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

回答

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);