将角度2升级到4.4后,Karma测试未运行

时间:2017-11-30 21:42:26

标签: angular karma-runner karma-jasmine angular-cli

我已按照此处的步骤将我的应用程序以角度2升级为角度4.4:

https://angular-update-guide.firebaseapp.com/

应用程序运行良好,但是当我使用ng test在karma中运行单元测试时。我收到以下错误:

Chrome 62.0.3202 (Windows 10 0.0.0) ERROR
  Uncaught SyntaxError: Unexpected token import
  at lib/test.ts:3

我的package.json:

{
  "name": "settings-editor",
  "version": "0.0.22",
  "license": "MIT",
  "main": "lib/index.ts",
  "typings": "lib/index.ts",
  "scripts": {
    "ng": "ng",
    "start": "ng serve  --port 4201",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },  
  "dependencies": {
    "@angular/common": "^4.4.6",
    "@angular/compiler": "^4.4.6",
    "@angular/core": "^4.4.6",
    "@angular/forms": "^4.4.6",
    "@angular/http": "^4.4.6",
    "@angular/platform-browser": "^4.4.6",
    "@angular/platform-browser-dynamic": "^4.4.6",
    "@angular/router": "^4.4.6",
    "@ng-bootstrap/ng-bootstrap": "1.0.0-alpha.24",
    "@types/lodash": "^4.14.85",
    "@types/moment": "^2.13.0",
    "@types/moment-timezone": "^0.2.34",
    "core-js": "^2.5.1",
    "lodash": "^4.17.4",
    "moment": "^2.19.2",
    "moment-timezone": "^0.5.11",
    "ng2-validation": "^4.1.0",
    "rxjs": "^5.1.0",
    "zone.js": "^0.8.18"
  },
  "devDependencies": {
    "@angular/cli": "1.5.3",
    "@angular/compiler-cli": "^4.4.6",
    "@types/jasmine": "2.8.2",
    "@types/node": "^8.0.53",
    "codelyzer": "^4.0.1",
    "font-awesome": "^4.7.0",
    "jasmine-core": "^2.8.0",
    "jasmine-spec-reporter": "^4.2.1",
    "karma": "^1.7.1",
    "karma-chrome-launcher": "^2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.3.0",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.0",
    "ts-node": "^3.2.2",
    "tslint": "^5.8.0",
    "typescript": "2.3.4"
  }  
}

控制台lib / test.ts中显示错误的文件:3

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';

// Unfortunately there's no typing for the `__karma__` variable. Just declare it as any.
declare var __karma__: any;
declare var require: any;

// Prevent Karma from running prematurely.
__karma__.loaded = function () {};

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
// Finally, start Karma to run the tests.
__karma__.start();

.angular-cli.json:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "settings-editor"
  },
  "apps": [
    {
      "root": "demo-app",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "demo",
      "styles": [
        "styles.less",
        "../node_modules/font-awesome/css/font-awesome.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "demo-app/tsconfig.app.json"
    },
    {
      "project": "demo-app/tsconfig.spec.json"
    },
    {
      "project": "e2e/tsconfig.e2e.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "less",
    "component": {}
  }
}

我该如何解决这个问题?

0 个答案:

没有答案