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

问题描述 投票:1回答:1

我在尝试使用ts节点建议调试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节点中解析

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

        }
node.js selenium visual-studio-code ts-node
1个回答
1
投票

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

阿尔萨斯通过动态加载javascript(或来自ts-code的typescript)来工作,这意味着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"]
}

注意执行preLaunchTasktsc将typescript代码转换为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

© www.soinside.com 2019 - 2024. All rights reserved.