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

时间:2018-01-28 23:38:45

标签: node.js selenium visual-studio-code ts-node

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

我正在使用阿尔萨斯语在打字稿中编写硒测试用例 我按照这里提供的说明操作: debug asaltian with vs code

但它在ts节点上崩溃说模块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上提供的另一个失败,因为它抱怨模块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",

        }

2 个答案:

答案 0 :(得分:1)

tldr :我在此回购邮件中创建了一个工作示例: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 debug命令。不要忘记在本地安装alsatiants-node

我创建了此回购,因此您可以在自己的计算机上进行测试:https://github.com/andrefarzat/vscode-alsatian-debug

答案 1 :(得分:0)

我认为您原始问题中的方法很好,您只需要在启动配置中添加“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);
});