3

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?

Valentin Vignal
  • 6,151
  • 2
  • 33
  • 73
  • that is probably a debugger feature to inject calls to `line X is executing` for each and every line it reaches, attach an event handler to this, or subscribe to the event, is done by the test runner – rioV8 Mar 29 '22 at 16:08

1 Answers1

5

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

gnikit
  • 1,031
  • 15
  • 25
  • I have tried this but it gives error in windoes 11 ```[main 2023-02-02T15:18:05.015Z] UtilityProcess<1>: Waiting for extension host with pid 13300 to exit. [main 2023-02-02T15:18:05.124Z] UtilityProcess<1>: received exit event with code 0. [main 2023-02-02T15:18:05.124Z] Extension host with pid 13300 exited with code: 0, signal: . Exit code: 0 Done ``` – xkeshav Feb 02 '23 at 15:21
  • The error message alone does not provide enough info as to what's going on. A guess would that when you run with coverage you are hitting the default time limit set in Mocha. Have a look at a working example and maybe try and create something similar: https://github.com/fortran-lang/vscode-fortran-support – gnikit Mar 11 '23 at 14:51
  • I want this to run continuously in VS Code like a linter, and in the line number section of the currently open file, any lines that aren't covered would be highlighted red. Is there an extension for this? – Boris Verkhovskiy Apr 11 '23 at 10:20
  • Figured it out using [this extension](https://marketplace.visualstudio.com/items?itemName=markis.code-coverage) and [this config](https://github.com/bcoe/c8/issues/71#issuecomment-504710008) (I had to change `"report-dir": "./.coverage",` to `"report-dir": "./coverage",` to remove the leading dot) – Boris Verkhovskiy Apr 11 '23 at 10:45