Karma-Jasmine似乎加载了两次文件

问题描述 投票:0回答:1

我正在使用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>

我怀疑它是由于一些搜索导致我here两次加载文件的Karma。但是,我没有在我的配置中添加任何连接的库文件或任何内容,因此特定的解决方案不适用于我。我错过了什么?

angularjs karma-jasmine karma-runner karma-typescript
1个回答
0
投票

这是我在我的业力配置中使用的方式,请尝试用此映射。节点模块应该在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}
]
© www.soinside.com 2019 - 2024. All rights reserved.