[英]SyntaxError Importing Mocha for Typescript Unit Testing
下面我有一個 Typescript 項目的示例測試。 我正在使用 mocha chai 進行測試。 測試用例暫時是空白的。
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 行更改為導入“mocha”; 我有一個類似的 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"
}
}
解決! package.json
中的test
腳本應更改為:
"test": "mocha --require ts-node/register --watch-extensions ts './src/test/test.ts'",
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.