Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add basic test agent #2

Merged
merged 1 commit into from
May 21, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: Test

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion scripts/quick-start.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-check
import { AppRunner } from '../tests/runner/runner.js';
import { AppRunner } from '../tests/common/runner.js';

const runner = new AppRunner();
runner.startServer(3000);
Expand Down
21 changes: 11 additions & 10 deletions tests/basic.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { expect, test } from 'playwright/test';
// @ts-ignore
import { AppRunner } from './runner/runner.js';
import { TestAgent } from './common/agent.js';

test.describe('basic test', () => {
const runner = new AppRunner();
test.beforeAll(async () => {
await Promise.all([
runner.startServer(3000, 'test-basic'),
runner.startWeb(5173, 3000),
]);
});
const agent = new TestAgent();

test.beforeAll(() =>
agent.start({
name: 'test-basic',
webPort: 5173,
backendPort: 3000,
})
);

test('local server works', async ({ page }) => {
await page.goto('http://localhost:5173');
const title = await page.title();
expect(title).toBe('Note App');
});

test.afterAll(async () => await runner.stop());
test.afterAll(async () => await agent.stop());
});
23 changes: 23 additions & 0 deletions tests/common/agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @ts-ignore
import { AppRunner } from './runner.js';

interface StartOptions {
name: string;
webPort: number;
backendPort: number;
}

export class TestAgent {
runner = new AppRunner();

async start(options: StartOptions) {
await Promise.all([
this.runner.startServer(options.backendPort, options.name),
this.runner.startWeb(options.webPort, options.backendPort),
]);
}

async stop() {
await this.runner.stop();
}
}
16 changes: 8 additions & 8 deletions tests/runner/runner.js → tests/common/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { spawn } from 'child_process';
import readline from 'readline';

export class AppRunner {
/** @param {string} name */
constructor(name = '') {
this.environment = name;
constructor() {
this.serverProcess = null;
this.webProcess = null;
}
Expand Down Expand Up @@ -94,10 +92,12 @@ export class AppRunner {
}

_terminateProcess(process, name) {
return new Promise((resolve, reject) => {
process.on('close', () => resolve());
process.on('error', () => reject());
process.kill('SIGINT');
});
return /** @type {Promise<void>} */ (
new Promise((resolve, reject) => {
process.on('close', () => resolve());
process.on('error', () => reject());
process.kill('SIGINT');
})
);
}
}
Loading