vitest-dev/vitest · error · SyntaxError
"postMessage" requires at least one argument.
Error message
"postMessage" requires at least one argument.
What it means
The worker-side context object created by Vitest's Worker constructor (packages/web-worker/src/worker.ts:74-79) exposes a `postMessage` to code running INSIDE the worker (i.e. `self.postMessage(...)`). It mirrors the WHATWG DedicatedWorkerGlobalScope.postMessage contract, which requires a message argument. Calling it with zero arguments throws a SyntaxError, exactly like a real browser worker would.
Source
Thrown at packages/web-worker/src/worker.ts:76
origin: typeof location !== 'undefined' ? location.origin : 'http://localhost:3000',
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: (...args: any[]) => {
return this._vw_workerTarget.removeEventListener(...args as [any, any])
},
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
},
}
selfProxy = new Proxy(context, {View on GitHub (pinned to d568f8ce37)
Solutions
- Pass an explicit message: `self.postMessage(payload)` — even `null` or `{}` is valid if you only need a signal.
- If the call is conditional, guard it so postMessage is only invoked when there is a payload to send.
- Audit any wrapper around postMessage to ensure it forwards at least the first argument.
Example fix
// before — inside worker.js
function notify() {
self.postMessage() // forgot the payload
}
// after
function notify(payload) {
if (payload !== undefined) self.postMessage(payload)
} Defensive patterns
Strategy: validation
Validate before calling
// Guard worker-side postMessage calls so they never run empty.
function postFromWorker(payload, transfer) {
if (arguments.length === 0 && payload === undefined) {
throw new TypeError('postFromWorker requires a payload')
}
const args = transfer ? [payload, transfer] : [payload ?? null]
self.postMessage(...args)
} Prevention
- Always pass an explicit payload to self.postMessage — use `null` only when intentionally sending an empty signal.
- Forward variadic args with a minimum-arity assertion in any wrapper.
- Unit-test worker message helpers with the no-argument case to catch dropped payloads early.
When it happens
Trigger: Worker code calling `self.postMessage()` or `postMessage()` with no arguments; a conditional code path that calls postMessage only for its side effect; a wrapper/helper that forwards an arguments object and forwards zero args; off-by-one logic that drops the message payload before posting.
Common situations: Refactoring worker message handlers and accidentally dropping the payload; calling postMessage() inside a wrapper that destructures `(...args)` but receives none; mirroring a parent API that occasionally posts empty signals; testing edge cases where the worker has nothing to send yet still calls postMessage.
Related errors
- bitLength is required
- Negative numbers are not supported
- Cannot compare screenshots without a test path
- `resolveOptions` has to be used in a test file
- Invalid command name "${name}". Only alphanumeric characters
AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03).
Data as JSON: /data/errors/0e27a57b5c4686a4.json.
Report an issue: GitHub.