【问题标题】:Can't run Mocha tests with TypeScript in VSCode无法在 VSCode 中使用 TypeScript 运行 Mocha 测试
【发布时间】:2020-09-05 09:50:04
【问题描述】:

我的测试文件:

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 在'import {' 上再次崩溃

{
    "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"
}

【问题讨论】:

    标签: typescript visual-studio-code mocha.js


    【解决方案1】:

    我让它与 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"
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-24
      • 2023-03-23
      • 2020-09-03
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      • 1970-01-01
      • 2019-12-03
      相关资源
      最近更新 更多