1

我在尝试使用 ts-node 建议调试 Alsatian 测试用例时遇到了一些麻烦 - 因为编写测试用例已经慢到爬行

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

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

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

包.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"
  }
}

亚军.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 上提供的另一个失败,因为它抱怨模块 chai 无法在 ts-node 中解析

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

        }
4

2 回答 2

2

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

Alsatian 通过ts-code动态加载 javascript(或 typescript from )来工作,这意味着 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仍然有效,您将Alsatian在 vscode 中使用调试命令。不要忘记在alsatian本地ts-node安装。

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

于 2018-03-03T22:59:49.207 回答
1

我认为您最初问题中的方法很好,您只需要在启动配置中添加“sourcemaps”:true。

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

        {
            "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);
});
于 2021-07-01T22:31:12.550 回答