Skip to content

Commit a7c9446

Browse files
claudeenko
authored andcommitted
chore: migrate integration test helpers from process.env to vi.stubEnv
Replace fragile process.env assignments and delete statements with Vitest's vi.stubEnv() and vi.unstubAllEnvs() API across all four integration test helper files. This ensures env variables are restored atomically even if tests crash before afterAll runs, and brings all helpers into consistency with health.test.ts (reference implementation). Files migrated: - auth.helpers.ts - friends.helpers.ts - search.helpers.ts - postgis.helpers.ts https://claude.ai/code/session_01SxmmhKAEjzLfZJv8GaFmCs
1 parent 6a55c04 commit a7c9446

File tree

4 files changed

+31
-52
lines changed

4 files changed

+31
-52
lines changed

apps/backend/tests/integration/auth.helpers.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { Hono } from 'hono';
77
import { runner } from 'node-pg-migrate';
88
import pg from 'pg';
99
import { Wait } from 'testcontainers';
10-
import { afterAll, beforeAll, beforeEach } from 'vitest';
10+
import { afterAll, beforeAll, beforeEach, vi } from 'vitest';
1111
import { createApp } from '../../src/index.js';
1212
import { resetAuth } from '../../src/lib/auth.js';
1313
import { resetRateLimiters } from '../../src/middleware/rate-limit.js';
@@ -41,7 +41,7 @@ export async function setupAuthTests(): Promise<AuthTestContext> {
4141
await resetAuth();
4242

4343
// Set DATABASE_URL from the container
44-
process.env.DATABASE_URL = container.getConnectionUri();
44+
vi.stubEnv('DATABASE_URL', container.getConnectionUri());
4545
resetConfig();
4646

4747
// Create connection pool
@@ -365,12 +365,12 @@ export function setupAuthTestSuite() {
365365

366366
beforeAll(async () => {
367367
// Set required environment variables for tests
368-
process.env.BETTER_AUTH_SECRET = 'test-better-auth-secret-test-better-auth-secret-1';
369-
process.env.JWT_SECRET = 'test-jwt-secret-test-jwt-secret-1';
370-
process.env.SESSION_SECRET = 'test-session-secret-test-session-secret-1';
371-
process.env.JWT_EXPIRY = '604800';
372-
process.env.FRONTEND_URL = 'http://localhost:5173';
373-
process.env.LOG_LEVEL = 'silent';
368+
vi.stubEnv('BETTER_AUTH_SECRET', 'test-better-auth-secret-test-better-auth-secret-1');
369+
vi.stubEnv('JWT_SECRET', 'test-jwt-secret-test-jwt-secret-1');
370+
vi.stubEnv('SESSION_SECRET', 'test-session-secret-test-session-secret-1');
371+
vi.stubEnv('JWT_EXPIRY', '604800');
372+
vi.stubEnv('FRONTEND_URL', 'http://localhost:5173');
373+
vi.stubEnv('LOG_LEVEL', 'silent');
374374

375375
context = await setupAuthTests();
376376
}, 120000); // 120 second timeout for container startup
@@ -382,13 +382,7 @@ export function setupAuthTestSuite() {
382382

383383
afterAll(async () => {
384384
await teardownAuthTests(context);
385-
delete process.env.BETTER_AUTH_SECRET;
386-
delete process.env.JWT_SECRET;
387-
delete process.env.SESSION_SECRET;
388-
delete process.env.JWT_EXPIRY;
389-
delete process.env.FRONTEND_URL;
390-
delete process.env.DATABASE_URL;
391-
delete process.env.LOG_LEVEL;
385+
vi.unstubAllEnvs();
392386
resetConfig();
393387
}, 120000);
394388

apps/backend/tests/integration/friends.helpers.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type pg from 'pg';
2-
import { afterAll, beforeAll, beforeEach } from 'vitest';
2+
import { afterAll, beforeAll, beforeEach, vi } from 'vitest';
33
import { resetRateLimiters } from '../../src/middleware/rate-limit.js';
44
import { hashPassword } from '../../src/utils/auth.js';
55
import { resetConfig } from '../../src/utils/config.js';
@@ -159,12 +159,12 @@ export function setupFriendsTestSuite() {
159159

160160
beforeAll(async () => {
161161
// Set required environment variables for tests
162-
process.env.BETTER_AUTH_SECRET = 'test-better-auth-secret-test-better-auth-secret-1';
163-
process.env.JWT_SECRET = 'test-jwt-secret-test-jwt-secret-1';
164-
process.env.SESSION_SECRET = 'test-session-secret-test-session-secret-1';
165-
process.env.JWT_EXPIRY = '604800';
166-
process.env.FRONTEND_URL = 'http://localhost:5173';
167-
process.env.LOG_LEVEL = 'silent';
162+
vi.stubEnv('BETTER_AUTH_SECRET', 'test-better-auth-secret-test-better-auth-secret-1');
163+
vi.stubEnv('JWT_SECRET', 'test-jwt-secret-test-jwt-secret-1');
164+
vi.stubEnv('SESSION_SECRET', 'test-session-secret-test-session-secret-1');
165+
vi.stubEnv('JWT_EXPIRY', '604800');
166+
vi.stubEnv('FRONTEND_URL', 'http://localhost:5173');
167+
vi.stubEnv('LOG_LEVEL', 'silent');
168168

169169
const authContext = await setupAuthTests();
170170

@@ -194,13 +194,7 @@ export function setupFriendsTestSuite() {
194194

195195
afterAll(async () => {
196196
await teardownAuthTests(context);
197-
delete process.env.BETTER_AUTH_SECRET;
198-
delete process.env.JWT_SECRET;
199-
delete process.env.SESSION_SECRET;
200-
delete process.env.JWT_EXPIRY;
201-
delete process.env.FRONTEND_URL;
202-
delete process.env.DATABASE_URL;
203-
delete process.env.LOG_LEVEL;
197+
vi.unstubAllEnvs();
204198
resetConfig();
205199
}, 120000);
206200

apps/backend/tests/integration/postgis.helpers.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { runner } from 'node-pg-migrate';
66
import pg from 'pg';
77
import pino from 'pino';
88
import { Wait } from 'testcontainers';
9-
import { afterAll, beforeAll } from 'vitest';
9+
import { afterAll, beforeAll, vi } from 'vitest';
1010
import { PostGISAddressClient } from '../../src/services/external/postgis-address.client.js';
1111
import { resetConfig } from '../../src/utils/config.js';
1212

@@ -50,7 +50,7 @@ export async function setupPostGISTests(): Promise<PostGISTestContext> {
5050
const connectionUri = container.getConnectionUri();
5151

5252
// Set DATABASE_URL from the container
53-
process.env.DATABASE_URL = connectionUri;
53+
vi.stubEnv('DATABASE_URL', connectionUri);
5454
resetConfig();
5555

5656
// Create connection pool
@@ -176,19 +176,16 @@ export function setupPostGISTestSuite() {
176176

177177
beforeAll(async () => {
178178
// Set required environment variables for tests
179-
process.env.JWT_SECRET = 'test-jwt-secret-test-jwt-secret-1';
180-
process.env.SESSION_SECRET = 'test-session-secret-test-session-secret-1';
181-
process.env.LOG_LEVEL = 'silent';
179+
vi.stubEnv('JWT_SECRET', 'test-jwt-secret-test-jwt-secret-1');
180+
vi.stubEnv('SESSION_SECRET', 'test-session-secret-test-session-secret-1');
181+
vi.stubEnv('LOG_LEVEL', 'silent');
182182

183183
context = await setupPostGISTests();
184184
}, 180000); // 180 second timeout for container startup
185185

186186
afterAll(async () => {
187187
await teardownPostGISTests(context);
188-
delete process.env.JWT_SECRET;
189-
delete process.env.SESSION_SECRET;
190-
delete process.env.DATABASE_URL;
191-
delete process.env.LOG_LEVEL;
188+
vi.unstubAllEnvs();
192189
resetConfig();
193190
}, 60000);
194191

apps/backend/tests/integration/search.helpers.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type pg from 'pg';
2-
import { afterAll, beforeAll, beforeEach } from 'vitest';
2+
import { afterAll, beforeAll, beforeEach, vi } from 'vitest';
33
import { resetRateLimiters } from '../../src/middleware/rate-limit.js';
44
import { resetConfig } from '../../src/utils/config.js';
55
import { completeTestUserOnboarding, setupAuthTests, teardownAuthTests } from './auth.helpers.js';
@@ -300,12 +300,12 @@ export function setupSearchTestSuite() {
300300

301301
beforeAll(async () => {
302302
// Set required environment variables for tests
303-
process.env.BETTER_AUTH_SECRET = 'test-better-auth-secret-test-better-auth-secret-1';
304-
process.env.JWT_SECRET = 'test-jwt-secret-test-jwt-secret-1';
305-
process.env.SESSION_SECRET = 'test-session-secret-test-session-secret-1';
306-
process.env.JWT_EXPIRY = '604800';
307-
process.env.FRONTEND_URL = 'http://localhost:5173';
308-
process.env.LOG_LEVEL = 'silent';
303+
vi.stubEnv('BETTER_AUTH_SECRET', 'test-better-auth-secret-test-better-auth-secret-1');
304+
vi.stubEnv('JWT_SECRET', 'test-jwt-secret-test-jwt-secret-1');
305+
vi.stubEnv('SESSION_SECRET', 'test-session-secret-test-session-secret-1');
306+
vi.stubEnv('JWT_EXPIRY', '604800');
307+
vi.stubEnv('FRONTEND_URL', 'http://localhost:5173');
308+
vi.stubEnv('LOG_LEVEL', 'silent');
309309

310310
const authContext = await setupAuthTests();
311311

@@ -338,13 +338,7 @@ export function setupSearchTestSuite() {
338338

339339
afterAll(async () => {
340340
await teardownAuthTests(context);
341-
delete process.env.BETTER_AUTH_SECRET;
342-
delete process.env.JWT_SECRET;
343-
delete process.env.SESSION_SECRET;
344-
delete process.env.JWT_EXPIRY;
345-
delete process.env.FRONTEND_URL;
346-
delete process.env.DATABASE_URL;
347-
delete process.env.LOG_LEVEL;
341+
vi.unstubAllEnvs();
348342
resetConfig();
349343
}, 120000);
350344

0 commit comments

Comments
 (0)