我正在 TypeScript 中使用 AngularJS 1.7 进行一个项目。对于测试,我们使用由Karma运行的Jasmine。但是,似乎当我尝试加载库文件时,Karma 会加载它们两次,这当然会导致许多问题。我对业力相当陌生,所以很可能我只是配置错误。这是有问题的 Karma 配置文件:
module.exports = function(config) {
config.set({
port: 9877,
files: [
'node_modules/angular/angular.js',
'node_modules/angular-animate/angular-animate.js',
'node_modules/angular-mocks/angular-mocks.js',
'node_modules/angular-ui-bootstrap/dist/ui-bootstrap.js',
'node_modules/core-js/client/shim.min.js',
'node_modules/d3/d3.js',
'node_modules/jquery/dist/jquery.js',
'node_modules/nvd3/build/nv.d3.js',
'./app/**/*.js',
'./app/**/*.ts'
],
frameworks: ['jasmine', 'karma-typescript'],
preprocessors: {
'./app/**/*.test.ts': ['karma-typescript'],
'./app/**/!(*.test).ts': ['karma-typescript', 'coverage']
},
reporters: ['progress', 'karma-typescript', 'coverage'],
coverageReporter: {
type: 'text-summary'
},
karmaTypescriptConfig: {
tsconfig: "./tsconfig.json"
},
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
browsers: ['ChromeHeadless'],
//https://github.com/karma-runner/karma-chrome-launcher/issues/73
//https://developers.google.com/web/updates/2017/06/headless-karma-mocha-chai
customLaunchers: {
ChromeNoSandbox: {
base: 'Chrome',
flags: ['--headless', '--disable-translate', '--remote-debugging-port=9222', '--no-sandbox']
}
},
plugins: [
'karma-chrome-launcher',
'karma-coverage',
'karma-jasmine',
'karma-typescript'
],
// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,
browserNoActivityTimeout: 30000
});
};
如果我拿出node_modules
中列出的文件,Karma抱怨一些AngularJS的东西是未定义的,所以我几乎可以肯定它们是必要的。但是当我尝试运行测试时,我收到以下错误:
HeadlessChrome 68.0.3440 (Windows 7.0.0) LOG: 'WARNING: Tried to load AngularJS more than once.'
Error: [$injector:modulerr] Failed to instantiate module ng due to:
at Function.angular.injector.$$annotate (node_modules/angular-mocks/angular-mocks.js:1:1)
at Function.angular.injector.$$annotate (node_modules/angular-mocks/angular-mocks.js:2921:36)
at Function.angular.injector.$$annotate (node_modules/angular-mocks/angular-mocks.js:2921:36)
at Function.angular.injector.$$annotate (node_modules/angular-mocks/angular-mocks.js:2921:36)
at Function.angular.injector.$$annotate (node_modules/angular-mocks/angular-mocks.js:2921:36)
at Function.angular.injector.$$annotate (node_modules/angular-mocks/angular-mocks.js:2921:36)
at Function.angular.injector.$$annotate (node_modules/angular-mocks/angular-mocks.js:2921:36)
at Function.angular.injector.$$annotate (node_modules/angular-mocks/angular-mocks.js:2921:36)
at Function.angular.injector.$$annotate (node_modules/angular-mocks/angular-mocks.js:2921:36)
at Function.angular.injector.$$annotate (node_modules/angular-mocks/angular-mocks.js:2921:36)
at <Jasmine>
at <Jasmine>
at node_modules/angular/angular.js:138:12
at node_modules/angular/angular.js:5027:15
at forEach (node_modules/angular/angular.js:387:20)
at loadModules (node_modules/angular/angular.js:4987:5)
at Object.createInjector [as injector] (node_modules/angular/angular.js:4904:19)
at UserContext.WorkFn (C:/Users/cdawson/AppData/Local/Temp/karma-typescript-bundle-17920e0YyaRMZnBnq.js:38954:52)
at window.inject.angular.mock.inject (C:/Users/cdawson/AppData/Local/Temp/karma-typescript-bundle-17920e0YyaRMZnBnq.js:38934:42)
at UserContext.<anonymous> (app/Alarm/alarm_alarmHub.service.test.ts:93:12 <- app/Alarm/alarm_alarmHub.service.test.js:77:13)
at <Jasmine>
我怀疑这是Karma加载文件两次,因为一些搜索导致我来到这里。但是,我没有向我的配置添加任何级联库文件或任何内容,因此该特定解决方案不适用于我。我错过了什么?
这就是我在业力配置中的使用方式,请尝试与此映射。 节点模块应该在供应商本身.js编译
files: [
// vendor files must be built as mock mode. ie, using "--api mock" (see commands below)
// 1. gulp build --api mock
'public/js/vendor.js',
// src & http-backend files
'src/app/**/app.!(spec).js',
'src/app/**/!(*.spec).js',
// complied templates
'public/js/app.templates.js',
// spec files
'src/app/**/*.spec.js',
// fixtures
{pattern: 'src/mocks/**/*.json', included: false}
]