SyntaxError导入Mocha以进行打字稿单元测试

时间:2020-07-14 17:42:41

标签: node.js typescript mocha chai

下面我有一个Typescript项目的示例测试。我正在使用摩卡柴进行测试。该测试用例目前为空。

import {KafkaConsumer} from '../infrastructure/delivery/kafka/kafka-consumer';
import {expect} from 'chai';
import {describe, it} from 'mocha';

describe('KafkaConsumer Initialization', () => {
    it('should initialize KafkaConsumer', () => {
    })
})

运行测试时,出现以下错误:

 import {describe} from 'mocha';
       ^

SyntaxError: Unexpected token {

有人知道为什么会这样吗?如果我将第3行更改为导入“摩卡”;我有一个类似的SyntaxError。如果没有该行,则describe()it()未定义

下面是我的package.json文件。

    {
  "name": "wss",
  "version": "0.0.0",
  "private": true,
  "main": "dist/index.js",
  "scripts": {
    "tsc": "tsc",
    "start": "node --inspect=5858 -r ts-node/register ./src/index.ts",
    "start:watch": "nodemon",
    "build": "tsc",
    "prod": "tsc && npm run postbuild && node ./dist/index.js",
    "postbuild": "npm run copy-files",
    "copy-files": "copyfiles -u 1 ./src/**/*.js dist",
    "test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register 'tests/**/*.ts'",
    "test:watch": "nodemon --exec npm test"
  },
  "nyc": {
    "require": [
      "ts-node/register"
    ],
    "include": [
      "src/**/*.ts"
    ],
    "extension": [
      ".ts"
    ],
    "reporter": [
      "lcov",
      "text"
    ],
    "sourceMap": true,
    "instrument": true
  },
  "dependencies": {
    "@types/dotenv": "^6.1.1",
    "@types/newrelic": "^5.11.0",
    "@types/redis": "^2.8.21",
    "aws-iot-device-sdk": "^2.2.4",
    "axios": "^0.19.0",
    "bluebird": "^3.5.5",
    "chai-http": "^4.3.0",
    "cookie-parser": "~1.4.3",
    "copyfiles": "^2.2.0",
    "dotenv": "^7.0.0",
    "express": "~4.16.0",
    "kafka-node": "^4.1.3",
    "lodash": "^4.17.15",
    "morgan": "~1.9.0",
    "newrelic": "^5.11.0",
    "redis": "^3.0.2",
    "reflect-metadata": "^0.1.13",
    "uuid": "^8.0.0"
  },
  "devDependencies": {
    "@types/bluebird": "^3.5.27",
    "@types/express": "^4.16.1",
    "@types/jsonwebtoken": "^8.3.2",
    "@types/lodash": "^4.14.150",
    "@types/mocha": "^8.0.0",
    "@types/node": "^11.13.14",
    "@types/winston": "^2.4.4",
    "chai": "^4.2.0",
    "eslint": "^4.19.1",
    "mocha": "^8.0.1",
    "nodemon": "^1.18.11",
    "nyc": "^14.1.1",
    "ts-node": "^8.10.2",
    "ts-node-dev": "^1.0.0-pre.41",
    "tslint": "^5.16.0",
    "typescript": "^3.9.2",
    "typescript-eslint-parser": "^22.0.0"
  }
}

1 个答案:

答案 0 :(得分:0)

已解决! test中的package.json脚本应更改为:

    "test": "mocha --require ts-node/register --watch-extensions ts './src/test/test.ts'",