diff --git a/apps/backend/tests/integration/auth.helpers.ts b/apps/backend/tests/integration/auth.helpers.ts index 0e8213aa..c5218012 100644 --- a/apps/backend/tests/integration/auth.helpers.ts +++ b/apps/backend/tests/integration/auth.helpers.ts @@ -7,7 +7,7 @@ import type { Hono } from 'hono'; import { runner } from 'node-pg-migrate'; import pg from 'pg'; import { Wait } from 'testcontainers'; -import { afterAll, beforeAll, beforeEach } from 'vitest'; +import { afterAll, beforeAll, beforeEach, vi } from 'vitest'; import { createApp } from '../../src/index.js'; import { resetAuth } from '../../src/lib/auth.js'; import { resetRateLimiters } from '../../src/middleware/rate-limit.js'; @@ -41,7 +41,7 @@ export async function setupAuthTests(): Promise { await resetAuth(); // Set DATABASE_URL from the container - process.env.DATABASE_URL = container.getConnectionUri(); + vi.stubEnv('DATABASE_URL', container.getConnectionUri()); resetConfig(); // Create connection pool @@ -365,12 +365,12 @@ export function setupAuthTestSuite() { beforeAll(async () => { // Set required environment variables for tests - process.env.BETTER_AUTH_SECRET = 'test-better-auth-secret-test-better-auth-secret-1'; - process.env.JWT_SECRET = 'test-jwt-secret-test-jwt-secret-1'; - process.env.SESSION_SECRET = 'test-session-secret-test-session-secret-1'; - process.env.JWT_EXPIRY = '604800'; - process.env.FRONTEND_URL = 'http://localhost:5173'; - process.env.LOG_LEVEL = 'silent'; + vi.stubEnv('BETTER_AUTH_SECRET', 'test-better-auth-secret-test-better-auth-secret-1'); + vi.stubEnv('JWT_SECRET', 'test-jwt-secret-test-jwt-secret-1'); + vi.stubEnv('SESSION_SECRET', 'test-session-secret-test-session-secret-1'); + vi.stubEnv('JWT_EXPIRY', '604800'); + vi.stubEnv('FRONTEND_URL', 'http://localhost:5173'); + vi.stubEnv('LOG_LEVEL', 'silent'); context = await setupAuthTests(); }, 120000); // 120 second timeout for container startup @@ -382,13 +382,7 @@ export function setupAuthTestSuite() { afterAll(async () => { await teardownAuthTests(context); - delete process.env.BETTER_AUTH_SECRET; - delete process.env.JWT_SECRET; - delete process.env.SESSION_SECRET; - delete process.env.JWT_EXPIRY; - delete process.env.FRONTEND_URL; - delete process.env.DATABASE_URL; - delete process.env.LOG_LEVEL; + vi.unstubAllEnvs(); resetConfig(); }, 120000); diff --git a/apps/backend/tests/integration/friends.helpers.ts b/apps/backend/tests/integration/friends.helpers.ts index 53a83fc0..ebe9e3a2 100644 --- a/apps/backend/tests/integration/friends.helpers.ts +++ b/apps/backend/tests/integration/friends.helpers.ts @@ -1,5 +1,5 @@ import type pg from 'pg'; -import { afterAll, beforeAll, beforeEach } from 'vitest'; +import { afterAll, beforeAll, beforeEach, vi } from 'vitest'; import { resetRateLimiters } from '../../src/middleware/rate-limit.js'; import { hashPassword } from '../../src/utils/auth.js'; import { resetConfig } from '../../src/utils/config.js'; @@ -159,12 +159,12 @@ export function setupFriendsTestSuite() { beforeAll(async () => { // Set required environment variables for tests - process.env.BETTER_AUTH_SECRET = 'test-better-auth-secret-test-better-auth-secret-1'; - process.env.JWT_SECRET = 'test-jwt-secret-test-jwt-secret-1'; - process.env.SESSION_SECRET = 'test-session-secret-test-session-secret-1'; - process.env.JWT_EXPIRY = '604800'; - process.env.FRONTEND_URL = 'http://localhost:5173'; - process.env.LOG_LEVEL = 'silent'; + vi.stubEnv('BETTER_AUTH_SECRET', 'test-better-auth-secret-test-better-auth-secret-1'); + vi.stubEnv('JWT_SECRET', 'test-jwt-secret-test-jwt-secret-1'); + vi.stubEnv('SESSION_SECRET', 'test-session-secret-test-session-secret-1'); + vi.stubEnv('JWT_EXPIRY', '604800'); + vi.stubEnv('FRONTEND_URL', 'http://localhost:5173'); + vi.stubEnv('LOG_LEVEL', 'silent'); const authContext = await setupAuthTests(); @@ -194,13 +194,7 @@ export function setupFriendsTestSuite() { afterAll(async () => { await teardownAuthTests(context); - delete process.env.BETTER_AUTH_SECRET; - delete process.env.JWT_SECRET; - delete process.env.SESSION_SECRET; - delete process.env.JWT_EXPIRY; - delete process.env.FRONTEND_URL; - delete process.env.DATABASE_URL; - delete process.env.LOG_LEVEL; + vi.unstubAllEnvs(); resetConfig(); }, 120000); diff --git a/apps/backend/tests/integration/postgis.helpers.ts b/apps/backend/tests/integration/postgis.helpers.ts index 0a0abd0d..bdd704aa 100644 --- a/apps/backend/tests/integration/postgis.helpers.ts +++ b/apps/backend/tests/integration/postgis.helpers.ts @@ -6,7 +6,7 @@ import { runner } from 'node-pg-migrate'; import pg from 'pg'; import pino from 'pino'; import { Wait } from 'testcontainers'; -import { afterAll, beforeAll } from 'vitest'; +import { afterAll, beforeAll, vi } from 'vitest'; import { PostGISAddressClient } from '../../src/services/external/postgis-address.client.js'; import { resetConfig } from '../../src/utils/config.js'; @@ -50,7 +50,7 @@ export async function setupPostGISTests(): Promise { const connectionUri = container.getConnectionUri(); // Set DATABASE_URL from the container - process.env.DATABASE_URL = connectionUri; + vi.stubEnv('DATABASE_URL', connectionUri); resetConfig(); // Create connection pool @@ -176,19 +176,16 @@ export function setupPostGISTestSuite() { beforeAll(async () => { // Set required environment variables for tests - process.env.JWT_SECRET = 'test-jwt-secret-test-jwt-secret-1'; - process.env.SESSION_SECRET = 'test-session-secret-test-session-secret-1'; - process.env.LOG_LEVEL = 'silent'; + vi.stubEnv('JWT_SECRET', 'test-jwt-secret-test-jwt-secret-1'); + vi.stubEnv('SESSION_SECRET', 'test-session-secret-test-session-secret-1'); + vi.stubEnv('LOG_LEVEL', 'silent'); context = await setupPostGISTests(); }, 180000); // 180 second timeout for container startup afterAll(async () => { await teardownPostGISTests(context); - delete process.env.JWT_SECRET; - delete process.env.SESSION_SECRET; - delete process.env.DATABASE_URL; - delete process.env.LOG_LEVEL; + vi.unstubAllEnvs(); resetConfig(); }, 60000); diff --git a/apps/backend/tests/integration/search.helpers.ts b/apps/backend/tests/integration/search.helpers.ts index e7e424ec..dc3ed3c2 100644 --- a/apps/backend/tests/integration/search.helpers.ts +++ b/apps/backend/tests/integration/search.helpers.ts @@ -1,5 +1,5 @@ import type pg from 'pg'; -import { afterAll, beforeAll, beforeEach } from 'vitest'; +import { afterAll, beforeAll, beforeEach, vi } from 'vitest'; import { resetRateLimiters } from '../../src/middleware/rate-limit.js'; import { resetConfig } from '../../src/utils/config.js'; import { completeTestUserOnboarding, setupAuthTests, teardownAuthTests } from './auth.helpers.js'; @@ -300,12 +300,12 @@ export function setupSearchTestSuite() { beforeAll(async () => { // Set required environment variables for tests - process.env.BETTER_AUTH_SECRET = 'test-better-auth-secret-test-better-auth-secret-1'; - process.env.JWT_SECRET = 'test-jwt-secret-test-jwt-secret-1'; - process.env.SESSION_SECRET = 'test-session-secret-test-session-secret-1'; - process.env.JWT_EXPIRY = '604800'; - process.env.FRONTEND_URL = 'http://localhost:5173'; - process.env.LOG_LEVEL = 'silent'; + vi.stubEnv('BETTER_AUTH_SECRET', 'test-better-auth-secret-test-better-auth-secret-1'); + vi.stubEnv('JWT_SECRET', 'test-jwt-secret-test-jwt-secret-1'); + vi.stubEnv('SESSION_SECRET', 'test-session-secret-test-session-secret-1'); + vi.stubEnv('JWT_EXPIRY', '604800'); + vi.stubEnv('FRONTEND_URL', 'http://localhost:5173'); + vi.stubEnv('LOG_LEVEL', 'silent'); const authContext = await setupAuthTests(); @@ -338,13 +338,7 @@ export function setupSearchTestSuite() { afterAll(async () => { await teardownAuthTests(context); - delete process.env.BETTER_AUTH_SECRET; - delete process.env.JWT_SECRET; - delete process.env.SESSION_SECRET; - delete process.env.JWT_EXPIRY; - delete process.env.FRONTEND_URL; - delete process.env.DATABASE_URL; - delete process.env.LOG_LEVEL; + vi.unstubAllEnvs(); resetConfig(); }, 120000);