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

发布于 2025-01-17 13:09:25 字数 1226 浏览 4 评论 0原文

我正在实现 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();

以及 package.json 中的命令:

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

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

I'm implementing a VSCode extension. I set up the project following this link.

It generates a starter project with a src/test/runTest.ts file:

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();

And a command in the package.json:

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

Is there a way to generate a coverage report with it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娜些时光,永不杰束 2025-01-24 13:09:25

VSCODE扩展Unitesting使用 mocha> mocha 在引擎盖下。您可以使用许多可用框架之一,例如 c8 jest istanbul 等。

安装您选择的框架,在这里我使用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"
  }

并根据您的扩​​展名 检查覆盖范围。在这里,我们正在检查.js文件放置在out/ dir下的文件,我们还排除了负责UniteSting IE OUT/TEST/TEST/的文件(通常)。

.c8rc

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

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

npm run coverage

“在此处输入图像说明”

使用上述工作回购: https://github.com/fortran-lang/vscode-fortran-support

VSCode extension unittesting uses Mocha under the hood. You can generate coverage reports like in any other Typescript/Javascript project using one of the many available frameworks, e.g. c8, jest, istanbul, etc.

Install the framework of your choice, here I use c8

npm i --save-dev c8

and add to scripts

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

Depending on your extension you might need to create a configuration file with the files you want to check for coverage. Here we are checking the compiled .js files placed under the out/ dir and we also excluding the files responsible for unittesting i.e. out/test/ (usually).

.c8rc

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

Run the coverage script and you should get an output of your coverage

npm run coverage

enter image description here

A working repo making use of the above: https://github.com/fortran-lang/vscode-fortran-support

慵挽 2025-01-24 13:09:25

如果您使用 vscode-test-cli 现在,它是创建一个默认值通过yo Code的新扩展,这很容易。

安装@vscode/test-cli的依赖项,如果尚未在项目中。

npm install --save-dev @vscode/test-cli

然后,您可以使用以下命令生成覆盖范围。

vscode-test --coverage

注意:您的版本至少应为0.0.6。

您还可以使用- Coverage-Output(默认: os 的温度)和- coverage-reporter(任何可能的Mocha Reporters)。

这意味着,如果您想使用文本和HTML记者在覆盖范围目录中生成覆盖范围,则可以使用以下内容。

vscode-test --coverage --coverage-output ./coverage --coverage-reporter html --coverage-reporter text"

@vscode/test-CLI版本0.0.4

  1. 安装C8
npm install --save-dev c8
  1. 旧解决方案添加覆盖脚本添加到package.json
"coverage": "c8 npm run test"
  1. 通过npm Run Coverage <: /代码>。

就我而言,不需要其他配置。

If you are using vscode-test-cli, which is now the default when creating a new extension via yo code, it is very easy.

Install the dependency of @vscode/test-cli, if not already in the project.

npm install --save-dev @vscode/test-cli

Then you can generate the coverage with the following command.

vscode-test --coverage

Note: Your version should be at least 0.0.6.

You can also set the output directory of the coverage with --coverage-output (default: temp dir of OS) and --coverage-reporter (any mocha reporters possible).

That means if you want to generate the coverage in your coverage directory with text and html reporter, you can use the following.

vscode-test --coverage --coverage-output ./coverage --coverage-reporter html --coverage-reporter text"

old solution with @vscode/test-cli version 0.0.4

  1. Install c8
npm install --save-dev c8
  1. Add a coverage script to your package.json:
"coverage": "c8 npm run test"
  1. Run the coverage via npm run coverage.

In my case, additional configuration was not needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文