从TypeScript进行mocha测试调试的问题 [英] Issue with mocha test debugging from typescript
本文介绍了从TypeScript进行mocha测试调试的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图调试一个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传递。 谢谢您。
推荐答案
更新:此答案已过时。现在可能有更好的方法来解决这个问题。我最近没有测试过它。
错误消息清楚地告诉您应该执行什么操作:
确保您有‘Promise’构造函数的声明或在--lib选项中包含‘ES2015’
由于我们绝对不想提供自己的定义,因此我们应该继续并将"ES2015"包括在选项中。
如何操作?
所有的ts节点sff都非常复杂。有经验的人大概能说出具体的tsconfig
怎么点,我就说不出来。我建议设置一个环境变量来实现此目标。
我翻了查源代码,发现必要的env var是TS_NODE_COMPILER_OPTIONS
,意思是在调用ts-node时,process.env
必须有属性:
TS_NODE_COMPILER_OPTIONS={"lib": ["ES2015"], ...}
以及如何做到这一点?
使用VS代码进行调试时,您可以通过将以下内容添加到配置中来实现此目标:
"env": {"TS_NODE_COMPILER_OPTIONS":"{"lib": ["ES2015"]}"}
如果要运行测试(例如使用npm test
),可以在使用cross-env运行测试脚本之前将此选项添加到环境变量。
我最终在我的package.json
:
"scripts": {
"test": "cross-env TS_NODE_COMPILER_OPTIONS="{\"lib\": [\"ES2015\"]}" mocha --require ts-node/register path/to/test(s).ts"
}
注意:-三重反斜杠是故意的
这篇关于从TypeScript进行mocha测试调试的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文