teardown express server after tests

This commit is contained in:
Nemo Godebski-Pedersen 2025-03-26 14:55:08 +00:00
parent 4275e6879d
commit 64fc4566c2
6 changed files with 57 additions and 6 deletions

4
.gitignore vendored
View File

@ -50,7 +50,9 @@ public_key.pem
__snapshots__/
# Playwright auth
*.playwright/.auth
src/test/integration/playwright/.auth
src/test/integration/playwright/.auth/
**/playwright/.auth/
test-results/
playwright-report/
playwright-coverage/

View File

@ -10,6 +10,8 @@ export default defineConfig({
testDir: 'src/test/integration',
globalSetup: require.resolve('./src/test/integration/global.setup.ts'),
globalTeardown: require.resolve('./src/test/integration/global.teardown.ts'),
// Reporter to use
reporter: 'html',
@ -24,7 +26,12 @@ export default defineConfig({
projects: [
{
name: 'setup',
testMatch: /.*\.setup\.ts/
testMatch: /.*\.setup\.ts/,
teardown: 'teardown',
},
{
name: 'teardown',
testMatch: /.*\.teardown\.ts/
},
{
name: 'chromium',

View File

@ -1,6 +1,7 @@
import { spawn, ChildProcess } from 'child_process';
import type { FullConfig } from '@playwright/test';
import { setExpressServer } from './server-manager';
// Ports for our test servers
const EXPRESS_PORT = 8085;
@ -9,8 +10,7 @@ export const servers = {
expressUrl: `http://localhost:${EXPRESS_PORT}`,
}
let expressServer: ChildProcess;
export let expressServer: ChildProcess;
/**
* Starts the Express server before running tests
@ -26,6 +26,9 @@ async function globalSetup(config: FullConfig) {
env: { ...process.env, PORT: EXPRESS_PORT.toString() }
});
// Store in shared manager
setExpressServer(expressServer);
// Log server output for debugging
expressServer.stdout?.on('data', (data) => {
console.log(`Express server: ${data}`);

View File

@ -0,0 +1,15 @@
import { getExpressServer } from './server-manager'
async function globalTeardown() {
console.log('Tearing down global setup...')
const expressServer = getExpressServer()
// Kill the express server
if (expressServer) {
expressServer.kill('SIGTERM')
console.log('Express server killed')
}
}
export default globalTeardown

View File

@ -1,9 +1,21 @@
import { describe,beforeAll, beforeEach, afterAll, afterEach } from 'vitest';
import { useIntegrationTestHooks } from './global.setup';
import { chromium, Browser, Page, BrowserContext } from 'playwright';
import {test, expect} from '@playwright/test';
test.describe('Opey Integration Tests in API Explorer', () => {
test.beforeAll(async ({ page }) => {
// Open the chat widget
await page.goto('/');
const chatButton = await page.waitForSelector('.chat-widget-button');
await chatButton.click();
})
test('Opey is running', async ({ page }) => {
await page.goto('/');
})
})

View File

@ -0,0 +1,12 @@
import { ChildProcess } from 'child_process';
// Global state to store server instance
let expressServerProcess: ChildProcess | null = null;
export function setExpressServer(server: ChildProcess): void {
expressServerProcess = server;
}
export function getExpressServer(): ChildProcess | null {
return expressServerProcess;
}