test: stop the best-effort relay sync from flaking the suite exit code
trySyncWithBackend fires a best-effort fetch('/api/sync') on many component mounts; in happy-dom it resolves to the dead localhost:3000 origin and the undici socket error could race teardown and flip the run's exit code to 1 even though all 791 tests passed. The global test setup now intercepts only that relay endpoint and rejects instantly with no socket, so best-effort sync still exercises its caught-error path. Verified: WalletCard's sync mount shows 0 ECONNREFUSED and 9/9 full-suite runs exit 0.
This commit is contained in:
1 parent
1cbc223ef9
commit
d126caf0fc
1 file changed
+32
@@ -1 +1,33 @@
|
||||
import "fake-indexeddb/auto";
|
||||
|
||||
// The best-effort cross-device sync (`trySyncWithBackend`) fires a fetch to the
|
||||
// relay (`/api/sync`) on many component mounts. In the happy-dom test
|
||||
// environment that relative URL resolves against the default document origin
|
||||
// (http://localhost:3000) and attempts a real TCP connect that ECONNREFUSEs.
|
||||
// The caller catches that rejection (sync is strictly best-effort), but the
|
||||
// undici socket error can still race test teardown and flip the whole run's
|
||||
// exit code to 1 even though every test passed - a flaky, non-deterministic
|
||||
// gate. Intercept ONLY that dead relay endpoint and reject instantly with no
|
||||
// socket, so best-effort sync still exercises its own caught-error path without
|
||||
// an open connection outliving the test. Any other fetch (real API integration
|
||||
// tests on their own ports) delegates to the real implementation, and tests
|
||||
// that need a specific fetch override this with `vi.stubGlobal`.
|
||||
const realFetch = globalThis.fetch?.bind(globalThis);
|
||||
if (realFetch) {
|
||||
globalThis.fetch = ((input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url =
|
||||
typeof input === "string"
|
||||
? input
|
||||
: input instanceof URL
|
||||
? input.href
|
||||
: (input as Request).url;
|
||||
if (
|
||||
url.startsWith("/api/sync") ||
|
||||
url.includes("localhost:3000") ||
|
||||
url.includes("127.0.0.1:3000")
|
||||
) {
|
||||
return Promise.reject(new Error("relay unavailable (test stub)"));
|
||||
}
|
||||
return realFetch(input, init);
|
||||
}) as typeof fetch;
|
||||
}
|
||||
Reference in new issue
Block a user