我的测试文件:
import { expect } from 'chai';
describe('test', () => {
it('compiles', () => {
expect(true).is.true;
});
});
运行此 npm 脚本有效:
"test": "mocha -r esm -r ts-node/register test/**/Test*.t.ts"
它应该如何工作的示例:https://adrianhall.github.io/web/2018/07/04/run-typescript-mocha-tests-in-vscode/
在该示例中,他们也在测试文件中使用 import { .. } ,但对我来说,只有当我需要 esm 时才有效(第一个谜团,但本身不是问题)。
当我在 VSCode 中运行此配置(来自示例)时,它在我之前“npm run test”时可以工作,但是当我更改测试代码并运行它时,它会在 TS 类型冒号上崩溃。如果没有“-r esm”, Mocha 会再次在“导入{”时崩溃
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--require", "esm",
"--require", "ts-node/register",
"--timeout", "999999",
"--colors",
"${workspaceFolder}/test/**/Test*.ts",
],
"internalConsoleOptions": "openOnSessionStart"
}
最佳答案
我让它与 ts-mocha 一起工作。
package.json 脚本
"test": "ts-mocha -r esm -p tsconfig.json test/**/Test*.ts"
launch.json 配置
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"runtimeArgs": [
"${workspaceFolder}/node_modules/ts-mocha/bin/ts-mocha",
"--timeout", "999999",
"-r", "esm",
"-p", "${workspaceFolder}/tsconfig.json", "${workspaceFolder}/test/**/Test*.ts",
],
"console": "integratedTerminal",
"protocol": "inspector"
}
关于typescript - 无法在 VSCode 中使用 TypeScript 运行 Mocha 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61889215/