test: add beforeEach cleanup in push.test.ts to prevent flaky state leakage
All checks were successful
Build & Push Docker Image / test (push) Successful in 10s
Build & Push Docker Image / build-and-push (push) Successful in 42s

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>
This commit is contained in:
Giancarmine Salucci
2026-05-10 15:56:05 +02:00
parent 470dd1642f
commit 35a2d86dbb

View File

@@ -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(() => {});
});