| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent dc2a4af commit 0381817
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,14 +36,14 @@ const kLineSplitRegex = /(?<=\r?\n)/u; | |||
| 36 | 36 | const kStatusRegex = /\/\* node:coverage (?<status>enable|disable) \*\//; | |
| 37 | 37 | ||
| 38 | 38 | class CoverageLine { | |
| 39 | - constructor(line, src, startOffset) { | ||
| 40 | - const newlineLength = | ||
| 39 | + constructor(line, startOffset, src, length = src?.length) { | ||
| 40 | + const newlineLength = src == null ? 0 : | ||
| 41 | 41 | RegExpPrototypeExec(kLineEndingRegex, src)?.[0].length ?? 0; | |
| 42 | 42 | ||
| 43 | 43 | this.line = line; | |
| 44 | 44 | this.src = src; | |
| 45 | 45 | this.startOffset = startOffset; | |
| 46 | - this.endOffset = startOffset + src.length - newlineLength; | ||
| 46 | + this.endOffset = startOffset + length - newlineLength; | ||
| 47 | 47 | this.ignore = false; | |
| 48 | 48 | this.count = this.startOffset === this.endOffset ? 1 : 0; | |
| 49 | 49 | } | |
@@ -83,7 +83,7 @@ class TestCoverage { | |||
| 83 | 83 | ||
| 84 | 84 | const lines = ArrayPrototypeMap(linesWithBreaks, (line, i) => { | |
| 85 | 85 | const startOffset = offset; | |
| 86 | - const coverageLine = new CoverageLine(i + 1, line, startOffset); | ||
| 86 | + const coverageLine = new CoverageLine(i + 1, startOffset, line); | ||
| 87 | 87 | ||
| 88 | 88 | offset += line.length; | |
| 89 | 89 | ||
@@ -335,8 +335,13 @@ class TestCoverage { | |||
| 335 | 335 | newResult.set(url, script); | |
| 336 | 336 | continue; | |
| 337 | 337 | } | |
| 338 | - const originalLines = this.getLines(url); | ||
| 339 | 338 | const { data, lineLengths } = sourceMapCache[url]; | |
| 339 | + let offset = 0; | ||
| 340 | + const executedLines = ArrayPrototypeMap(lineLengths, (length, i) => { | ||
| 341 | + const coverageLine = new CoverageLine(i + 1, offset, null, length); | ||
| 342 | + offset += length; | ||
| 343 | + return coverageLine; | ||
| 344 | + }); | ||
| 340 | 345 | if (data.sourcesContent != null) { | |
| 341 | 346 | for (let j = 0; j < data.sources.length; ++j) { | |
| 342 | 347 | this.getLines(data.sources[j], data.sourcesContent[j]); | |
@@ -353,7 +358,7 @@ class TestCoverage { | |||
| 353 | 358 | const newRanges = []; | |
| 354 | 359 | for (let k = 0; k < ranges.length; ++k) { | |
| 355 | 360 | const { startOffset, endOffset, count } = ranges[k]; | |
| 356 | - const { lines } = mapRangeToLines(ranges[k], originalLines); | ||
| 361 | + const { lines } = mapRangeToLines(ranges[k], executedLines); | ||
| 357 | 362 | ||
| 358 | 363 | let startEntry = sourceMap | |
| 359 | 364 | .findEntry(lines[0].line - 1, MathMax(0, startOffset - lines[0].startOffset)); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,11 +1,25 @@ | |||
| 1 | - const source = ` | ||
| 1 | + const sources = { | ||
| 2 | + // Virtual file. Dosen't exist on disk | ||
| 3 | + "virtual.js": ` | ||
| 2 | 4 | import { test } from 'node:test'; | |
| 3 | 5 | test('test', async () => {}); | |
| 4 | - `; | ||
| 6 | + `, | ||
| 7 | + // file with source map. this emulates the behavior of tsx | ||
| 8 | + "sum.test.ts": `\ | ||
| 9 | + import{describe,it}from"node:test";import assert from"node:assert";import{sum}from"./sum.ts";describe("sum",()=>{it("should sum two numbers",()=>{assert.deepStrictEqual(sum(1,2),3)});it("should error out if one is not a number",()=>{assert.throws(()=>sum(1,"b"),Error)})}); | ||
| 10 | + | ||
| 11 | + //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJtYXBwaW5ncyI6IkFBQUEsT0FBUyxTQUFVLE9BQVUsWUFDN0IsT0FBTyxXQUFZLGNBQ25CLE9BQVMsUUFBVyxRQUVwQixTQUFTLE1BQU8sSUFBTSxDQUNwQixHQUFHLHlCQUEwQixJQUFNLENBQy9CLE9BQU8sZ0JBQWdCLElBQUksRUFBRSxDQUFDLEVBQUcsQ0FBQyxDQUN0QyxDQUFDLEVBRUQsR0FBRywwQ0FBMkMsSUFBTSxDQUNsRCxPQUFPLE9BQU8sSUFBTSxJQUFJLEVBQUcsR0FBRyxFQUFHLEtBQUssQ0FDeEMsQ0FBQyxDQUNILENBQUMiLCJuYW1lcyI6W10sImlnbm9yZUxpc3QiOltdLCJzb3VyY2VzIjpbIi4vc3VtLnRlc3QudHMiXSwic291cmNlc0NvbnRlbnQiOltudWxsXX0=`, | ||
| 12 | + // file with source map. this emulates the behavior of tsx | ||
| 13 | + "sum.ts": `\ | ||
| 14 | + var __defProp=Object.defineProperty;var __name=(target,value)=>__defProp(target,"name",{value,configurable:true});function sum(...n){if(!n.every(num=>typeof num==="number"))throw new Error("Not a number");return n.reduce((acc,cur)=>acc+cur)}__name(sum,"sum");export{sum}; | ||
| 15 | + | ||
| 16 | + //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJtYXBwaW5ncyI6ImtIQUFPLFNBQVMsT0FBUSxFQUFHLENBQ3pCLEdBQUksQ0FBQyxFQUFFLE1BQU8sS0FBUSxPQUFPLE1BQVEsUUFBUSxFQUFHLE1BQU0sSUFBSSxNQUFNLGNBQWMsRUFDOUUsT0FBTyxFQUFFLE9BQU8sQ0FBQyxJQUFLLE1BQVEsSUFBTSxHQUFHLENBQ3pDLENBSGdCIiwibmFtZXMiOltdLCJpZ25vcmVMaXN0IjpbXSwic291cmNlcyI6WyIuL3N1bS50cyJdLCJzb3VyY2VzQ29udGVudCI6W251bGxdfQ==`, | ||
| 17 | + }; | ||
| 5 | 18 | ||
| 6 | 19 | export async function load(url, context, nextLoad) { | |
| 7 | - if (url.endsWith('virtual.js')) { | ||
| 8 | - return { format: "module", source, shortCircuit: true }; | ||
| 20 | + const file = url.split('/').at(-1); | ||
| 21 | + if (sources[file] !== undefined) { | ||
| 22 | + return { format: "module", source: sources[file], shortCircuit: true }; | ||
| 9 | 23 | } | |
| 10 | 24 | return nextLoad(url, context); | |
| 11 | 25 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,13 @@ | |||
| 1 | + import { describe, it } from 'node:test' | ||
| 2 | + import assert from 'node:assert' | ||
| 3 | + import { sum } from './sum' | ||
| 4 | + | ||
| 5 | + describe('sum', () => { | ||
| 6 | + it('should sum two numbers', () => { | ||
| 7 | + assert.deepStrictEqual(sum(1, 2), 3) | ||
| 8 | + }) | ||
| 9 | + | ||
| 10 | + it('should error out if one is not a number', () => { | ||
| 11 | + assert.throws(() => sum(1, 'b'), Error) | ||
| 12 | + }) | ||
| 13 | + }) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,4 @@ | |||
| 1 | + export function sum (...n) { | ||
| 2 | + if (!n.every((num) => typeof num === 'number')) throw new Error('Not a number') | ||
| 3 | + return n.reduce((acc, cur) => acc + cur) | ||
| 4 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -303,3 +303,35 @@ test('coverage with ESM hook - source irrelevant', skipIfNoInspector, () => { | |||
| 303 | 303 | assert(result.stdout.toString().includes(report)); | |
| 304 | 304 | assert.strictEqual(result.status, 0); | |
| 305 | 305 | }); | |
| 306 | + | ||
| 307 | + test('coverage with ESM hook - source transpiled', skipIfNoInspector, () => { | ||
| 308 | + let report = [ | ||
| 309 | + '# start of coverage report', | ||
| 310 | + '# ------------------------------------------------------------------', | ||
| 311 | + '# file | line % | branch % | funcs % | uncovered lines', | ||
| 312 | + '# ------------------------------------------------------------------', | ||
| 313 | + '# hooks.mjs | 100.00 | 100.00 | 100.00 | ', | ||
| 314 | + '# register-hooks.js | 100.00 | 100.00 | 100.00 | ', | ||
| 315 | + '# sum.test.ts | 100.00 | 100.00 | 100.00 | ', | ||
| 316 | + '# sum.ts | 100.00 | 100.00 | 100.00 | ', | ||
| 317 | + '# ------------------------------------------------------------------', | ||
| 318 | + '# all files | 100.00 | 100.00 | 100.00 |', | ||
| 319 | + '# ------------------------------------------------------------------', | ||
| 320 | + '# end of coverage report', | ||
| 321 | + ].join('\n'); | ||
| 322 | + | ||
| 323 | + if (common.isWindows) { | ||
| 324 | + report = report.replaceAll('/', '\\'); | ||
| 325 | + } | ||
| 326 | + | ||
| 327 | + const fixture = fixtures.path('test-runner', 'coverage-loader'); | ||
| 328 | + const args = [ | ||
| 329 | + '--import', './register-hooks.js', '--test', '--experimental-test-coverage', | ||
| 330 | + '--test-reporter', 'tap', 'sum.test.ts', | ||
| 331 | + ]; | ||
| 332 | + const result = spawnSync(process.execPath, args, { cwd: fixture }); | ||
| 333 | + | ||
| 334 | + assert.strictEqual(result.stderr.toString(), ''); | ||
| 335 | + assert(result.stdout.toString().includes(report)); | ||
| 336 | + assert.strictEqual(result.status, 0); | ||
| 337 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments