Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/__helpers__/restore-test-spies.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { afterEach, beforeEach, type MockInstance, vi } from 'vitest';

/**
* Restore spies created inside a test (or its hooks) after each test.
*
* The suite intentionally keeps some spies at module scope (inside `describe`
* blocks) so every test in the file reuses them; a global `restoreMocks` would
* tear those down after the first test. Track spies instead: module-scope spies
* are created before the first `beforeEach` and are forgotten by the reset,
* while spies created during a test are restored afterwards.
*/
const testSpies: MockInstance[] = [];
const persistentSpies = new Set<MockInstance>();
let firstTest = true;
const originalSpyOn = vi.spyOn.bind(vi) as (...args: unknown[]) => MockInstance;

vi.spyOn = ((...args: unknown[]) => {
const spy = originalSpyOn(...args);

if (!persistentSpies.has(spy)) {
testSpies.push(spy);
}

return spy;
}) as unknown as typeof vi.spyOn;

beforeEach(() => {
if (firstTest) {
firstTest = false;

// Anything already spied before the first test belongs to the file, not the
// test. Keep it alive for every test in the file.
for (const spy of testSpies) {
persistentSpies.add(spy);
}
}

testSpies.length = 0;
});

afterEach(() => {
for (const spy of testSpies.splice(0)) {
spy.mockRestore();
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ describe('renderer/hooks/notifications/useNotificationActions.ts', () => {
beforeEach(() => {
getAllNotificationsMock.mockReset();
useAccountsStore.setState({ accounts: [mockGitHubCloudAccount] });
mockGitifyNotification.unread = true;
});

it('exposes the notification actions without fetching or creating a query', () => {
Expand Down
5 changes: 2 additions & 3 deletions src/renderer/hooks/useNotifications.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,13 @@ describe('renderer/hooks/useNotifications.ts', () => {
raiseSoundNotificationSpy.mockClear();
raiseNativeNotificationSpy.mockClear();
getAllNotificationsMock.mockReset();
getAllNotificationsMock.mockResolvedValue([]);
refreshEnrichmentCacheMock.mockReset();
refreshEnrichmentCacheMock.mockReturnValue(false);
clearServerPollIntervals();

useAccountsStore.setState({ accounts: [mockGitHubCloudAccount] });
useNotificationActionFailuresStore.getState().reset();

// Reset mock notification state between tests since it's mutated
mockGitifyNotification.unread = true;
});

describe('fetching notifications', () => {
Expand Down
8 changes: 6 additions & 2 deletions vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default defineConfig({
test: {
globals: true,
pool: 'vmThreads',
isolate: false,
isolate: true,
clearMocks: true,
unstubEnvs: true,
unstubGlobals: true,
Expand Down Expand Up @@ -56,6 +56,7 @@ export default defineConfig({
exclude: [...defaultExclude, '**/*.visual.test.tsx'],
setupFiles: [
'./src/__helpers__/electron-log.setup.ts',
'./src/__helpers__/restore-test-spies.setup.ts',
'./src/renderer/__helpers__/vitest.setup.ts',
],
},
Expand All @@ -67,7 +68,10 @@ export default defineConfig({
name: 'node [main, shared]',
environment: 'node',
include: ['src/shared/**/*.test.{ts,tsx}', 'src/main/**/*.test.{ts,tsx}'],
setupFiles: ['./src/__helpers__/electron-log.setup.ts'],
setupFiles: [
'./src/__helpers__/electron-log.setup.ts',
'./src/__helpers__/restore-test-spies.setup.ts',
],
},
},
{
Expand Down
Loading