Skip to content

Commit

Permalink
Add health e2e test for db
Browse files Browse the repository at this point in the history
  • Loading branch information
albinma committed Aug 9, 2023
1 parent fee3913 commit 06882a6
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 17 deletions.
12 changes: 12 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
NODE_ENV=development

# Development
NEXT_PUBLIC_SITE_URL=http://localhost:3000

# Database
DB_USERNAME=yourusername
DB_PASSWORD=yourpassword
DB_HOST=cockroachdb_cluster
DB_PORT=26257
DB_NAME=panther_dev
DB_URL=postgresql://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=verify-full
15 changes: 0 additions & 15 deletions .env.sample

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"@nextui-org/react": "^2.0.2",
"@prisma/client": "5.1.1",
"@prisma/client": "^5.1.1",
"autoprefixer": "10.4.14",
"classnames": "^2.3.2",
"eslint": "8.46.0",
Expand Down
26 changes: 26 additions & 0 deletions src/app/[locale]/health/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import getURL from '@/utils/getURL';

async function getData(): Promise<{ status: string }> {
const res = await fetch(getURL('/api/health'));
// The return value is *not* serialized
// You can return Date, Map, Set, etc.

if (!res.ok) {
// This will activate the closest `error.js` Error Boundary
throw new Error('Failed to fetch data');
}

const data = await res.json();

return data as { status: string };
}

export default async function HealthPage(): Promise<JSX.Element> {
const data = await getData();
return (
<div className="flex min-h-screen flex-col items-center justify-between p-24 dark:bg-black dark:text-slate-50 transition-colors duration-150">
<h1>Health</h1>
<p data-testid="health-status">{data.status}</p>
</div>
);
}
17 changes: 17 additions & 0 deletions src/app/api/health/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { PrismaClient } from '@prisma/client';
import { NextResponse } from 'next/server';

const prisma = new PrismaClient();

export async function GET(): Promise<NextResponse> {
try {
const userCount = await prisma.user.count();
if (userCount <= 0) {
throw new Error('No users found, seed failed');
}

return NextResponse.json({ status: 'healthy' });
} catch (error) {
return NextResponse.json({ status: 'error', error });
}
}
8 changes: 8 additions & 0 deletions src/utils/getURL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const IS_SERVER = typeof window === 'undefined';
export default function getURL(path: string): string {
const baseURL = IS_SERVER
? process.env.NEXT_PUBLIC_SITE_URL
: window.location.origin;

return new URL(path, baseURL).toString();
}
8 changes: 8 additions & 0 deletions tests/e2e/health.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { test, expect } from '@playwright/test';

test('should navigate to health page and be healthy', async ({ page }) => {
await page.goto('/health');
await expect(page).toHaveURL('/health');
const p = page.getByTestId('health-status');
await expect(p).toHaveText('healthy');
});
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,7 @@
optionalDependencies:
fsevents "2.3.2"

"@prisma/client@5.1.1":
"@prisma/client@^5.1.1":
version "5.1.1"
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-5.1.1.tgz#ea2b0c8599bdb3f86d92e8df46fba795a744db01"
integrity sha512-fxcCeK5pMQGcgCqCrWsi+I2rpIbk0rAhdrN+ke7f34tIrgPwA68ensrpin+9+fZvuV2OtzHmuipwduSY6HswdA==
Expand Down

0 comments on commit 06882a6

Please sign in to comment.