|
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { expect, test } from '@playwright/test'; |
| 4 | +import { getPort, killProcess, runDevCommand } from '../../utils/runCommands'; |
| 5 | + |
| 6 | +function getPackageVersion(name: string) { |
| 7 | + const pkgJsonPath = path.join( |
| 8 | + __dirname, |
| 9 | + 'node_modules', |
| 10 | + name, |
| 11 | + 'package.json', |
| 12 | + ); |
| 13 | + return JSON.parse(readFileSync(pkgJsonPath, 'utf-8')).version as string; |
| 14 | +} |
| 15 | + |
| 16 | +test.describe('React 18 test', async () => { |
| 17 | + let appPort: number; |
| 18 | + let app: Awaited<ReturnType<typeof runDevCommand>> | null; |
| 19 | + test.beforeAll(async () => { |
| 20 | + const appDir = __dirname; |
| 21 | + appPort = await getPort(); |
| 22 | + app = await runDevCommand(appDir, appPort); |
| 23 | + }); |
| 24 | + |
| 25 | + test.afterAll(async () => { |
| 26 | + if (app) { |
| 27 | + await killProcess(app); |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + test('Index page', async ({ page }) => { |
| 32 | + await page.goto(`http://localhost:${appPort}`); |
| 33 | + const h1 = page.locator('h1'); |
| 34 | + await expect(h1).toContainText('Hello world'); |
| 35 | + const body = page.locator('body'); |
| 36 | + await expect(body).toContainText( |
| 37 | + `react-router-dom ${getPackageVersion('react-router-dom')}`, |
| 38 | + ); |
| 39 | + await expect(body).toContainText(`react ${getPackageVersion('react')}`); |
| 40 | + }); |
| 41 | + |
| 42 | + test('404 page', async ({ page }) => { |
| 43 | + await page.goto(`http://localhost:${appPort}/404`, { |
| 44 | + waitUntil: 'networkidle', |
| 45 | + }); |
| 46 | + // find the 404 text in the page |
| 47 | + await expect(page.locator('body')).toContainText('404'); |
| 48 | + }); |
| 49 | +}); |
0 commit comments