【发布时间】:2019-02-20 18:06:25
【问题描述】:
我尝试调试 mocha 测试,但我遇到了问题,我不知道该如何解决。我之前在 google 上搜索过 stackoverflow,但没有任何成功。
错误是:
TSError: ⨯ Unable to compile TypeScript:
source-map-support.js:444 error TS2468: Cannot find global value 'Promise'.backend/test/textToSpeech/lib.ts(11,30): error TS2705: An async
function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option.backend/test/textToSpeech/lib.ts(12,27): error TS2705: An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option.
tsconfig.json 文件如下所示:
{
"compilerOptions": {
"module": "commonjs",
"watch": true,
"noImplicitAny": false,
"removeComments": true,
"outDir": "./dist",
"sourceMap": true,
"target": "es6",
"lib": [
"ES2015"
],
"types": [
"node",
"pg-promise"
],
"typeRoots": [
"node_modules/@types"
]
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
vscode launch.json 配置是 vscode launch.json 配置是
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/backend/node_modules/mocha/bin/_mocha",
"args": [
"--require", "ts-node/register",
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/backend/test/textToSpeech/lib.ts"
],
"internalConsoleOptions": "openOnSessionStart"
}
测试文件:
import {} from 'mocha'
import { expect } from 'chai'
import config from '../configuration'
import { TextToSpeechLib } from '../../src/libs/textToSpeech/'
var textToSpeach = new TextToSpeechLib(config)
var text = 'Hello world'
describe('TextToSpeach lib', async () => {
it ('Convert text ...', async () => {
console.log("==== =a= =s= a==")
let resp = await textToSpeach.convertText(text);
expect(resp.status).to.be.equal('success')
})
})
我尝试了很多东西。就像启动器不加载 tsconfig。我试图在启动器配置中将“--lib”、“'ES2015'”作为 arg 传递。 谢谢。
【问题讨论】:
-
我忘记测试了:
-
我不确定,也许尝试将“ES2015.Promise”添加到tsconfig中的lib数组?
-
没有。不行 。我在谷歌搜索后尝试过这个。看起来 tsconfig 没有加载。我试图弄清楚如何指定 mocha 来加载 tsconfg,但还没有成功。
-
也许您应该指定您只想编译 TypeScript 测试文件?在我的 NPM 包脚本中,我声明了用于测试的特定文件:“test”:“mocha --timeout 10000 --colors --compilers ts:ts-node/register --exit ./tests/**/*. ts --full-trace --recursive",我也认为你可以从描述函数中省略异步
-
我的 tsconfig: { "compilerOptions": { "sourceMap": true, "baseUrl": "src", "experimentalDecorators": true, "target": "es6", "module": " commonjs", "declaration": true, "emitDecoratorMetadata": true, "allowJs": false, "outDir": "lib" } }
标签: typescript debugging visual-studio-code mocha.js