我正在做一个在TypeScript中使用AngularJS 1.7的项目。对于测试,我们使用Karma运行的Jasmine。然而,似乎当我试图加载库文件时,Karma加载了它们两次,这当然会导致许多问题。我是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加载了两次文件,因为一些搜索导致了我的here。但是,我没有在我的配置中添加任何串联的库文件或任何东西,因此这个特定的解决方案不适用于我。我遗漏了什么?
发布于 2018-09-11 05:57:45
这就是我如何在我的业力配置中使用,请尝试与此映射。节点模块应该在vendor.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}
]https://stackoverflow.com/questions/52265956
复制相似问题