Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect coverage in web workers with @vitest/web-worker and @vitest/coverage-v8 #7379

Open
6 tasks done
robojumper opened this issue Jan 29, 2025 · 1 comment · May be fixed by #7417
Open
6 tasks done

Incorrect coverage in web workers with @vitest/web-worker and @vitest/coverage-v8 #7379

robojumper opened this issue Jan 29, 2025 · 1 comment · May be fixed by #7417
Labels
feat: coverage Issues and PRs related to the coverage feature p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@robojumper
Copy link

robojumper commented Jan 29, 2025

Describe the bug

I'm using @vitest/web-worker to test a web worker. @vitest/coverage-v8 reports incorrect coverage metrics, while @vitest/coverage-istanbul seems to work fine.

Reproduction

https://github.com/robojumper/vitest-web-worker-v8-coverage-bug - run npm run test:coverage and view the coverage report for src/worker/worker.ts:

v8:
Image

istanbul:
Image

istanbul coverage looks correct while v8 coverage is completely wrong.

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (12) x64 AMD Ryzen 5 3600 6-Core Processor
    Memory: 18.36 GB / 31.91 GB
  Binaries:
    Node: 22.13.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 10.9.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.6.0 - ~\AppData\Roaming\npm\pnpm.CMD
  Browsers:
    Edge: Chromium (132.0.2957.127)
    Internet Explorer: 11.0.19041.4355
  npmPackages:
    @vitest/coverage-istanbul: ^3.0.4 => 3.0.4
    @vitest/coverage-v8: ^3.0.4 => 3.0.4
    @vitest/ui: ^3.0.4 => 3.0.4
    @vitest/web-worker: ^3.0.4 => 3.0.4
    vite: ^6.0.0 => 6.0.11
    vitest: ^3.0.4 => 3.0.4

Used Package Manager

npm

Validations

@AriPerkkio AriPerkkio added feat: coverage Issues and PRs related to the coverage feature p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Jan 30, 2025
@AriPerkkio
Copy link
Member

AriPerkkio commented Feb 4, 2025

The hard-coded execution wrapper doesn't match @vitest/web-worker's context lenght:

// TODO: vite-node should export this
const WRAPPER_LENGTH = 185

Custom context is created at:

prepareContext(context: Record<string, any>) {
const ctx = super.prepareContext(context)
// not supported for now, we can't synchronously load modules
return Object.assign(ctx, this.context, {
importScripts,
})
}

const context = {
onmessage: null as null | ((e: MessageEvent) => void),
onmessageerror: null,
onerror: null,
onlanguagechange: null,
onoffline: null,
ononline: null,
onrejectionhandled: null,
onrtctransform: null,
onunhandledrejection: null,
origin: typeof location !== 'undefined' ? location.origin : 'http://localhost:3000',
importScripts: () => {
throw new Error(
'[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead.',
)
},
crossOriginIsolated: false,
name: options?.name || '',
close: () => this.terminate(),
dispatchEvent: (event: Event) => {
return this._vw_workerTarget.dispatchEvent(event)
},
addEventListener: (...args: any[]) => {
if (args[1]) {
this._vw_insideListeners.set(args[0], args[1])
}
return this._vw_workerTarget.addEventListener(...args as [any, any])
},
removeEventListener: this._vw_workerTarget.removeEventListener,
postMessage: (...args: any[]) => {
if (!args.length) {
throw new SyntaxError(
'"postMessage" requires at least one argument.',
)
}
debug(
'posting message %o from the worker %s to the main thread',
args[0],
this._vw_name,
)
const event = createMessageEvent(args[0], args[1], cloneType())
this.dispatchEvent(event)
},
get self() {
return selfProxy
},
}

We should probably pass the WRAPPER_LENGTH from test runner threads to main thread when reporting coverage results. 🤔

Changing the WRAPPER_LENGTH to 429 fixes the issue:

AriPerkkio added a commit to AriPerkkio/vitest that referenced this issue Feb 4, 2025
AriPerkkio added a commit to AriPerkkio/vitest that referenced this issue Feb 4, 2025
AriPerkkio added a commit to AriPerkkio/vitest that referenced this issue Feb 6, 2025
AriPerkkio added a commit to AriPerkkio/vitest that referenced this issue Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: coverage Issues and PRs related to the coverage feature p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants