Angular2 - 使用PhantomJS运行单元测试时出现配置错误

时间:2016-09-30 10:37:49

标签: angular phantomjs

在我的Angular2.0.1应用程序中,我想使用karma和PhantomJS进行单元测试。当我发出

npm run test

我有一个日志

[karma]: Karma v1.3.0 server started at http://localhost:9876/
[launcher]: Launching browser PhantomJS with unlimited concurrency
[launcher]: Starting browser PhantomJS
[PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on socket /#weBxwT1c9A8XQjo9AAAA with id 35739171
PhantomJS 2.1.1 (Windows 8 0.0.0) ERROR
  Error: Missing: SyncTestZoneSpec
  at node_modules/zone.js/dist/jasmine-patch.js:32

我该如何解决?

我的package.json

{
  "scripts": {
    "start": "tsc && concurrently \"watch-less -d ./less -r ./css -e .css\" \"tsc -w\" \"lite-server\" ",
    "watch": "concurrently \"watch-less -d ./less -r ./css -e .css\" \"tsc -w\"",
    "lite": "lite-server",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings",
    "test": "karma start --browsers PhantomJS",
    "e2e": "protractor protractor.config.js",
    "e2e-jenkins": "concurrently -k -s first \"lite-server -c bs-config-jenkins.js\" \"protractor protractor-jenkins.config.js\"",
    "extract": "ng-xi18n"
  },
  "license": "ISC",
  "dependencies": {
    "@angular/common": "~2.0.1",
    "@angular/compiler": "~2.0.1",
    "@angular/core": "~2.0.1",
    "@angular/forms": "~2.0.1",
    "@angular/http": "~2.0.1",
    "@angular/platform-browser": "~2.0.1",
    "@angular/platform-browser-dynamic": "~2.0.1",
    "@angular/router": "~3.0.1",
    "@angular/upgrade": "~2.0.1",
    "angular2-in-memory-web-api": "0.0.20",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-beta.12",
    "systemjs": "0.19.39",
    "zone.js": "^0.6.25"
  },
  "devDependencies": {
    "canonical-path": "0.0.2",
    "concurrently": "^3.0.0",
    "jasmine": "^2.5.2",
    "jasmine-fail-fast": "^2.0.0",
    "karma": "^1.3.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-firefox-launcher": "^1.0.0",
    "karma-htmlfile-reporter": "^0.3.4",
    "karma-jasmine": "^1.0.2",
    "karma-phantomjs-launcher": "^1.0.2",
    "lite-server": "^2.2.2",
    "protractor": "^4.0.8",
    "protractor-jasmine2-screenshot-reporter": "^0.3.2",
    "typescript": "^1.8.10",    
    "typings": "^1.4.0",
    "watch-less": "0.0.4"
  }
}

karma.conf.js

module.exports = function(config) {

  var appBase   = 'app/';      // transpiled app JS files
  var appAssets ='/base/app/'; // component assets fetched by Angular's compiler

  config.set({
    basePath: '',
    frameworks: ['jasmine'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-firefox-launcher'),
      require('karma-phantomjs-launcher'),
      require('karma-htmlfile-reporter')
    ],

    customLaunchers: {
      // From the CLI. Not used here but interesting
      // chrome setup for travis CI using chromium
      Chrome_travis_ci: {
        base: 'Chrome',
        flags: ['--no-sandbox']
      }
    },
    files: [
      // Polyfills
      'node_modules/core-js/client/shim.js',
      'node_modules/reflect-metadata/Reflect.js',

      // System.js for module loading
      'node_modules/systemjs/dist/system-polyfills.js',
      'node_modules/systemjs/dist/system.src.js',

      // Zone.js dependencies
      'node_modules/zone.js/dist/zone.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',

      // RxJs.
      { pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false },
      { pattern: 'node_modules/rxjs/**/*.js.map', included: false, watched: false },

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

      // Angular 2 itself and the testing library
      { pattern: 'node_modules/@angular/**/*.js', included: false, watched: false },
      { pattern: 'node_modules/@angular/**/*.js.map', included: false, watched: false },

      // transpiled application & spec code paths loaded via module imports
      { pattern: appBase + '**/*.js', included: false, watched: true },

      // asset (HTML & CSS) paths loaded via Angular's component compiler
      // (these paths need to be rewritten, see proxies section)
      { pattern: appBase + '**/*.html', included: false, watched: true },
      { pattern: appBase + '**/*.css', included: false, watched: true },

      // paths for debugging with source maps in dev tools
      { pattern: appBase + '**/*.ts', included: false, watched: false },
      { pattern: appBase + '**/*.js.map', included: false, watched: false }
    ],

    // proxied base paths for loading assets
    proxies: {
      // required for component assets fetched by Angular's compiler
      "/app/": appAssets
    },

    exclude: [],
    preprocessors: {},
    reporters: ['progress', 'html'],

    // HtmlReporter configuration
    htmlReporter: {
      // Open this file to see results in browser
      outputFile: '_test-output/tests.html',

      // Optional
      pageTitle: 'Unit Tests',
      subPageTitle: __dirname
    },

    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome', 'Firefox', 'PhantomJS'],
    singleRun: true
  })
}

1 个答案:

答案 0 :(得分:1)

您也可以查看karma.conf.js以供参考。

更新模块加载files的依赖关系,如下所示

files:[
  // System.js for module loading
  'node_modules/systemjs/dist/system.src.js',

  // Polyfills
  'node_modules/core-js/client/shim.js',
  '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',

  // RxJs
  { pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false },
  { pattern: 'node_modules/rxjs/**/*.js.map', included: false, watched: false },

  // Paths loaded via module imports:
  // Angular itself
  {pattern: 'node_modules/@angular/**/*.js', included: false, watched: false},
  {pattern: 'node_modules/@angular/**/*.js.map', included: false, watched: false},

  ....
]