v160 · dom · workers · tooling + advanced

Synthetic connect fidelity

Unit tests for shared-worker code usually fake the handshake with new MessageEvent("connect", init). Chrome 160 changed the browser-fired event only: the MessageEvent constructor still defaults data to null, per MessageEventInit. So a fake that used to match Chrome now matches only Chrome ≤159. Pick a fake; the worker builds it next to the real connect event it just received and diffs them field by field.

Checking for SharedWorker…

Compare a fake with the real event

Synthetic event to build inside the worker
Not compared yet.
Field-by-field diff: real connect event vs. synthetic
FieldReal (browser-fired)SyntheticSame?
————

what to put in your test fake

// Matches Chrome 160+, Firefox and Safari (everything except isTrusted):
const { port1 } = new MessageChannel();
self.dispatchEvent(new MessageEvent("connect", {
  data: "",            // the constructor default is still null!
  ports: [port1],      // the constructor freezes this array for you
  source: port1,
}));

// isTrusted is always false on a synthetic event — never branch on it
// in production code you also want to unit-test.

The real event can only be produced by the browser, so the “real” column is measured, not assumed. On Chrome 160+ you can get the legacy real value back for comparison by launching with --disable-blink-features=SharedWorkerConnectEventEmptyStringData.

see also