Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] deno test --coverage should output transpiled code and sourcemap #23359

Open
cenfun opened this issue Apr 14, 2024 · 0 comments
Labels
feat new feature (which has been agreed to/accepted) testing related to deno test and coverage

Comments

@cenfun
Copy link

cenfun commented Apr 14, 2024

Deno currently supports collecting raw V8 coverage data through the --coverage flag, which is fantastic.

As a result, we have the opportunity to use third-party tools to directly convert from raw V8 coverage data and generate various other coverage reports, not just the lcov and simple html provided by the official.

  • With raw V8 coverage data, more types of common coverage reports, such as V8 coverage reports, can be generated directly, and custom coverage reports are also supported.
  • With raw V8 coverage data, it can be merged with other V8 coverage reports, such as e2e test (Playwright/Puppeteer) coverage, and even Node.js (Jest V8 provider) or Istanbul (v8-to-istanbul) coverage.

The problem

However, Deno's V8 coverage data does not include transpiled code and sourcemap, or we can not get the transpiled code and sourcemap for these V8 coverage data.

  • The startOffset and endOffset in the coverage data are matched to the position of the transpiled code. Without the transpiled code, the position cannot be matched.
  • Without a sourcemap, the position of the original code cannot be found.

For example, after providing the transpiled code and sourcemap, the coverage json could be following

{
  "scriptId": "310",
  "url": "file:///E:/workspace/deno-coverage-reports/src/main.ts",
  "source": "<the transpiled source code>",
  "sourceMap": "<the sourcemap for the transpiled source code>",
  "functions": [
    ...
  ]
}

Expectation

In fact, the transpiled code and sourcemap should be separated because multiple coverage data may exist for the same file. For example, we expect to get the coverage files like following:

- coverage // coverage dir
  - coverage-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.json // coverage data 1 for source file 1
  - coverage-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.json // coverage data 2 for source file 1
  - coverage-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.json // coverage data 3 for source file 2
  - source-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.json // source file 1, include transpiled code and sourcemap 
  - source-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.json // source file 2, include transpiled code and sourcemap 

Attempt

I tried to use the tool MCR to generate native V8 coverage reports from Deno's coverage data, but due to the above problem, it was unsuccessful. For details, refer to the repo:
https://github.com/cenfun/deno-coverage-reports

// coverage-report.js
import MCR from "npm:monocart-coverage-reports@latest";
import { fileURLToPath } from "node:url";

const mcr = MCR({
  //logging: "debug",
  name: "My Deno Coverage Report",
  outputDir: "./coverage-reports/",
  reports: ["v8", "console-details"],
});

const coverageList = [];
const coverageDataDir = "coverage";
for (const dirEntry of Deno.readDirSync(coverageDataDir)) {
  const data = Deno.readTextFileSync(`${coverageDataDir}/${dirEntry.name}`);
  const fileCoverage = JSON.parse(data);
  if (fileCoverage.url.startsWith("file://")) {
    const source = Deno.readTextFileSync(fileURLToPath(fileCoverage.url));
    // NOTE: It requires transpiled source code and sourcemap. Unfortunately, Deno doesn't provide.
    // The source is original code not transpiled code.
    // Because of the offset in coverage is mapping to transpiled code. So, the source is wrong here.
    fileCoverage.source = source;
    coverageList.push(fileCoverage);
  }
}

await mcr.add(coverageList);
await mcr.generate();

CLI

deno run --allow-env --allow-read --allow-sys --allow-write tasks/coverage-report.js

Related issues

#21325
#18147
#21582
#20007
#16626

@lucacasonato lucacasonato added feat new feature (which has been agreed to/accepted) testing related to deno test and coverage labels Jun 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat new feature (which has been agreed to/accepted) testing related to deno test and coverage
Projects
None yet
Development

No branches or pull requests

2 participants