Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions src/viteDevProxy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @vitest-environment node
import { afterEach, describe, expect, test, vi } from 'vitest';
import type { UserConfig, UserConfigFnObject } from 'vite';
import viteConfig from '../vite.config';

const resolve = (env: Record<string, string>) => {
for (const [name, value] of Object.entries(env)) vi.stubEnv(name, value);
return (viteConfig as UserConfigFnObject)({ mode: 'development', command: 'serve' }) as UserConfig;
};

describe('local dev server proxy', () => {
afterEach(() => vi.unstubAllEnvs());

test('upgrades market-data WebSockets through the backend target', () => {
const proxy = resolve({ VITE_REAL_API_TARGET: 'http://backend-api:8080' }).server?.proxy;
// marketData.ts opens /ws/v1/market-data/prices on the page origin when
// VITE_API_BASE_URL is empty, so the dev server must forward the upgrade.
expect(proxy?.['/ws']).toEqual({ target: 'http://backend-api:8080', changeOrigin: true, ws: true });
expect(proxy?.['/api']).toEqual({ target: 'http://backend-api:8080', changeOrigin: true });
});

test('leaves the proxy off without a real API target', () => {
vi.stubEnv('VITE_REAL_API_TARGET', '');
expect(resolve({}).server).toBeUndefined();
});
});
3 changes: 3 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export default defineConfig(({ mode }) => {
} : {}),
'/api': { target: env.VITE_REAL_API_TARGET, changeOrigin: true },
'/actuator': { target: env.VITE_REAL_API_TARGET, changeOrigin: true },
// The market-data price stream opens /ws/v1/... on the page origin, so
// the upgrade has to reach backend-api through this server too.
'/ws': { target: env.VITE_REAL_API_TARGET, changeOrigin: true, ws: true },
},
} : undefined,
test: {
Expand Down
Loading