From 8aacdafd4f9e49275c4f513d4e34d69f565ed9fc Mon Sep 17 00:00:00 2001 From: AriPerkkio Date: Fri, 21 Apr 2023 16:43:31 +0300 Subject: [PATCH] fix(coverage): workspaces c8 source maps --- packages/coverage-c8/src/provider.ts | 6 ++- pnpm-lock.yaml | 6 +++ .../check-coverage.test.ts | 46 +++++++++++++++++++ .../coverage-report-tests/vitest.config.ts | 7 +++ test/workspaces/package.json | 7 ++- test/workspaces/src/math.ts | 11 +++++ test/workspaces/vitest.config.ts | 1 + 7 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 test/workspaces/coverage-report-tests/check-coverage.test.ts create mode 100644 test/workspaces/coverage-report-tests/vitest.config.ts diff --git a/packages/coverage-c8/src/provider.ts b/packages/coverage-c8/src/provider.ts index d37e446610876..e47ad12277e06 100644 --- a/packages/coverage-c8/src/provider.ts +++ b/packages/coverage-c8/src/provider.ts @@ -95,8 +95,12 @@ export class C8CoverageProvider extends BaseCoverageProvider implements Coverage const sourceMapMeta: Record = {} const extensions = Array.isArray(this.options.extension) ? this.options.extension : [this.options.extension] + const fetchCache = this.ctx.projects.map(project => + Array.from(project.vitenode.fetchCache.entries()), + ).flat() + const entries = Array - .from(this.ctx.vitenode.fetchCache.entries()) + .from(fetchCache) .filter(entry => report._shouldInstrument(entry[0])) .map(([file, { result }]) => { if (!result.map) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b9af1ed3e0007..7da5ad7ad3518 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1768,6 +1768,12 @@ importers: test/workspaces: devDependencies: + '@types/istanbul-lib-coverage': + specifier: ^2.0.4 + version: 2.0.4 + istanbul-lib-coverage: + specifier: ^3.2.0 + version: 3.2.0 jsdom: specifier: latest version: 21.1.1 diff --git a/test/workspaces/coverage-report-tests/check-coverage.test.ts b/test/workspaces/coverage-report-tests/check-coverage.test.ts new file mode 100644 index 0000000000000..f84df1cbdb927 --- /dev/null +++ b/test/workspaces/coverage-report-tests/check-coverage.test.ts @@ -0,0 +1,46 @@ +import { existsSync, readFileSync } from 'node:fs' +import { normalize } from 'node:path' +import { expect, test } from 'vitest' +import libCoverage from 'istanbul-lib-coverage' +import { resolve } from 'pathe' + +test('coverage exists', () => { + expect(existsSync('./coverage')).toBe(true) + expect(existsSync('./coverage/index.html')).toBe(true) +}) + +test('file coverage summary matches', () => { + const coverageJson = JSON.parse(readFileSync('./coverage/coverage-final.json', 'utf-8')) + const coverageMap = libCoverage.createCoverageMap(coverageJson) + const fileCoverage = coverageMap.fileCoverageFor(normalize(resolve('./src/math.ts'))) + + // There should be 1 uncovered branch and 1 uncovered function. See math.ts. + expect(fileCoverage.toSummary()).toMatchInlineSnapshot(` + { + "branches": { + "covered": 3, + "pct": 75, + "skipped": 0, + "total": 4, + }, + "functions": { + "covered": 2, + "pct": 66.66, + "skipped": 0, + "total": 3, + }, + "lines": { + "covered": 7, + "pct": 50, + "skipped": 0, + "total": 14, + }, + "statements": { + "covered": 7, + "pct": 50, + "skipped": 0, + "total": 14, + }, + } + `) +}) diff --git a/test/workspaces/coverage-report-tests/vitest.config.ts b/test/workspaces/coverage-report-tests/vitest.config.ts new file mode 100644 index 0000000000000..18bb8372ffc45 --- /dev/null +++ b/test/workspaces/coverage-report-tests/vitest.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + test: { + include: ['./check-coverage.test.ts'], + }, +}) diff --git a/test/workspaces/package.json b/test/workspaces/package.json index 0b411b9d61268..10bc7a91f8bff 100644 --- a/test/workspaces/package.json +++ b/test/workspaces/package.json @@ -3,10 +3,13 @@ "type": "module", "private": true, "scripts": { - "test": "vitest", - "coverage": "vitest run --coverage" + "test": "pnpm test:workspace && pnpm test:coverage", + "test:workspace": "vitest run", + "test:coverage": "vitest run --root coverage-report-tests" }, "devDependencies": { + "@types/istanbul-lib-coverage": "^2.0.4", + "istanbul-lib-coverage": "^3.2.0", "jsdom": "latest", "vitest": "workspace:*" } diff --git a/test/workspaces/src/math.ts b/test/workspaces/src/math.ts index 5d8550bb9d4d8..f1e1874089f6e 100644 --- a/test/workspaces/src/math.ts +++ b/test/workspaces/src/math.ts @@ -1,3 +1,14 @@ +/* eslint-disable unused-imports/no-unused-vars */ export function sum(a: number, b: number) { + if (a === 3 && b === 4) { + // This should be uncovered + return 7 + } + return a + b } + +function uncoveredFunction() { + // This should be uncovered + return 1 +} diff --git a/test/workspaces/vitest.config.ts b/test/workspaces/vitest.config.ts index 977555a2c4782..a817e50b78f3c 100644 --- a/test/workspaces/vitest.config.ts +++ b/test/workspaces/vitest.config.ts @@ -9,6 +9,7 @@ if (process.env.TEST_WATCH) { export default defineConfig({ test: { coverage: { + enabled: true, all: true, }, reporters: ['default', 'json'],