在执行mocha测试时,使用类装饰器时需要获取错误" reflect-metadata shim"

时间:2017-05-23 10:36:42

标签: node.js angular typescript mocha chai

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

当我使用" npm test"运行测试用例时,我收到以下错误

  

C:\ J□\ MEAN \ MSDN应用内\ node_modules \ @angular \芯\束\ core.umd.js:335                   使用类装饰器时,需要使用throw' reflect-metadata shim;&#39 ;;                   ^   使用类装饰器时需要reflect-metadata shim   错误的ERR!测试失败。有关详细信息,请参见上文。

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"
	]
}




请帮助解决错误。

1 个答案:

答案 0 :(得分:0)

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