【问题标题】:How to generate a coverage report with vscode extension tests如何使用 vscode 扩展测试生成覆盖率报告
【发布时间】:2022-08-19 01:31:22
【问题描述】:

我正在实现一个 VSCode 扩展。我在this link 之后设置了项目。

它会生成一个带有src/test/runTest.ts 文件的启动项目:

import * as path from \'path\';

import { runTests } from \'@vscode/test-electron\';

async function main() {
    try {
        // The folder containing the Extension Manifest package.json
        // Passed to `--extensionDevelopmentPath`
        const extensionDevelopmentPath = path.resolve(__dirname, \'../../\');

        // The path to test runner
        // Passed to --extensionTestsPath
        const extensionTestsPath = path.resolve(__dirname, \'./suite/index\');

        // Download VS Code, unzip it and run the integration test
        await runTests({ extensionDevelopmentPath, extensionTestsPath });
    } catch (err) {
        console.error(\'Failed to run tests\');
        process.exit(1);
    }
}

main();

以及package.json 中的一个命令:

{
    \"compile\": \"tsc -p ./\",
    \"pretest\": \"npm run compile && npm run lint\",
    \"lint\": \"eslint src --ext ts\",
    \"test\": \"node ./out/test/runTest.js\"
}

有没有办法用它生成覆盖率报告?

  • 这可能是一个调试器功能,可以为它到达的每一行注入对line X is executing 的调用,将事件处理程序附加到此,或订阅事件,由测试运行程序完成

标签: testing visual-studio-code vscode-extensions test-coverage


【解决方案1】:

VSCode 扩展单元测试在后台使用Mocha。您可以使用许多可用框架之一生成覆盖率报告,就像在任何其他 Typescript/Javascript 项目中一样,例如c8jestistanbul

安装你选择的框架,这里我用c8

npm i --save-dev c8

并添加到脚本

  "scripts": {
    "compile": "tsc -p ./",
    "pretest": "npm run compile && npm run lint",
    "lint": "eslint src --ext ts",
    "test": "node ./out/test/runTest.js",
    "coverage": "c8 --check-coverage npm run test"
  }

根据您的扩​​展,您可能需要使用要检查覆盖范围的文件创建配置文件。在这里,我们正在检查位于 out/ 目录下的已编译 .js 文件,并且我们还排除了负责单元测试的文件,即 out/test/(通常)。

.c8rc

{
  "all": true,
  "include": ["out/**"],
  "exclude": ["**/node_modules/**", "out/test/"],
  "reports": ["html", "text"]
}

运行coverage 脚本,您应该得到覆盖范围的输出

npm run coverage

【讨论】:

    猜你喜欢
    • 2019-10-11
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 2017-09-03
    • 2018-11-14
    相关资源
    最近更新 更多