Angular 2-运行组件测试的问题 [英] Angular 2 - Issues running Component tests

查看:103
本文介绍了Angular 2-运行组件测试的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对Angular 2-rc5运行组件测试,并且在运行测试时遇到以下错误:

I am trying to run a component test for Angular 2 - rc5 and am getting the following error when I run the tests:

socket上缺少错误处理程序. TypeError:(msg ||").replace不是一个函数 在/project/node_modules/karma/lib/reporter.js:48:23

Missing error handler on socket. TypeError: (msg || "").replace is not a function at /project/node_modules/karma/lib/reporter.js:48:23

我得出的结论是,当我注释掉setBaseProviders行(以及组件测试中可能使用这些提供程序的行)时,其余的测试套件可以正常运行.不幸的是,看来我需要这些提供程序才能使用TestComponentBuilder运行组件测试.

I concluded that when I comment out the setBaseProviders line (and the lines in the component tests that might make use of these providers) the rest of the test suite runs without issues. Unfortunately, it appears I need these providers in order to run component tests with the TestComponentBuilder.

// other import statements above
import {
  TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS,
  TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
} from '@angular/platform-browser-dynamic/testing';

setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
  TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
// other code below that is currently commented out

我从node_modules/karma/lib/reporter.js:48内部记录了错误消息,并收到以下消息:'已创建具有不同配置的平台.请先销毁它.'

I logged the error message from inside node_modules/karma/lib/reporter.js:48 and received the following message: 'A platform with a different configuration has been created. Please destroy it first.'

下面是我的package.json文件和karma-conf.js.我还使用了angular2-CLI来设置初始存储库.关于可能是什么问题的任何想法?请让我知道是否还有其他信息可以帮助您进行故障排除.

Below is my package.json file and karma-conf.js. I have also used angular2-CLI in order to set up the initial repository. Any ideas on what may be the issue? Please let me know if there is any other information I could provide to help troubleshoot.

karma.conf

module.exports = function (config) {
  config.set({
    basePath: '..',
    frameworks: ['jasmine'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher')
    ],
    customLaunchers: {
      // chrome setup for travis CI using chromium
      Chrome_travis_ci: {
        base: 'Chrome',
        flags: ['--no-sandbox']
      }
    },
    files: [
      { pattern: 'dist/vendor/es6-shim/es6-shim.js', included: true, watched: false },
      { pattern: 'dist/vendor/zone.js/dist/zone.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/async-test.js', included: true, watched: false },

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

      // Distribution folder.
      { pattern: 'dist/**/*', included: false, watched: true }
    ],
    exclude: [
      // Vendor packages might include spec files. We don't want to use those.
      'dist/vendor/**/*.spec.js'
    ],
    preprocessors: {},
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

package.json

{
  "name": "free-code-camp",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "start": "ng server",
    "postinstall": "typings install",
    "lint": "tslint \"src/**/*.ts\"",
    "format": "clang-format -i -style=file --glob=src/**/*.ts",
    "pree2e": "webdriver-manager update",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^2.0.0-rc.5",
    "@angular/compiler": "^2.0.0-rc.5",
    "@angular/core": "^2.0.0-rc.5",
    "@angular/http": "^2.0.0-rc.5",
    "@angular/platform-browser": "^2.0.0-rc.5",
    "@angular/platform-browser-dynamic": "^2.0.0-rc.5",
    "d3": "^4.2.2",
    "es6-shim": "^0.35.0",
    "reflect-metadata": "0.1.3",
    "rxjs": "5.0.0-beta.6",
    "systemjs": "0.19.31",
    "zone.js": "^0.6.12"
  },
  "devDependencies": {
    "angular-cli": "0.0.*",
    "clang-format": "^1.0.35",
    "codelyzer": "0.0.14",
    "ember-cli-inject-live-reload": "^1.4.0",
    "jasmine-core": "^2.4.1",
    "jasmine-spec-reporter": "^2.4.0",
    "karma": "^0.13.15",
    "karma-chrome-launcher": "^0.2.3",
    "karma-jasmine": "^0.3.8",
    "protractor": "^3.3.0",
    "ts-node": "^0.5.5",
    "tslint": "^3.6.0",
    "typescript": "^1.8.10",
    "typings": "^0.8.1"
  }
}

推荐答案

所以我对茉莉花和因果报应还是很陌生,但是我遇到了相同的错误:

So I'm quite new to jasmine and karma but I had the same error:

已创建具有不同配置的平台.请先销毁它.

A platform with a different configuration has been created. Please destroy it first.

这可能是因为您已在karma-test-shim.js文件中设置了基本提供程序.

This is probably because you've set the base providers in your karma-test-shim.js file.

如果是这种情况,则需要将其从单元测试中删除:

If that's the case you need to remove this from your unit test:

setBaseTestProviders(
  TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
  TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);

我想您只能设置一次基本提供程序,所以错误可能来自单元测试中的其他地方.

I imagine you can only set the base providers once, so the error might come from elsewhere within your unit test.

karam-test-shim.js应该看起来像这样:

karam-test-shim.js should look like this:

..
var testing = require('@angular/core/testing');
var browser = require('@angular/platform-browser-dynamic/testing');

testing.setBaseTestProviders(
  browser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
  browser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS
);

这篇关于Angular 2-运行组件测试的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆