File size: 1,249 Bytes
2276d59
 
 
 
 
 
 
 
 
 
 
 
65fc551
 
2276d59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65fc551
2276d59
 
 
 
 
 
 
 
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
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import app from '../../src/app.js';
import { Server } from 'http';

describe('API Integration (Express Layer)', () => {
  let server: Server;
  let baseUrl: string;

  beforeAll(() => {
    return new Promise((resolve) => {
      // ephemeral port
      server = app.listen(0, '127.0.0.1', () => {
        const address = server.address();
        const port = typeof address === 'string' ? 0 : address?.port || 0;
        baseUrl = `http://127.0.0.1:${port}`;
        resolve();
      });
    });
  });

  afterAll(() => {
    server.close();
  });

  it('GET /ping should return 200 pong', async () => {
    const res = await fetch(`${baseUrl}/ping`);
    expect(res.status).toBe(200);
    const text = await res.text();
    expect(text).toBe('pong');
  });

  it('GET /health should return 200 with status ok', async () => {
    const res = await fetch(`${baseUrl}/health`);
    expect(res.status).toBe(200);
    const json = await res.json();
    expect(json).toMatchObject({
      status: 'ok',
    });
  });

  it('GET /non-existent-route should return 404', async () => {
    const res = await fetch(`${baseUrl}/api/v1/invalid`);
    expect(res.status).toBe(404);
  });
});