38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import { siteUrl } from '#web-runtime/lib/site-url';
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs();
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe('siteUrl', () => {
|
|
it('uses SITE_URL, trimming stray whitespace', () => {
|
|
vi.stubEnv('SITE_URL', ' https://crux.example.com ');
|
|
|
|
expect(siteUrl().origin).toBe('https://crux.example.com');
|
|
});
|
|
|
|
it('falls back to localhost for local development', () => {
|
|
vi.stubEnv('SITE_URL', '');
|
|
vi.stubEnv('NODE_ENV', 'development');
|
|
|
|
expect(siteUrl().origin).toBe('http://localhost:3000');
|
|
});
|
|
|
|
it('complains loudly when a production build has none, since the symptom is silent', () => {
|
|
const error = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
vi.stubEnv('SITE_URL', '');
|
|
vi.stubEnv('NODE_ENV', 'production');
|
|
|
|
expect(siteUrl().origin).toBe('http://localhost:3000');
|
|
expect(error).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it('throws on a malformed SITE_URL rather than building wrong canonicals', () => {
|
|
vi.stubEnv('SITE_URL', 'crux.example.com');
|
|
|
|
expect(() => siteUrl()).toThrow();
|
|
});
|
|
});
|