|
1 | 1 | /** |
2 | 2 | * @vitest-environment node |
3 | 3 | */ |
4 | | -import { describe, expect, it } from 'vitest' |
| 4 | +import { NextRequest } from 'next/server' |
| 5 | +import { afterEach, describe, expect, it } from 'vitest' |
5 | 6 | import { GET } from '@/app/api/health/route' |
6 | 7 |
|
| 8 | +afterEach(() => { |
| 9 | + process.env.APP_VERSION = '' |
| 10 | + process.env.NEXT_PUBLIC_APP_VERSION = '' |
| 11 | + process.env.GIT_SHA = '' |
| 12 | + process.env.VERCEL_GIT_COMMIT_SHA = '' |
| 13 | + process.env.COMMIT_SHA = '' |
| 14 | +}) |
| 15 | + |
7 | 16 | describe('GET /api/health', () => { |
8 | | - it('returns an ok status payload', async () => { |
9 | | - const response = await GET() |
| 17 | + it('returns status with runtime version metadata', async () => { |
| 18 | + process.env.APP_VERSION = 'v1.2.3' |
| 19 | + process.env.GIT_SHA = 'abc123' |
| 20 | + |
| 21 | + const response = await GET(new NextRequest('http://localhost/api/health')) |
| 22 | + |
| 23 | + expect(response.status).toBe(200) |
| 24 | + await expect(response.json()).resolves.toEqual({ |
| 25 | + status: 'ok', |
| 26 | + timestamp: expect.any(String), |
| 27 | + version: 'v1.2.3', |
| 28 | + commit: 'abc123', |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + it('falls back to the package version when runtime metadata is not provided', async () => { |
| 33 | + process.env.APP_VERSION = '' |
| 34 | + process.env.NEXT_PUBLIC_APP_VERSION = '' |
| 35 | + process.env.GIT_SHA = '' |
| 36 | + process.env.VERCEL_GIT_COMMIT_SHA = '' |
| 37 | + process.env.COMMIT_SHA = '' |
| 38 | + |
| 39 | + const response = await GET(new NextRequest('http://localhost/api/health')) |
10 | 40 |
|
11 | 41 | expect(response.status).toBe(200) |
12 | 42 | await expect(response.json()).resolves.toEqual({ |
13 | 43 | status: 'ok', |
14 | 44 | timestamp: expect.any(String), |
| 45 | + version: '0.1.0', |
| 46 | + commit: null, |
15 | 47 | }) |
16 | 48 | }) |
17 | 49 | }) |
0 commit comments