typescript - 无法在 VSCode 中使用 TypeScript 运行 Mocha 测试

标签 typescript visual-studio-code mocha.js

我的测试文件:

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/

相关文章:

angular - 如何在 Ionic 3 中高效地存储和使用 Authentication

python - 导入错误: DLL load failed while importing lib with importing syft in vscode

reactjs - ESLint 尊重 VS Code jsconfig.json baseUrl 和路径

javascript - Mocha Webcomponents 测试 - ReferenceError : customElements is not defined

javascript - 三括号(即 {{{ stuff }}})在 Javascript/Typescript 中是什么意思?

reactjs - 只有在使用 'IterableIterator<number>' 标志或使用 '--downlevelIteration' 或更高的 '--target' 时,才能迭代类型 'es2015'

javascript - 使用 setInterval 测试功能时,Mocha 和 Chai 测试失败

javascript - 单元测试中安装组件的差异

javascript - 正确编写和导入自定义 Node 模块

visual-studio-code - VS代码: User Settings: Where are the definition of available properties?