angularjs - ZoneAwarePromise 已被覆盖

标签 angularjs unit-testing jasmine karma-jasmine zone

在将应用程序升级到 rc-7 并运行我的单元测试以确保没有任何问题后,我在大约一半的单元测试中收到此错误消息。

“错误:Zone.js 检测到 ZoneAwarePromise '(window|global).Promise' 已被覆盖。
最可能的原因是在 node_modules/zone.js/中的 Zone.js 之后加载了 Promise polyfill(加载 zone.js 时不需要 Polyfilling Promise api。如果必须加载,请在加载 zone.js 之前加载) dist/zone.js(第 21 行)”

我能想到的最接近的事情是我在单元测试中通过手动返回 observable 来模拟来自 http 调用的响应。

这是一个使用 Jasmine 和 Karma 进行单元测试的 Angular2 应用程序。

更新
因此,根据错误消息,这似乎是加载顺序问题,但是我不会在我可以确定的任何地方加载 polyfill,除非我不小心捕获了它们。我附上了我的 karma.conf.js 和 systemjs.conf.js 以帮助查找错误。

Karma.conf.js

module.exports = function(config) {
  var gulpConfig = require('../gulp/config')();

  /**
   * List of npm packages that imported via `import` syntax
   */
  var dependencies = [
    '@angular',
    'lodash',
    'rxjs'
  ];

  var configuration = {
    basePath: '../../',

    browserNoActivityTimeout: 20000,
    frameworks: ['jasmine'],
    browsers: ['PhantomJS'],
    reporters: ['progress', 'coverage'],

    preprocessors: {},

    // Generate json used for remap-istanbul
    coverageReporter: {
      includeAllSources: true,
      dir: 'coverage/appCoverage/remap/',
      reporters: [
        { type: 'json', subdir: 'report-json' }
      ]
    },

    files: [
      'node_modules/reflect-metadata/Reflect.js',
      'node_modules/zone.js/dist/zone.js',
      'node_modules/zone.js/dist/long-stack-trace-zone.js',
      'node_modules/zone.js/dist/proxy.js',
      'node_modules/zone.js/dist/sync-test.js',
      'node_modules/zone.js/dist/jasmine-patch.js',
      'node_modules/zone.js/dist/async-test.js',
      'node_modules/zone.js/dist/fake-async-test.js',

      'node_modules/angular/angular.js',
      'node_modules/core-js/client/shim.min.js',
      'node_modules/systemjs/dist/system.src.js'
    ],

    // proxied base paths
    proxies: {
      // required for component assests fetched by Angular's compiler
      "/src/": "/base/src/",
      "/app/": "/base/src/app/",
      "/node_modules/": "/base/node_modules/"
    },

    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    //browserNoActivityTimeout: 100000
  };

  configuration.preprocessors[gulpConfig.tmpApp + '**/!(*.spec)+(.js)'] = ['coverage'];
  configuration.preprocessors[gulpConfig.tmpApp + '**/*.js'] = ['sourcemap'];
  configuration.preprocessors[gulpConfig.tmpTest + '**/*.js'] = ['sourcemap'];

  var files = [
    gulpConfig.tmpTest + 'test-helpers/global/**/*.js',
    gulpConfig.src + 'systemjs.conf.js',
    'config/test/karma-test-shim.js',
    createFilePattern(gulpConfig.tmpApp + '**/*.js', { included: false }),
    createFilePattern(gulpConfig.tmpTest + 'test-helpers/*.js', { included: false }),
    createFilePattern(gulpConfig.app + '**/*.html', { included: false }),
    createFilePattern(gulpConfig.app + '**/*.css', { included: false }),
    createFilePattern(gulpConfig.app + '**/*.ts', { included: false, watched: false }),
    createFilePattern(gulpConfig.tmpApp + '**/*.js.map', { included: false, watched: false })
  ];

  configuration.files = configuration.files.concat(files);

  dependencies.forEach(function(key) {
    configuration.files.push({
        pattern: 'node_modules/' + key + '/**/*.js',
        included: false,
        watched: false
    });
  });

  if (process.env.APPVEYOR) {
    configuration.browsers = ['IE'];
    configuration.singleRun = true;
    configuration.browserNoActivityTimeout = 90000; // Note: default value (10000) is not enough
  }

  config.set(configuration);

  // Helpers
  function createFilePattern(path, config) {
    config.pattern = path;
    return config;
  }
}

更新
我的解决方案包括确保我的所有 zone.js 文件最后加载到 karma.conf.js 中。我猜我包含的其他内容也包括 polyfill,但在 zone.js 之后加载,因此引发了错误。将 zone.js 移动到最后一切正常。

最佳答案

我遇到了同样的问题。 (Angular 2 RC.7 Jasmine Karma) 这是由于我在 中加载测试文件的顺序。 karma.conf.js .我正在加载 /zone.js/dist/zone.js 之前 /systemjs/dist/system-polyfills.js .在 systemjs polyfills 之后更改文件顺序以加载 zone.js 解决了这个问题。

这是我在 karma.conf.js 中的当前设置,它为我解决了错误。

files: [
  {pattern: 'dist/vendor/es6-shim/es6-shim.js', included: true, watched: false},
  {pattern: 'dist/vendor/reflect-metadata/Reflect.js', included: true, watched: false},
  {pattern: 'dist/vendor/systemjs/dist/system-polyfills.js', included: true, watched: false},
  {pattern: 'dist/vendor/systemjs/dist/system.src.js', included: true, watched: false},
  {pattern: 'dist/vendor/zone.js/dist/zone.js', included: true, watched: false},
  {pattern: 'dist/vendor/zone.js/dist/proxy.js', included: true, watched: false},
  {pattern: 'dist/vendor/zone.js/dist/sync-test.js', included: true, watched: false},
  {pattern: 'dist/vendor/zone.js/dist/long-stack-trace-zone.js', included: true, watched: false},
  {pattern: 'dist/vendor/zone.js/dist/async-test.js', included: true, watched: false},
  {pattern: 'dist/vendor/zone.js/dist/fake-async-test.js', included: true, watched: false},
  {pattern: 'dist/vendor/zone.js/dist/jasmine-patch.js', included: true, watched: false},

  {pattern: 'config/karma-test-shim.js', included: true, watched: true},

  // Distribution folder.
  {pattern: 'dist/**/*', included: false, watched: true}
],

关于angularjs - ZoneAwarePromise 已被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39496171/

相关文章:

c# - 单元测试时模拟CancellationToken.IsCancellationRequested

javascript - 使用自定义匹配器的 jasmine 2.0 测试失败 : undefined is not a function

javascript - 为 ng-repeat 分配唯一值 ID

javascript - Angular State Provider 抛出无法解决状态错误

javascript - 从子 Controller 获取表单状态到父 Controller

angularjs - 将 Karma-runner 与 AngularJS、Jasmine、CoffeeScript 一起使用

angular - 测试我的组件时,fixture.whenStable() 永远不会解析

JavaScript 。 Uncaught ReferenceError : OnChange is not defined

swift - 如何使用 XCTWaiter 和异常快速测试异步函数

c# - 从同一解决方案中的不同测试项目引用项目中的 C# 命名空间