Skip to content

Commit 7495a3e

Browse files
committed
chore: improved cleanup explanations for sample test
1 parent a561b1a commit 7495a3e

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

test/1.test.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ describe('init', () => {
3838
vi.spyOn(db, 'getRepo').mockResolvedValue(TEST_REPO);
3939
});
4040

41+
// Runs after each test
42+
afterEach(function () {
43+
// Restore all stubs: This cleans up replaced behaviour on existing modules
44+
// Required when using vi.spyOn or vi.fn to stub modules/functions
45+
vi.restoreAllMocks();
46+
47+
// Clear module cache: Wipes modules cache so imports are fresh for the next test file
48+
// Required when using vi.doMock to override modules
49+
vi.resetModules();
50+
});
51+
52+
// Runs after all tests
53+
afterAll(function () {
54+
// Must close the server to avoid EADDRINUSE errors when running tests in parallel
55+
service.httpServer.close();
56+
});
57+
4158
// Example test: check server is running
4259
it('should return 401 if not logged in', async function () {
4360
const res = await request(app).get('/api/auth/profile');
@@ -80,18 +97,4 @@ describe('init', () => {
8097
expect(authMethods).toHaveLength(3);
8198
expect(authMethods[0].type).toBe('local');
8299
});
83-
84-
// Runs after each test
85-
afterEach(function () {
86-
// Restore all stubs
87-
vi.restoreAllMocks();
88-
89-
// Clear module cache
90-
vi.resetModules();
91-
});
92-
93-
// Runs after all tests
94-
afterAll(function () {
95-
service.httpServer.close();
96-
});
97100
});

0 commit comments

Comments
 (0)