|
| 1 | +/** |
| 2 | + * Node.js E2E test for Vite dev server. |
| 3 | + * This test runs in Node.js environment (not browser mode) to: |
| 4 | + * 1. Start a Vite dev server programmatically |
| 5 | + * 2. Launch a Playwright browser |
| 6 | + * 3. Navigate to the dev server and verify the application works correctly |
| 7 | + * |
| 8 | + * This ensures our package is compatible with Vite configurations and works |
| 9 | + * correctly when served through a Vite dev server. |
| 10 | + */ |
| 11 | +import { dirname, resolve } from 'path'; |
| 12 | +import { chromium } from 'playwright'; |
| 13 | +import { fileURLToPath } from 'url'; |
| 14 | +import { createServer } from 'vite'; |
| 15 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 16 | + |
| 17 | +const __filename = fileURLToPath(import.meta.url); |
| 18 | +const __dirname = dirname(__filename); |
| 19 | +const projectRoot = resolve(__dirname, '..'); |
| 20 | + |
| 21 | +describe('Vite Dev Server E2E', () => { |
| 22 | + let server; |
| 23 | + let browser; |
| 24 | + let page; |
| 25 | + let serverUrl; |
| 26 | + |
| 27 | + beforeAll(async () => { |
| 28 | + // Start Vite dev server using the config file |
| 29 | + server = await createServer({ |
| 30 | + mode: 'development', |
| 31 | + configFile: resolve(projectRoot, 'vite.config.ts'), |
| 32 | + server: { |
| 33 | + port: 0 // Use random available port |
| 34 | + } |
| 35 | + }); |
| 36 | + await server.listen(); |
| 37 | + serverUrl = `http://localhost:${server.config.server.port}`; |
| 38 | + |
| 39 | + // Launch browser |
| 40 | + browser = await chromium.launch({ headless: true }); |
| 41 | + page = await browser.newPage(); |
| 42 | + }); |
| 43 | + |
| 44 | + afterAll(async () => { |
| 45 | + // Clean up |
| 46 | + if (page) await page.close(); |
| 47 | + if (browser) await browser.close(); |
| 48 | + if (server) await server.close(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should display the inserted customer "Fred" in the HTML list when running in dev mode', async () => { |
| 52 | + // Navigate to the dev server |
| 53 | + await page.goto(serverUrl); |
| 54 | + |
| 55 | + // Wait for the customer list to appear and contain "Fred" |
| 56 | + await page.waitForSelector('#customers-list', { timeout: 10000 }); |
| 57 | + |
| 58 | + // Wait for "Fred" to appear in the list |
| 59 | + await page.waitForFunction( |
| 60 | + () => { |
| 61 | + const customersList = document.getElementById('customers-list'); |
| 62 | + if (!customersList) return false; |
| 63 | + const listItems = customersList.querySelectorAll('li'); |
| 64 | + const customerNames = Array.from(listItems).map((li) => li.textContent); |
| 65 | + return customerNames.includes('Fred'); |
| 66 | + }, |
| 67 | + { timeout: 10000 } |
| 68 | + ); |
| 69 | + |
| 70 | + // Get all list items and verify |
| 71 | + const listItems = await page.locator('#customers-list li').all(); |
| 72 | + const customerNames = await Promise.all(listItems.map((li) => li.textContent())); |
| 73 | + |
| 74 | + // Verify "Fred" is in the list |
| 75 | + expect(customerNames).toContain('Fred'); |
| 76 | + }); |
| 77 | +}); |
0 commit comments