Fix BroadcastChannel errors in vitest

Added mock for BroadcastChannel in vitest setup to resolve type mismatch
between Node.js and browser implementations. Pinia uses BroadcastChannel
for state synchronization, which was causing unhandled exceptions during
test runs. The mock extends EventTarget and provides no-op implementations
for postMessage and close methods.
This commit is contained in:
Dannon Baker
2025-11-19 23:23:02 -05:00
parent 06fbd05c58
commit e7d09d70fa
+25
View File
@@ -63,6 +63,31 @@ if (typeof HTMLDialogElement !== "undefined") {
HTMLDialogElement.prototype.close = HTMLDialogElement.prototype.close || vi.fn();
}
// Mock BroadcastChannel to fix Pinia state synchronization errors
// Node.js's BroadcastChannel has a type mismatch with the browser API
class MockBroadcastChannel extends EventTarget {
name: string;
constructor(name: string) {
super();
this.name = name;
}
postMessage() {
// No-op for tests
}
close() {
// No-op for tests
}
}
Object.defineProperty(global, "BroadcastChannel", {
writable: true,
configurable: true,
value: MockBroadcastChannel,
});
// Import and setup MSW if needed
// This will be uncommented when tests using MSW are migrated
// import { setupServer } from "msw/node";