forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: failing test for vitest-dev#7379
- Loading branch information
1 parent
c82387d
commit 954d2a1
Showing
6 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export async function calcInBackground(arg: number) { | ||
const worker = new Worker(new URL('./worker', import.meta.url), { | ||
type: 'module', | ||
}); | ||
|
||
const cleanup = () => { | ||
worker.terminate(); | ||
}; | ||
|
||
let doResolve: ((arg: number) => void) | undefined = undefined; | ||
const resultPromise = new Promise<number>((resolve) => { | ||
doResolve = resolve; | ||
}); | ||
|
||
worker.onmessage = (ev: MessageEvent<number>) => { | ||
doResolve!(ev.data); | ||
}; | ||
|
||
worker.postMessage(arg); | ||
|
||
const result = await resultPromise; | ||
cleanup(); | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
self.onmessage = (ev: MessageEvent<number>) => { | ||
const start = performance.now(); | ||
let data = ev.data; | ||
if (data === 5) { | ||
uncovered() | ||
throw new Error("uncovered"); | ||
} | ||
|
||
if(data === 6) { | ||
uncovered() | ||
throw new Error("uncovered"); | ||
|
||
} | ||
|
||
while (data > 9) { | ||
data -= 1; | ||
} | ||
|
||
const result = doCalc(data); | ||
postMessage(result); | ||
}; | ||
|
||
function uncovered() { | ||
return "This is uncovered" | ||
} | ||
|
||
function doCalc(arg: number) { | ||
return arg * 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { expect, test } from 'vitest'; | ||
import { calcInBackground } from '../src/worker-wrapper'; | ||
|
||
test('worker', async () => { | ||
const result = await calcInBackground(45); | ||
expect(result).toBe(18); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { expect } from 'vitest' | ||
import { isV8Provider, readCoverageMap, runVitest, test } from '../utils' | ||
|
||
test('web worker coverage is correct', async () => { | ||
await runVitest({ | ||
setupFiles: ['@vitest/web-worker'], | ||
include: ['fixtures/test/web-worker.ts'], | ||
coverage: { | ||
include: ['fixtures/src/worker.ts'], | ||
reporter: 'json', | ||
}, | ||
}) | ||
|
||
const coverageMap = await readCoverageMap() | ||
const fileCoverage = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/worker.ts') | ||
const summary = fileCoverage.toSummary() | ||
|
||
if (isV8Provider()) { | ||
expect(summary).toMatchInlineSnapshot(` | ||
{ | ||
"branches": { | ||
"covered": 3, | ||
"pct": 60, | ||
"skipped": 0, | ||
"total": 5, | ||
}, | ||
"functions": { | ||
"covered": 2, | ||
"pct": 66.66, | ||
"skipped": 0, | ||
"total": 3, | ||
}, | ||
"lines": { | ||
"covered": 14, | ||
"pct": 60.86, | ||
"skipped": 0, | ||
"total": 23, | ||
}, | ||
"statements": { | ||
"covered": 14, | ||
"pct": 60.86, | ||
"skipped": 0, | ||
"total": 23, | ||
}, | ||
} | ||
`) | ||
} | ||
else { | ||
expect(summary).toMatchInlineSnapshot(` | ||
{ | ||
"branches": { | ||
"covered": 2, | ||
"pct": 50, | ||
"skipped": 0, | ||
"total": 4, | ||
}, | ||
"functions": { | ||
"covered": 2, | ||
"pct": 66.66, | ||
"skipped": 0, | ||
"total": 3, | ||
}, | ||
"lines": { | ||
"covered": 10, | ||
"pct": 66.66, | ||
"skipped": 0, | ||
"total": 15, | ||
}, | ||
"statements": { | ||
"covered": 10, | ||
"pct": 66.66, | ||
"skipped": 0, | ||
"total": 15, | ||
}, | ||
} | ||
`) | ||
} | ||
}) |