summaryrefslogtreecommitdiff
path: root/src/lib/constants.test.ts
blob: 65fb4668dda2df24326bce71e9cc065e73a26306 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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/');
  });
});