使用 ts-node vscode 调试 Alsatian 测试用例

Debug Alsatian test cases with vscode using ts-node

我在尝试使用 ts-node 调试 Alsatian 测试用例时遇到了一些问题 建议将不胜感激 - 因为编写测试用例已经慢得像爬行

我正在使用 Alsatian 在打字稿中编写 selenium 测试用例 我已按照此处提供的说明进行操作: debug asaltian with vs code

但它在 ts-node 上崩溃,提示模块 chai 未定义

如果有人可以帮助在 vscode 中逐行完成这些工作和调试,那就太好了

package.json:

{
  "dependencies": {
    "@types/dotenv": "^4.0.2",
    "@types/selenium-webdriver": "^3.0.8",
    "alsatian": "^2.0.0",
    "dotenv": "^4.0.0",
    "selenium-webdriver": "^4.0.0-alpha.1",
    "ts-node": "^4.1.0",
    "tslib": "^1.8.1",
    "typescript": "^2.6.2"
  }
}

runner.ts:

import tapSpec = require('tap-spec');
import { TestSet, TestRunner } from "alsatian";
import { config as dotenv } from 'dotenv';

(async () =>
{
    // Load up any pseudo environment variables
    dotenv({ path: __dirname + '/../.env' });

    // Setup the alsatian test runner
    let testRunner = new TestRunner();
    let tapStream = testRunner.outputStream;
    let testSet = TestSet.create();
    testSet.addTestsFromFiles('/**/*/*.spec.ts');

    // This will output a human readable report to the console.
    tapStream.pipe(tapSpec()).pipe(process.stdout);

    // Runs the tests
    await testRunner.run(testSet);
})()
.catch(e =>
{
    console.error(e);
    process.exit(1);
});

这是旧的 launch.json,我之前尝试连接到运行器,此配置启动但未连接。

alasatian github 上提供的另一个失败,因为它抱怨无法在 ts-node 中解析模块 chai

{
            "name": "ANEX.Website.ManagementPortal.Tests",
            "type": "node",
            "request": "launch",
            "runtimeExecutable": "yarn",
            "runtimeArgs": [
                "run",
                "ts-node",
                "Tests/runner.ts"
            ],
            "cwd": "${workspaceFolder}/ANEX.Website.ManagementPortal.Tests",
            "timeout": 20000,
            "protocol": "inspector",

        }

tldr:我在这个 repo 中创建了一个工作示例:https://github.com/andrefarzat/vscode-alsatian-debug

Alsatian 通过动态加载 javascript(或来自 ts-code 的打字稿)工作,这意味着 vscode 无法跟踪执行,因为没有与之相关的地图文件.

我可以在执行前添加 ts 转译步骤使其工作。

这是我的 launch.json:

{
    "type": "node",
    "request": "launch",
    "name": "Alsatian",
    "preLaunchTask": "tsc: build - tsconfig.json",
    "program": "${workspaceFolder}/node_modules/.bin/alsatian",
    "args": ["./dist/tests/**/*.js"]
}

注意preLaunchTask执行tsc将打字稿代码转译成javascript并放入dist文件夹。我将主要代码放入 src 文件夹,将测试代码放入 tests 文件夹。

这是我的 tsconfig.json:

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "outDir": "./dist/",
        "sourceMap": true,
        "noImplicitAny": true,
        "module": "commonjs",
        "target": "es5",
        "lib": ["es6"]
    },
    "include": [
        "./src/*.ts",
        "./src/**/*.ts",
        "./tests/*.ts",
        "./tests/**/*.ts"
    ],
    "exclude": [
        "./tests/runner.ts"
    ]
}

像这样,ts-node ./tests/runner.ts 仍然有效,您将在 vscode 中拥有 Alsatian 调试命令。不要忘记在本地安装 alsatiants-node

我创建了这个 repo,所以你可以在你的机器上测试:https://github.com/andrefarzat/vscode-alsatian-debug

我认为你原来问题中的方法很好,你只需要添加 "sourcemaps": true, 到启动配置。

这是我的配置,每个 运行 不需要 tsc 构建。您可以将它用于 运行 来自 Alsatian 的正常示例 运行ner,或者在这种情况下,我创建了一个 运行ner,它 运行 是当前打开文件中的测试:

        {
            "name": "Run tests in current file",
            "type": "node",
            "request": "launch",
            "args": ["test/individual_file_runner.ts", "${file}"],
            "runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
            "sourceMaps": true,
            "cwd": "${workspaceRoot}",
            "protocol": "inspector",
        }

和individual_file_runner.ts的代码:

import { TestSet, TestRunner } from "alsatian";

(async () =>
{
    // Setup the alsatian test runner
    let testRunner = new TestRunner();
    let tapStream = testRunner.outputStream;
    let testSet = TestSet.create();
    const fileToTest = process.argv[2];
    console.log(`running tests in file: ${fileToTest}`)
    testSet.addTestsFromFiles(fileToTest);

    // This will output a human readable report to the console.
    tapStream.pipe(process.stdout);

    // Runs the tests
    await testRunner.run(testSet);
})()
.catch(e =>
{
    console.error(e);
    process.exit(1);
});