Skip to content

Commit 8696fec

Browse files
committed
fixed failing tests, added env mock
1 parent 00f269e commit 8696fec

File tree

10 files changed

+124
-50
lines changed

10 files changed

+124
-50
lines changed

apps/sim/app/api/chat/[identifier]/otp/route.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ describe('Chat OTP API Route', () => {
156156
}),
157157
}))
158158

159+
vi.doMock('@/lib/core/config/env', async () => {
160+
const { createEnvMock } = await import('@sim/testing')
161+
return createEnvMock()
162+
})
163+
159164
vi.doMock('zod', () => ({
160165
z: {
161166
object: vi.fn().mockReturnValue({

apps/sim/app/api/chat/route.test.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,13 @@ describe('Chat API Route', () => {
249249
}),
250250
}))
251251

252-
vi.doMock('@/lib/core/config/env', () => ({
253-
env: {
252+
vi.doMock('@/lib/core/config/env', async () => {
253+
const { createEnvMock } = await import('@sim/testing')
254+
return createEnvMock({
254255
NODE_ENV: 'development',
255256
NEXT_PUBLIC_APP_URL: 'http://localhost:3000',
256-
},
257-
isTruthy: (value: string | boolean | number | undefined) =>
258-
typeof value === 'string'
259-
? value.toLowerCase() === 'true' || value === '1'
260-
: Boolean(value),
261-
getEnv: (variable: string) => process.env[variable],
262-
}))
257+
})
258+
})
263259

264260
const validData = {
265261
workflowId: 'workflow-123',
@@ -296,15 +292,13 @@ describe('Chat API Route', () => {
296292
}),
297293
}))
298294

299-
vi.doMock('@/lib/core/config/env', () => ({
300-
env: {
295+
vi.doMock('@/lib/core/config/env', async () => {
296+
const { createEnvMock } = await import('@sim/testing')
297+
return createEnvMock({
301298
NODE_ENV: 'development',
302299
NEXT_PUBLIC_APP_URL: 'http://localhost:3000',
303-
},
304-
isTruthy: (value: string | boolean | number | undefined) =>
305-
typeof value === 'string' ? value === 'true' || value === '1' : Boolean(value),
306-
getEnv: (variable: string) => process.env[variable],
307-
}))
300+
})
301+
})
308302

309303
const validData = {
310304
workflowId: 'workflow-123',

apps/sim/app/api/copilot/api-keys/route.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ describe('Copilot API Keys API Route', () => {
2121
SIM_AGENT_API_URL_DEFAULT: 'https://agent.sim.example.com',
2222
}))
2323

24-
vi.doMock('@/lib/core/config/env', () => ({
25-
env: {
26-
SIM_AGENT_API_URL: null,
24+
vi.doMock('@/lib/core/config/env', async () => {
25+
const { createEnvMock } = await import('@sim/testing')
26+
return createEnvMock({
27+
SIM_AGENT_API_URL: undefined,
2728
COPILOT_API_KEY: 'test-api-key',
28-
},
29-
}))
29+
})
30+
})
3031
})
3132

3233
afterEach(() => {

apps/sim/app/api/copilot/stats/route.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ describe('Copilot Stats API Route', () => {
4646
SIM_AGENT_API_URL_DEFAULT: 'https://agent.sim.example.com',
4747
}))
4848

49-
vi.doMock('@/lib/core/config/env', () => ({
50-
env: {
51-
SIM_AGENT_API_URL: null,
49+
vi.doMock('@/lib/core/config/env', async () => {
50+
const { createEnvMock } = await import('@sim/testing')
51+
return createEnvMock({
52+
SIM_AGENT_API_URL: undefined,
5253
COPILOT_API_KEY: 'test-api-key',
53-
},
54-
}))
54+
})
55+
})
5556
})
5657

5758
afterEach(() => {

apps/sim/app/api/workspaces/invitations/route.test.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,10 @@ describe('Workspace Invitations API Route', () => {
8787
WorkspaceInvitationEmail: vi.fn(),
8888
}))
8989

90-
vi.doMock('@/lib/core/config/env', () => ({
91-
env: {
92-
RESEND_API_KEY: 'test-resend-key',
93-
NEXT_PUBLIC_APP_URL: 'https://test.sim.ai',
94-
FROM_EMAIL_ADDRESS: 'Sim <noreply@test.sim.ai>',
95-
EMAIL_DOMAIN: 'test.sim.ai',
96-
},
97-
}))
90+
vi.doMock('@/lib/core/config/env', async () => {
91+
const { createEnvMock } = await import('@sim/testing')
92+
return createEnvMock()
93+
})
9894

9995
vi.doMock('@/lib/core/utils/urls', () => ({
10096
getEmailDomain: vi.fn().mockReturnValue('sim.ai'),

apps/sim/lib/uploads/providers/blob/client.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,16 @@ describe('Azure Blob Storage Client', () => {
5454
toString: () => 'sv=2021-06-08&se=2023-01-01T00%3A00%3A00Z&sr=b&sp=r&sig=test',
5555
})
5656

57-
vi.doMock('@/lib/core/config/env', () => ({
58-
env: {
57+
vi.doMock('@/lib/core/config/env', async () => {
58+
const { createEnvMock } = await import('@sim/testing')
59+
return createEnvMock({
5960
AZURE_ACCOUNT_NAME: 'testaccount',
6061
AZURE_ACCOUNT_KEY: 'testkey',
6162
AZURE_CONNECTION_STRING:
6263
'DefaultEndpointsProtocol=https;AccountName=testaccount;AccountKey=testkey;EndpointSuffix=core.windows.net',
6364
AZURE_STORAGE_CONTAINER_NAME: 'testcontainer',
64-
},
65-
}))
65+
})
66+
})
6667

6768
vi.doMock('@sim/logger', () => ({
6869
createLogger: vi.fn().mockReturnValue({

apps/sim/lib/uploads/providers/s3/client.test.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ describe('S3 Client', () => {
3131
getSignedUrl: mockGetSignedUrl,
3232
}))
3333

34-
vi.doMock('@/lib/core/config/env', () => ({
35-
env: {
34+
vi.doMock('@/lib/core/config/env', async () => {
35+
const { createEnvMock } = await import('@sim/testing')
36+
return createEnvMock({
3637
S3_BUCKET_NAME: 'test-bucket',
3738
AWS_REGION: 'test-region',
3839
AWS_ACCESS_KEY_ID: 'test-access-key',
3940
AWS_SECRET_ACCESS_KEY: 'test-secret-key',
40-
},
41-
}))
41+
})
42+
})
4243

4344
vi.doMock('@sim/logger', () => ({
4445
createLogger: vi.fn().mockReturnValue({
@@ -298,14 +299,15 @@ describe('S3 Client', () => {
298299

299300
describe('s3Client initialization', () => {
300301
it('should initialize with correct configuration when credentials are available', async () => {
301-
vi.doMock('@/lib/core/config/env', () => ({
302-
env: {
302+
vi.doMock('@/lib/core/config/env', async () => {
303+
const { createEnvMock } = await import('@sim/testing')
304+
return createEnvMock({
303305
S3_BUCKET_NAME: 'test-bucket',
304306
AWS_REGION: 'test-region',
305307
AWS_ACCESS_KEY_ID: 'test-access-key',
306308
AWS_SECRET_ACCESS_KEY: 'test-secret-key',
307-
},
308-
}))
309+
})
310+
})
309311

310312
vi.doMock('@/lib/uploads/setup', () => ({
311313
S3_CONFIG: {
@@ -331,14 +333,15 @@ describe('S3 Client', () => {
331333
})
332334

333335
it('should initialize without credentials when env vars are not available', async () => {
334-
vi.doMock('@/lib/core/config/env', () => ({
335-
env: {
336+
vi.doMock('@/lib/core/config/env', async () => {
337+
const { createEnvMock } = await import('@sim/testing')
338+
return createEnvMock({
336339
S3_BUCKET_NAME: 'test-bucket',
337340
AWS_REGION: 'test-region',
338341
AWS_ACCESS_KEY_ID: undefined,
339342
AWS_SECRET_ACCESS_KEY: undefined,
340-
},
341-
}))
343+
})
344+
})
342345

343346
vi.doMock('@/lib/uploads/setup', () => ({
344347
S3_CONFIG: {

packages/testing/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@ export * from './assertions'
4545
export * from './builders'
4646
export * from './factories'
4747
export {
48+
createEnvMock,
4849
createMockDb,
4950
createMockFetch,
51+
createMockGetEnv,
5052
createMockLogger,
5153
createMockResponse,
5254
createMockSocket,
5355
createMockStorage,
5456
databaseMock,
57+
defaultMockEnv,
5558
drizzleOrmMock,
59+
envMock,
5660
loggerMock,
5761
type MockFetchResponse,
5862
setupGlobalFetchMock,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { vi } from 'vitest'
2+
3+
/**
4+
* Default mock environment values for testing
5+
*/
6+
export const defaultMockEnv = {
7+
// Core
8+
DATABASE_URL: 'postgresql://test:test@localhost:5432/test',
9+
BETTER_AUTH_URL: 'https://test.sim.ai',
10+
BETTER_AUTH_SECRET: 'test-secret-that-is-at-least-32-chars-long',
11+
ENCRYPTION_KEY: 'test-encryption-key-32-chars-long!',
12+
INTERNAL_API_SECRET: 'test-internal-api-secret-32-chars!',
13+
14+
// Email
15+
RESEND_API_KEY: 'test-resend-key',
16+
FROM_EMAIL_ADDRESS: 'Sim <noreply@test.sim.ai>',
17+
EMAIL_DOMAIN: 'test.sim.ai',
18+
PERSONAL_EMAIL_FROM: 'Test <test@test.sim.ai>',
19+
20+
// URLs
21+
NEXT_PUBLIC_APP_URL: 'https://test.sim.ai',
22+
}
23+
24+
/**
25+
* Creates a mock getEnv function that returns values from the provided env object
26+
*/
27+
export function createMockGetEnv(envValues: Record<string, string | undefined> = defaultMockEnv) {
28+
return vi.fn((key: string) => envValues[key])
29+
}
30+
31+
/**
32+
* Creates a complete env mock object for use with vi.doMock
33+
*
34+
* @example
35+
* ```ts
36+
* vi.doMock('@/lib/core/config/env', () => createEnvMock())
37+
*
38+
* // With custom values
39+
* vi.doMock('@/lib/core/config/env', () => createEnvMock({
40+
* NEXT_PUBLIC_APP_URL: 'https://custom.example.com',
41+
* }))
42+
* ```
43+
*/
44+
export function createEnvMock(overrides: Record<string, string | undefined> = {}) {
45+
const envValues = { ...defaultMockEnv, ...overrides }
46+
47+
return {
48+
env: envValues,
49+
getEnv: createMockGetEnv(envValues),
50+
isTruthy: (value: string | boolean | number | undefined) =>
51+
typeof value === 'string' ? value.toLowerCase() === 'true' || value === '1' : Boolean(value),
52+
isFalsy: (value: string | boolean | number | undefined) =>
53+
typeof value === 'string'
54+
? value.toLowerCase() === 'false' || value === '0'
55+
: value === false,
56+
}
57+
}
58+
59+
/**
60+
* Pre-configured env mock for direct use with vi.mock
61+
*
62+
* @example
63+
* ```ts
64+
* vi.mock('@/lib/core/config/env', () => envMock)
65+
* ```
66+
*/
67+
export const envMock = createEnvMock()

packages/testing/src/mocks/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export {
2424
databaseMock,
2525
drizzleOrmMock,
2626
} from './database.mock'
27+
// Env mocks
28+
export { createEnvMock, createMockGetEnv, defaultMockEnv, envMock } from './env.mock'
2729
// Fetch mocks
2830
export {
2931
createMockFetch,

0 commit comments

Comments
 (0)