【问题标题】:Karma-Jasmine seems to load files twiceKarma-Jasmine 似乎加载了两次文件
【发布时间】:2019-02-15 08:53:03
【问题描述】:

我正在开发一个在 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。但是,我没有在我的配置中添加任何串联的库文件或任何东西,因此特定的解决方案不适用于我。我错过了什么?

【问题讨论】:

    标签: angularjs karma-jasmine karma-runner karma-typescript


    【解决方案1】:

    这就是我在我的业力配置中使用的方式,请尝试使用它进行映射。 node 模块应该在 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}
    ]
    

    【讨论】:

      猜你喜欢
      • 2020-04-06
      • 2022-12-24
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      相关资源
      最近更新 更多