执行 mocha 测试时,出现错误 "reflect-metadata shim is required when using class decorators"

While executing mocha tests, Getting error "reflect-metadata shim is required when using class decorators"

在Typescript中用chai编写了一个mocha测试用例,并按照文章http://brianflove.com/2016/11/11/typescript-2-express-mongoose-mocha-chai/安装了所有依赖项。

虽然我 运行 测试用例 "npm test",但我遇到了以下错误

C:\J\MEAN\MSDN-App\node_modules\@angular\core\bundles\core.umd.js:335 throw 'reflect-metadata shim is required when using class decorators'; ^ reflect-metadata shim is required when using class decorators npm ERR! Test failed. See above for more details.

package.json是:

{
  "name": "test-app",
  "version": "1.0.0",
  "description": "test description",
  "main": "server.js",
  "scripts": {
    "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
    "test": "mocha -r ts-node/register test/**/*.ts",
    "lite": "lite-server",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings"
  },
  "author": "",
  "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.4.10",
    "@angular/upgrade": "^2.0.1",
    "@covalent/core": "^1.0.0-beta.3-2",
    "@ngrx/store": "^2.0.1",
    "@types/node": "7.0.7",
    "@types/underscore": "^1.8.0",
    "angular-in-memory-web-api": "^0.1.1",
    "body-parser": "^1.17.1",
    "bootstrap": "^3.3.7",
    "core-js": "^2.4.1",
    "ejs": "^2.5.6",
    "express": "^4.15.2",
    "mongojs": "^2.4.0",
    "node-sspi": "^0.1.14",
    "nodemailer": "^4.0.1",
    "nodemailer-smtp-transport": "^2.7.4",
    "properties-reader": "0.0.15",
    "reflect-metadata": "^0.1.10",
    "rxjs": "5.0.0-beta.12",
    "systemjs": "0.19.39",
    "ts-node": "^3.0.4",
    "underscore": "^1.8.3",
    "zone.js": "^0.6.25"
  },
  "devDependencies": {
    "@types/chai": "^3.5.2",
    "@types/core-js": "0.9.35",
    "@types/mocha": "^2.2.41",
    "chai": "^3.5.0",
    "concurrently": "^3.0.0",
    "lite-server": "^2.2.2",
    "mocha": "^3.4.1",
    "reflect-metadata": "^0.1.10",
    "ts-node": "^3.0.4",
    "typescript": "^2.0.3",
    "typings": "^1.4.0"
  }
}

而tsconfig.json是:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
 "exclude": [
  "node_modules",
  "node_modules/@types",
  "bower_components",
  "typings"
 ]
}

请帮忙解决错误。

答案很简单,但很难找到。不应在 test.ts 文件中包含 @angular/http 并测试服务使用单元测试,要测试 angular 组件,我们必须使用 Angular 测试实用程序进行测试。