Skip to content

Commit

Permalink
fix(coverage): v8 to warn instead of crash when conversion fails (#6318)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Aug 12, 2024
1 parent 1f6cb59 commit 91dea8c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,13 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
)
await converter.load()

converter.applyCoverage(functions)
try {
converter.applyCoverage(functions)
}
catch (error) {
this.ctx.logger.error(`Failed to convert coverage for ${url}.\n`, error)
}

coverageMap.merge(converter.toIstanbul())
}),
)
Expand Down
2 changes: 2 additions & 0 deletions test/coverage-test/fixtures/src/cjs-package/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require("./target");
module.exports = "Entry here"
5 changes: 5 additions & 0 deletions test/coverage-test/fixtures/src/cjs-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "cjs-package",
"main": "./entry.js",
"type": "commonjs"
}
9 changes: 9 additions & 0 deletions test/coverage-test/fixtures/src/cjs-package/target.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

module.exports = {
debug: 0,
info: 1,
warn: 2,
error: 3,
fatal: 4,
};
40 changes: 40 additions & 0 deletions test/coverage-test/test/convert-failure.v8.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { createRequire } from 'node:module'
import { expect } from 'vitest'
import { coverageTest, normalizeURL, readCoverageMap, runVitest, test } from '../utils'

test('logs warning but doesn\'t crash when coverage conversion fails', async () => {
const { stderr, exitCode } = await runVitest({
include: [normalizeURL(import.meta.url)],
coverage: { reporter: 'json', include: ['fixtures/src/**'], all: false },
}, { throwOnError: false })

// Logged warning should not set erroneous exit code
expect(exitCode).toBe(0)

expect(stderr).toMatch('Failed to convert coverage for file://')
expect(stderr).toMatch('/fixtures/src/cjs-package/target.js.')
expect(stderr).toMatch('TypeError: Cannot read properties of undefined (reading \'endCol\')')

const coverageMap = await readCoverageMap()

expect(coverageMap.files()).toMatchInlineSnapshot(`
[
"<process-cwd>/fixtures/src/cjs-package/entry.js",
"<process-cwd>/fixtures/src/cjs-package/target.js",
]
`)
})

coverageTest('load file both from Vite and outside it', async () => {
const entry = createRequire(import.meta.url)('../fixtures/src/cjs-package' as any)
const target = await import('../fixtures/src/cjs-package/target.js' as any)

expect(entry).toBe('Entry here')
expect(target.default).toStrictEqual({
debug: 0,
error: 3,
fatal: 4,
info: 1,
warn: 2,
})
})

0 comments on commit 91dea8c

Please sign in to comment.