如何使用 vscode 扩展测试生成覆盖率报告

Val*_*nal 4 testing test-coverage visual-studio-code vscode-extensions

我正在实现 VSCode 扩展。我按照此链接设置了该项目。

它生成一个带有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();
Run Code Online (Sandbox Code Playgroud)

和一个命令package.json

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();
Run Code Online (Sandbox Code Playgroud)

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

nik*_*ohn 5

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

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

npm i --save-dev c8
Run Code Online (Sandbox Code Playgroud)

并添加到脚本中

  "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"
  }
Run Code Online (Sandbox Code Playgroud)

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

.c8rc

{
  "all": true,
  "include": ["out/**"],
  "exclude": ["**/node_modules/**", "out/test/"],
  "reporter": ["html", "text"]
}
Run Code Online (Sandbox Code Playgroud)

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

npm run coverage
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

使用上述内容的工作存储库:https ://github.com/fortran-lang/vscode-fortran-support