Skip to content

Commit 2910400

Browse files
committed
test: acquire browser rendering session in vitest globalSetup
1 parent 953373e commit 2910400

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { TestProject } from 'vitest/node';
2+
import { AcquireResponse } from '@cloudflare/playwright';
3+
4+
const testsServerUrl = process.env.TESTS_SERVER_URL ?? `http://localhost:8787`;
5+
6+
export default async function setup(project: TestProject) {
7+
const response = await fetch(`${testsServerUrl}/v1/acquire`, { method: 'GET' });
8+
const { sessionId } = await response.json() as AcquireResponse;
9+
project.provide('sessionId', sessionId);
10+
}
11+
12+
declare module 'vitest' {
13+
export interface ProvidedContext {
14+
sessionId: string;
15+
}
16+
}

packages/playwright-cloudflare/tests/src/proxyTests.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { TestEndPayload } from "@cloudflare/playwright/internal";
22
import { ManualPromise } from "./manualPromise";
33
import { WebSocket, MessageEvent } from "ws";
4+
import { inject } from 'vitest';
5+
6+
const sessionId = inject('sessionId');
47

58
type TestPayload = Pick<TestEndPayload, 'testId' | 'status' | 'errors'>;
69

@@ -15,6 +18,8 @@ export async function proxyTests(file: string) {
1518
const wsUrl = new URL(`${testsServerUrl}/${file}`.replace(/^http/, 'ws'));
1619
if (process.env.CI)
1720
wsUrl.searchParams.set('timeout', '30');
21+
if (sessionId)
22+
wsUrl.searchParams.set('sessionId', sessionId);
1823
websocket = new WebSocket(wsUrl);
1924
websocket.addEventListener('message', (ev: MessageEvent) => {
2025
const payload = JSON.parse(ev.data as string) as TestPayload;

packages/playwright-cloudflare/tests/src/testsServer.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default {
2222
async fetch(request: Request, env: Env): Promise<Response> {
2323
const url = new URL(request.url);
2424
const timeout = url.searchParams.has('timeout') ? parseInt(url.searchParams.get('timeout')!, 10) * 1000 : 10_000;
25+
const sessionId = url.searchParams.get('sessionId') ?? undefined;
2526
const file = url.pathname.substring(1);
2627

2728
const upgradeHeader = request.headers.get('Upgrade');
@@ -38,7 +39,7 @@ export default {
3839

3940
setCurrentEnv(env);
4041

41-
const browser = await connectBrowser(env);
42+
const browser = await connectBrowser(env, sessionId);
4243
setCurrentBrowser(browser);
4344
// we need to run browser rendering in dev remote mode,
4445
// so URL will ne a public one which is what we need
@@ -73,9 +74,11 @@ export default {
7374
}
7475
};
7576

76-
async function connectBrowser(env: Env) {
77-
const actives = await sessions(env.BROWSER);
78-
let [sessionId] = actives.filter(a => !a.connectionId).map(a => a.sessionId);
77+
async function connectBrowser(env: Env, sessionId?: string) {
78+
if (!sessionId) {
79+
const actives = await sessions(env.BROWSER);
80+
sessionId = actives.filter(a => !a.connectionId).map(a => a.sessionId)[0];
81+
}
7982

8083
if (!sessionId) {
8184
const sessionLimits = await limits(env.BROWSER);

packages/playwright-cloudflare/tests/vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export default defineConfig({
88
maxConcurrency: 1,
99
minWorkers: 1,
1010
maxWorkers: 1,
11+
globalSetup: './src/globalSetup.ts',
1112
},
1213
});

0 commit comments

Comments
 (0)