diff options
| author | Bertrand Yuan <189593334+bertyuan@users.noreply.github.com> | 2026-03-26 00:19:31 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-03-26 00:19:31 +0800 |
| commit | f247a8c4a863ec430f4a705b5c493d652c8429bd (patch) | |
| tree | 71d0985970984c105582f6e3c370b254f38e9bbe /src/lib/constants.test.ts | |
| parent | f7a02fe0e112cf108fc5f22872f1efc077e99fe8 (diff) | |
| parent | cd3c4bc89c169616b38bdb7443bb4eb7571b020c (diff) | |
Merge pull request #12 from bertyuan/fix-vitestv1.1
Fix vitest
Diffstat (limited to 'src/lib/constants.test.ts')
| -rw-r--r-- | src/lib/constants.test.ts | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/lib/constants.test.ts b/src/lib/constants.test.ts new file mode 100644 index 0000000..65fb466 --- /dev/null +++ b/src/lib/constants.test.ts @@ -0,0 +1,54 @@ +import { afterAll, beforeEach, describe, expect, test, vi } from 'vitest'; + +const originalEnv = process.env; + +function setEnv(key: string, value?: string) { + if (value === undefined) { + delete process.env[key]; + return; + } + + process.env[key] = value; +} + +describe('constants', () => { + beforeEach(() => { + vi.resetModules(); + process.env = { ...originalEnv }; + }); + + afterAll(() => { + process.env = originalEnv; + }); + + test('uses localhost base url outside production', async () => { + setEnv('NODE_ENV', 'development'); + setEnv('VERCEL_PROJECT_PRODUCTION_URL', 'example.com'); + + const constants = await import('./constants'); + + expect(constants.isProduction).toBe(false); + expect(constants.baseUrl.href).toBe('http://localhost:3000/'); + }); + + test('uses vercel production url in production', async () => { + setEnv('NODE_ENV', 'production'); + setEnv('VERCEL_PROJECT_PRODUCTION_URL', 'blog.example.com'); + + const constants = await import('./constants'); + + expect(constants.isProduction).toBe(true); + expect(constants.baseUrl.href).toBe('https://blog.example.com/'); + }); + + test('falls back to localhost when production url is missing', async () => { + setEnv('NODE_ENV', 'production'); + setEnv('VERCEL_PROJECT_PRODUCTION_URL'); + + const constants = await import('./constants'); + + expect(constants.isProduction).toBe(true); + expect(constants.baseUrl.href).toBe('http://localhost:3000/'); + }); +}); + |
