Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixed failing tests, added env mock
  • Loading branch information
waleedlatif1 committed Jan 2, 2026
commit 8696fecdd7955b4fa62462b77ee548fb4c8b9df3
5 changes: 5 additions & 0 deletions apps/sim/app/api/chat/[identifier]/otp/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ describe('Chat OTP API Route', () => {
}),
}))

vi.doMock('@/lib/core/config/env', async () => {
const { createEnvMock } = await import('@sim/testing')
return createEnvMock()
})

vi.doMock('zod', () => ({
z: {
object: vi.fn().mockReturnValue({
Expand Down
26 changes: 10 additions & 16 deletions apps/sim/app/api/chat/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,13 @@ describe('Chat API Route', () => {
}),
}))

vi.doMock('@/lib/core/config/env', () => ({
env: {
vi.doMock('@/lib/core/config/env', async () => {
const { createEnvMock } = await import('@sim/testing')
return createEnvMock({
NODE_ENV: 'development',
NEXT_PUBLIC_APP_URL: 'http://localhost:3000',
},
isTruthy: (value: string | boolean | number | undefined) =>
typeof value === 'string'
? value.toLowerCase() === 'true' || value === '1'
: Boolean(value),
getEnv: (variable: string) => process.env[variable],
}))
})
})

const validData = {
workflowId: 'workflow-123',
Expand Down Expand Up @@ -296,15 +292,13 @@ describe('Chat API Route', () => {
}),
}))

vi.doMock('@/lib/core/config/env', () => ({
env: {
vi.doMock('@/lib/core/config/env', async () => {
const { createEnvMock } = await import('@sim/testing')
return createEnvMock({
NODE_ENV: 'development',
NEXT_PUBLIC_APP_URL: 'http://localhost:3000',
},
isTruthy: (value: string | boolean | number | undefined) =>
typeof value === 'string' ? value === 'true' || value === '1' : Boolean(value),
getEnv: (variable: string) => process.env[variable],
}))
})
})

const validData = {
workflowId: 'workflow-123',
Expand Down
11 changes: 6 additions & 5 deletions apps/sim/app/api/copilot/api-keys/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ describe('Copilot API Keys API Route', () => {
SIM_AGENT_API_URL_DEFAULT: 'https://agent.sim.example.com',
}))

vi.doMock('@/lib/core/config/env', () => ({
env: {
SIM_AGENT_API_URL: null,
vi.doMock('@/lib/core/config/env', async () => {
const { createEnvMock } = await import('@sim/testing')
return createEnvMock({
SIM_AGENT_API_URL: undefined,
COPILOT_API_KEY: 'test-api-key',
},
}))
})
})
})

afterEach(() => {
Expand Down
11 changes: 6 additions & 5 deletions apps/sim/app/api/copilot/stats/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ describe('Copilot Stats API Route', () => {
SIM_AGENT_API_URL_DEFAULT: 'https://agent.sim.example.com',
}))

vi.doMock('@/lib/core/config/env', () => ({
env: {
SIM_AGENT_API_URL: null,
vi.doMock('@/lib/core/config/env', async () => {
const { createEnvMock } = await import('@sim/testing')
return createEnvMock({
SIM_AGENT_API_URL: undefined,
COPILOT_API_KEY: 'test-api-key',
},
}))
})
})
})

afterEach(() => {
Expand Down
12 changes: 4 additions & 8 deletions apps/sim/app/api/workspaces/invitations/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,10 @@ describe('Workspace Invitations API Route', () => {
WorkspaceInvitationEmail: vi.fn(),
}))

vi.doMock('@/lib/core/config/env', () => ({
env: {
RESEND_API_KEY: 'test-resend-key',
NEXT_PUBLIC_APP_URL: 'https://test.sim.ai',
FROM_EMAIL_ADDRESS: 'Sim <noreply@test.sim.ai>',
EMAIL_DOMAIN: 'test.sim.ai',
},
}))
vi.doMock('@/lib/core/config/env', async () => {
const { createEnvMock } = await import('@sim/testing')
return createEnvMock()
})

vi.doMock('@/lib/core/utils/urls', () => ({
getEmailDomain: vi.fn().mockReturnValue('sim.ai'),
Expand Down
9 changes: 5 additions & 4 deletions apps/sim/lib/uploads/providers/blob/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,16 @@ describe('Azure Blob Storage Client', () => {
toString: () => 'sv=2021-06-08&se=2023-01-01T00%3A00%3A00Z&sr=b&sp=r&sig=test',
})

vi.doMock('@/lib/core/config/env', () => ({
env: {
vi.doMock('@/lib/core/config/env', async () => {
const { createEnvMock } = await import('@sim/testing')
return createEnvMock({
AZURE_ACCOUNT_NAME: 'testaccount',
AZURE_ACCOUNT_KEY: 'testkey',
AZURE_CONNECTION_STRING:
'DefaultEndpointsProtocol=https;AccountName=testaccount;AccountKey=testkey;EndpointSuffix=core.windows.net',
AZURE_STORAGE_CONTAINER_NAME: 'testcontainer',
},
}))
})
})

vi.doMock('@sim/logger', () => ({
createLogger: vi.fn().mockReturnValue({
Expand Down
27 changes: 15 additions & 12 deletions apps/sim/lib/uploads/providers/s3/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ describe('S3 Client', () => {
getSignedUrl: mockGetSignedUrl,
}))

vi.doMock('@/lib/core/config/env', () => ({
env: {
vi.doMock('@/lib/core/config/env', async () => {
const { createEnvMock } = await import('@sim/testing')
return createEnvMock({
S3_BUCKET_NAME: 'test-bucket',
AWS_REGION: 'test-region',
AWS_ACCESS_KEY_ID: 'test-access-key',
AWS_SECRET_ACCESS_KEY: 'test-secret-key',
},
}))
})
})

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

describe('s3Client initialization', () => {
it('should initialize with correct configuration when credentials are available', async () => {
vi.doMock('@/lib/core/config/env', () => ({
env: {
vi.doMock('@/lib/core/config/env', async () => {
const { createEnvMock } = await import('@sim/testing')
return createEnvMock({
S3_BUCKET_NAME: 'test-bucket',
AWS_REGION: 'test-region',
AWS_ACCESS_KEY_ID: 'test-access-key',
AWS_SECRET_ACCESS_KEY: 'test-secret-key',
},
}))
})
})

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

it('should initialize without credentials when env vars are not available', async () => {
vi.doMock('@/lib/core/config/env', () => ({
env: {
vi.doMock('@/lib/core/config/env', async () => {
const { createEnvMock } = await import('@sim/testing')
return createEnvMock({
S3_BUCKET_NAME: 'test-bucket',
AWS_REGION: 'test-region',
AWS_ACCESS_KEY_ID: undefined,
AWS_SECRET_ACCESS_KEY: undefined,
},
}))
})
})

vi.doMock('@/lib/uploads/setup', () => ({
S3_CONFIG: {
Expand Down
4 changes: 4 additions & 0 deletions packages/testing/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ export * from './assertions'
export * from './builders'
export * from './factories'
export {
createEnvMock,
createMockDb,
createMockFetch,
createMockGetEnv,
createMockLogger,
createMockResponse,
createMockSocket,
createMockStorage,
databaseMock,
defaultMockEnv,
drizzleOrmMock,
envMock,
loggerMock,
type MockFetchResponse,
setupGlobalFetchMock,
Expand Down
67 changes: 67 additions & 0 deletions packages/testing/src/mocks/env.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { vi } from 'vitest'

/**
* Default mock environment values for testing
*/
export const defaultMockEnv = {
// Core
DATABASE_URL: 'postgresql://test:test@localhost:5432/test',
BETTER_AUTH_URL: 'https://test.sim.ai',
BETTER_AUTH_SECRET: 'test-secret-that-is-at-least-32-chars-long',
ENCRYPTION_KEY: 'test-encryption-key-32-chars-long!',
INTERNAL_API_SECRET: 'test-internal-api-secret-32-chars!',

// Email
RESEND_API_KEY: 'test-resend-key',
FROM_EMAIL_ADDRESS: 'Sim <noreply@test.sim.ai>',
EMAIL_DOMAIN: 'test.sim.ai',
PERSONAL_EMAIL_FROM: 'Test <test@test.sim.ai>',

// URLs
NEXT_PUBLIC_APP_URL: 'https://test.sim.ai',
}

/**
* Creates a mock getEnv function that returns values from the provided env object
*/
export function createMockGetEnv(envValues: Record<string, string | undefined> = defaultMockEnv) {
return vi.fn((key: string) => envValues[key])
}

/**
* Creates a complete env mock object for use with vi.doMock
*
* @example
* ```ts
* vi.doMock('@/lib/core/config/env', () => createEnvMock())
*
* // With custom values
* vi.doMock('@/lib/core/config/env', () => createEnvMock({
* NEXT_PUBLIC_APP_URL: 'https://custom.example.com',
* }))
* ```
*/
export function createEnvMock(overrides: Record<string, string | undefined> = {}) {
const envValues = { ...defaultMockEnv, ...overrides }

return {
env: envValues,
getEnv: createMockGetEnv(envValues),
isTruthy: (value: string | boolean | number | undefined) =>
typeof value === 'string' ? value.toLowerCase() === 'true' || value === '1' : Boolean(value),
isFalsy: (value: string | boolean | number | undefined) =>
typeof value === 'string'
? value.toLowerCase() === 'false' || value === '0'
: value === false,
}
}

/**
* Pre-configured env mock for direct use with vi.mock
*
* @example
* ```ts
* vi.mock('@/lib/core/config/env', () => envMock)
* ```
*/
export const envMock = createEnvMock()
2 changes: 2 additions & 0 deletions packages/testing/src/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export {
databaseMock,
drizzleOrmMock,
} from './database.mock'
// Env mocks
export { createEnvMock, createMockGetEnv, defaultMockEnv, envMock } from './env.mock'
// Fetch mocks
export {
createMockFetch,
Expand Down