From 35a2d86dbb6857eac38a52e8469ba9040b2c633f Mon Sep 17 00:00:00 2001 From: Giancarmine Salucci Date: Sun, 10 May 2026 15:56:05 +0200 Subject: [PATCH] test: add beforeEach cleanup in push.test.ts to prevent flaky state leakage Adds a beforeEach hook that clears subscriptions and resets mocks before each test, making the suite robust against any state left by a previous test even if afterEach didn't run cleanly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/tests/push.test.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/tests/push.test.ts b/src/tests/push.test.ts index df4e39b..029e331 100644 --- a/src/tests/push.test.ts +++ b/src/tests/push.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, vi, afterEach } from 'vitest'; +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; // ── Hoist mock functions so they're available inside vi.mock() factories ─────── const { mockSetVapidDetails, mockWebPushSend } = vi.hoisted(() => ({ @@ -24,12 +24,16 @@ import { sendNotification, getVapidPublicKey } from '$lib/server/push.js'; import { savePushSubscription, deletePushSubscription, getAllSubscriptions } from '$lib/server/db.js'; import { rm } from 'fs/promises'; -afterEach(async () => { - mockSetVapidDetails.mockReset(); +beforeEach(() => { + // Ensure a clean subscription table before each test + for (const s of getAllSubscriptions()) deletePushSubscription(s.endpoint); mockWebPushSend.mockReset(); + mockSetVapidDetails.mockReset(); +}); + +afterEach(async () => { // Remove all test subscriptions between tests - const subs = getAllSubscriptions(); - for (const s of subs) deletePushSubscription(s.endpoint); + for (const s of getAllSubscriptions()) deletePushSubscription(s.endpoint); await rm(TEST_DATA_DIR, { recursive: true, force: true }).catch(() => {}); });