Skip to content

Commit

Permalink
test: failing test for vitest-dev#7379
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Feb 6, 2025
1 parent 12eaf3e commit ac25a5e
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 4 deletions.
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions test/coverage-test/fixtures/src/worker-wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export async function sumInBackground(a: number, b: number) {
const worker = new Worker(new URL('./worker', import.meta.url), {
type: 'module',
});


const promise = new Promise<MessageEvent>(resolve => {
worker.onmessage = resolve;
});

function uncovered() {
return "This is uncovered"
}

worker.postMessage({ a, b });

const result = await promise;
covered();
worker.terminate();

return result.data;
}


function covered() {
return "This is covered"
}

export function uncovered() {
return "This is uncovered"
}
25 changes: 25 additions & 0 deletions test/coverage-test/fixtures/src/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
self.onmessage = (ev: MessageEvent) => {
const {a, b} = ev.data;

if (a === 5) {
uncovered()
throw new Error("uncovered");
}

if(b === 6) {
uncovered()
throw new Error("uncovered");

}

covered();
postMessage(a + b);
};

export function uncovered() {
return "This is uncovered"
}

function covered() {
return "This is covered"
}
8 changes: 8 additions & 0 deletions test/coverage-test/fixtures/test/web-worker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { expect, test } from 'vitest';
import { sumInBackground } from '../src/worker-wrapper';

test('run a Worker', async () => {
const result = await sumInBackground(15, 7);

expect(result).toBe(22);
});
1 change: 1 addition & 0 deletions test/coverage-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@vitest/browser": "workspace:*",
"@vitest/coverage-istanbul": "workspace:*",
"@vitest/coverage-v8": "workspace:*",
"@vitest/web-worker": "workspace:*",
"@vue/test-utils": "latest",
"happy-dom": "latest",
"istanbul-lib-coverage": "^3.2.0",
Expand Down
67 changes: 67 additions & 0 deletions test/coverage-test/test/web-worker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { expect } from 'vitest'
import { formatSummary, isV8Provider, readCoverageMap, runVitest, test } from '../utils'

test('web worker coverage is correct', async () => {
await runVitest({
setupFiles: ['@vitest/web-worker'],
include: ['fixtures/test/web-worker.test.ts'],
environment: 'jsdom',
coverage: {
include: [
// Runs in web-worker's runner with custom context -> execution wrapper ~430 chars
'fixtures/src/worker.ts',

// Runs in default runner -> execution wrapper ~185 chars
'fixtures/src/worker-wrapper.ts',
],
reporter: 'json',
},
})

const coverageMap = await readCoverageMap()
const worker = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/worker.ts')
const wrapper = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/worker-wrapper.ts')

const summary = {
[worker.path]: formatSummary(worker.toSummary()),
[wrapper.path]: formatSummary(wrapper.toSummary()),
}

// Check HTML report if these change unexpectedly
if (isV8Provider()) {
expect(summary).toMatchInlineSnapshot(`
{
"<process-cwd>/fixtures/src/worker-wrapper.ts": {
"branches": "3/3 (100%)",
"functions": "2/4 (50%)",
"lines": "18/22 (81.81%)",
"statements": "18/22 (81.81%)",
},
"<process-cwd>/fixtures/src/worker.ts": {
"branches": "2/4 (50%)",
"functions": "2/3 (66.66%)",
"lines": "11/19 (57.89%)",
"statements": "11/19 (57.89%)",
},
}
`)
}
else {
expect(summary).toMatchInlineSnapshot(`
{
"<process-cwd>/fixtures/src/worker-wrapper.ts": {
"branches": "0/0 (100%)",
"functions": "3/5 (60%)",
"lines": "9/11 (81.81%)",
"statements": "9/11 (81.81%)",
},
"<process-cwd>/fixtures/src/worker.ts": {
"branches": "2/4 (50%)",
"functions": "2/3 (66.66%)",
"lines": "7/12 (58.33%)",
"statements": "7/12 (58.33%)",
},
}
`)
}
})
9 changes: 8 additions & 1 deletion test/coverage-test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FileCoverageData } from 'istanbul-lib-coverage'
import type { CoverageSummary, FileCoverageData } from 'istanbul-lib-coverage'
import type { TestFunction } from 'vitest'
import type { UserConfig } from 'vitest/node'
import { readFileSync } from 'node:fs'
Expand Down Expand Up @@ -89,6 +89,13 @@ export async function readCoverageMap(name = './coverage/coverage-final.json') {
return libCoverage.createCoverageMap(coverageJson)
}

export function formatSummary(summary: CoverageSummary) {
return (['branches', 'functions', 'lines', 'statements'] as const).reduce((all, current) => ({
...all,
[current]: `${summary[current].covered}/${summary[current].total} (${summary[current].pct}%)`,
}), {})
}

export function normalizeFilename(filename: string) {
return normalize(filename)
.replace(normalize(process.cwd()), '<process-cwd>')
Expand Down
3 changes: 0 additions & 3 deletions test/coverage-test/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
server: {
watch: null,
},
test: {
reporters: 'verbose',
isolate: false,
Expand Down

0 comments on commit ac25a5e

Please sign in to comment.