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>
This commit is contained in:
@@ -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(() => {});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user