|
| 1 | +import { debugChildProcess, getFreePort, test } from './utils.js'; |
| 2 | +import { fileURLToPath } from 'node:url'; |
| 3 | +import { cp, mkdir, rm } from 'node:fs/promises'; |
| 4 | +import { exec, execSync } from 'node:child_process'; |
| 5 | +import { expect, type Page } from '@playwright/test'; |
| 6 | +import crypto from 'node:crypto'; |
| 7 | +import waitPort from 'wait-port'; |
| 8 | + |
| 9 | +const cacheDir = fileURLToPath(new URL('./.cache', import.meta.url)); |
| 10 | +const exampleDir = fileURLToPath( |
| 11 | + new URL('../examples/07_router', import.meta.url), |
| 12 | +); |
| 13 | +const wakuDir = fileURLToPath(new URL('../packages/waku', import.meta.url)); |
| 14 | + |
| 15 | +async function testRouterExample(page: Page, port: number) { |
| 16 | + await waitPort({ |
| 17 | + port, |
| 18 | + }); |
| 19 | + |
| 20 | + await page.goto(`http://localhost:${port}`); |
| 21 | + await expect(page.getByRole('heading', { name: 'Home' })).toBeVisible(); |
| 22 | + |
| 23 | + await page.click("a[href='/foo']"); |
| 24 | + |
| 25 | + await expect(page.getByRole('heading', { name: 'Foo' })).toBeVisible(); |
| 26 | + |
| 27 | + await page.goto(`http://localhost:${port}/foo`); |
| 28 | + await expect(page.getByRole('heading', { name: 'Foo' })).toBeVisible(); |
| 29 | +} |
| 30 | + |
| 31 | +test.describe('07_router standalone', () => { |
| 32 | + const dirname = crypto.randomUUID(); |
| 33 | + test.beforeAll('copy code', async () => { |
| 34 | + await mkdir(cacheDir, { |
| 35 | + recursive: true, |
| 36 | + }); |
| 37 | + await cp(exampleDir, `${cacheDir}/${dirname}`, { recursive: true }); |
| 38 | + // cleanup node_modules and output |
| 39 | + await rm(`${cacheDir}/${dirname}/node_modules`, { |
| 40 | + recursive: true, |
| 41 | + force: true, |
| 42 | + }); |
| 43 | + await rm(`${cacheDir}/${dirname}/dist`, { recursive: true, force: true }); |
| 44 | + execSync('pnpm install', { |
| 45 | + cwd: `${cacheDir}/${dirname}`, |
| 46 | + stdio: 'inherit', |
| 47 | + }); |
| 48 | + // copy waku |
| 49 | + await cp(wakuDir, `${cacheDir}/${dirname}/node_modules/waku`, { |
| 50 | + recursive: true, |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + test('should prod work', async ({ page }) => { |
| 55 | + execSync('pnpm run build', { |
| 56 | + cwd: `${cacheDir}/${dirname}`, |
| 57 | + stdio: 'inherit', |
| 58 | + }); |
| 59 | + const port = await getFreePort(); |
| 60 | + const cp = exec('pnpm run start', { |
| 61 | + cwd: `${cacheDir}/${dirname}`, |
| 62 | + env: { |
| 63 | + ...process.env, |
| 64 | + PORT: `${port}`, |
| 65 | + }, |
| 66 | + }); |
| 67 | + debugChildProcess(cp); |
| 68 | + await testRouterExample(page, port); |
| 69 | + cp.kill(); |
| 70 | + }); |
| 71 | + |
| 72 | + test('should dev work', async ({ page }) => { |
| 73 | + const port = await getFreePort(); |
| 74 | + const cp = exec('pnpm run dev', { |
| 75 | + cwd: `${cacheDir}/${dirname}`, |
| 76 | + env: { |
| 77 | + ...process.env, |
| 78 | + PORT: `${port}`, |
| 79 | + }, |
| 80 | + }); |
| 81 | + debugChildProcess(cp); |
| 82 | + await testRouterExample(page, port); |
| 83 | + cp.kill(); |
| 84 | + }); |
| 85 | +}); |
0 commit comments