|
1 |
| -import { assert, test, beforeAll, expect } from 'vitest'; |
| 1 | +/* eslint-disable no-console */ |
| 2 | +import { existsSync, readFileSync, writeFileSync } from 'fs'; |
| 3 | +import { join } from 'path'; |
| 4 | +import { assert, beforeAll, beforeEach, describe, expect, test } from 'vitest'; |
2 | 5 | import {
|
3 | 6 | assertHostUnused,
|
| 7 | + DEFAULT_TIMEOUT, |
4 | 8 | getPageHtml,
|
5 |
| - promisifiedTreeKill, |
6 | 9 | killAllRegisteredProcesses,
|
| 10 | + log, |
| 11 | + promisifiedTreeKill, |
7 | 12 | runCommandUntil,
|
8 | 13 | scaffoldQwikProject,
|
9 |
| - DEFAULT_TIMEOUT, |
| 14 | + type QwikProjectType, |
10 | 15 | } from '../utils';
|
11 |
| -import { existsSync, readFileSync, writeFileSync } from 'fs'; |
12 |
| -import { join } from 'path'; |
13 | 16 |
|
14 | 17 | let SERVE_PORT = 3535;
|
15 |
| -beforeAll(() => { |
16 |
| - const config = scaffoldQwikProject(); |
17 |
| - global.tmpDir = config.tmpDir; |
18 |
| - |
19 |
| - return async () => { |
20 |
| - await killAllRegisteredProcesses(); |
21 |
| - config.cleanupFn(); |
22 |
| - }; |
| 18 | +beforeEach(() => { |
| 19 | + // the port doesn't clear immediately after the previous test |
| 20 | + SERVE_PORT++; |
23 | 21 | });
|
| 22 | +for (const type of ['empty', 'playground'] as QwikProjectType[]) { |
| 23 | + describe(`template: ${type}`, () => { |
| 24 | + beforeAll(() => { |
| 25 | + console.log('================================================ scaffolding', type); |
| 26 | + const config = scaffoldQwikProject(type); |
| 27 | + global.tmpDir = config.tmpDir; |
24 | 28 |
|
25 |
| -test( |
26 |
| - 'Should serve the app in dev mode and update the content on hot reload', |
27 |
| - { timeout: DEFAULT_TIMEOUT }, |
28 |
| - async () => { |
29 |
| - const host = `http://localhost:${SERVE_PORT}/`; |
30 |
| - await assertHostUnused(host); |
31 |
| - const p = await runCommandUntil( |
32 |
| - `npm run dev -- --port ${SERVE_PORT}`, |
33 |
| - global.tmpDir, |
34 |
| - (output) => { |
35 |
| - return output.includes(host); |
36 |
| - } |
37 |
| - ); |
38 |
| - assert.equal(existsSync(global.tmpDir), true); |
| 29 | + return async () => { |
| 30 | + try { |
| 31 | + await killAllRegisteredProcesses(); |
| 32 | + } catch (e) { |
| 33 | + log(`Error during process cleanup: ${e.message}`); |
| 34 | + } |
| 35 | + config.cleanupFn(); |
| 36 | + }; |
| 37 | + }); |
39 | 38 |
|
40 |
| - await expectHtmlOnARootPage(host); |
| 39 | + if (type === 'playground') { |
| 40 | + test( |
| 41 | + 'Should serve the app in dev mode and update the content on hot reload', |
| 42 | + { timeout: DEFAULT_TIMEOUT }, |
| 43 | + async () => { |
| 44 | + const host = `http://localhost:${SERVE_PORT}/`; |
| 45 | + await assertHostUnused(host); |
| 46 | + const p = await runCommandUntil( |
| 47 | + `npm run dev -- --port ${SERVE_PORT}`, |
| 48 | + global.tmpDir, |
| 49 | + (output) => { |
| 50 | + return output.includes(host); |
| 51 | + } |
| 52 | + ); |
| 53 | + assert.equal(existsSync(global.tmpDir), true); |
41 | 54 |
|
42 |
| - await promisifiedTreeKill(p.pid!, 'SIGKILL'); |
43 |
| - } |
44 |
| -); |
| 55 | + await expectHtmlOnARootPage(host); |
45 | 56 |
|
46 |
| -test('Should preview the app', { timeout: DEFAULT_TIMEOUT }, async () => { |
47 |
| - // the port doesn't clear immediately after the previous test |
48 |
| - SERVE_PORT++; |
49 |
| - const host = `http://localhost:${SERVE_PORT}/`; |
50 |
| - await assertHostUnused(host); |
51 |
| - const p = await runCommandUntil( |
52 |
| - `npm run preview -- --port ${SERVE_PORT}`, |
53 |
| - global.tmpDir, |
54 |
| - (output) => { |
55 |
| - return output.includes(host); |
| 57 | + // Don't let process termination errors fail the test |
| 58 | + try { |
| 59 | + await promisifiedTreeKill(p.pid!, 'SIGKILL'); |
| 60 | + } catch (e) { |
| 61 | + log(`Error terminating dev server: ${e.message}`); |
| 62 | + } |
| 63 | + } |
| 64 | + ); |
56 | 65 | }
|
57 |
| - ); |
58 |
| - assert.equal(existsSync(global.tmpDir), true); |
59 | 66 |
|
60 |
| - await expectHtmlOnARootPage(host); |
| 67 | + test('Should preview the app', { timeout: DEFAULT_TIMEOUT }, async () => { |
| 68 | + const host = `http://localhost:${SERVE_PORT}/`; |
| 69 | + await assertHostUnused(host); |
61 | 70 |
|
62 |
| - await promisifiedTreeKill(p.pid!, 'SIGKILL'); |
63 |
| -}); |
| 71 | + // First build the app |
| 72 | + const buildProcess = await runCommandUntil(`npm run build`, global.tmpDir, (output) => { |
| 73 | + return output.includes('dist/build') || output.includes('built in'); |
| 74 | + }); |
| 75 | + |
| 76 | + try { |
| 77 | + await promisifiedTreeKill(buildProcess.pid!, 'SIGKILL'); |
| 78 | + } catch (e) { |
| 79 | + log(`Error terminating build process: ${e.message}`); |
| 80 | + } |
| 81 | + |
| 82 | + // Now run the preview |
| 83 | + const p = await runCommandUntil( |
| 84 | + `npm run preview -- --no-open --port ${SERVE_PORT}`, |
| 85 | + global.tmpDir, |
| 86 | + (output) => { |
| 87 | + return output.includes(host); |
| 88 | + } |
| 89 | + ); |
| 90 | + |
| 91 | + assert.equal(existsSync(global.tmpDir), true); |
| 92 | + |
| 93 | + // Wait a bit for the server to fully start |
| 94 | + await new Promise((resolve) => setTimeout(resolve, 2000)); |
| 95 | + |
| 96 | + const res = await fetch(host, { headers: { accept: 'text/html' } }).then((r) => r.text()); |
| 97 | + console.log('** res', res); |
| 98 | + |
| 99 | + // Check for the appropriate content based on template type |
| 100 | + if (type === 'playground') { |
| 101 | + expect(res).toContain('fantastic'); |
| 102 | + } else if (type === 'empty') { |
| 103 | + expect(res).toContain('Hi'); |
| 104 | + expect(res).toContain('qwik'); |
| 105 | + } |
| 106 | + |
| 107 | + try { |
| 108 | + await promisifiedTreeKill(p.pid!, 'SIGKILL'); |
| 109 | + } catch (e) { |
| 110 | + log(`Error terminating preview server: ${e.message}`); |
| 111 | + } |
| 112 | + }); |
| 113 | + }); |
| 114 | +} |
64 | 115 |
|
65 | 116 | async function expectHtmlOnARootPage(host: string) {
|
66 | 117 | expect((await getPageHtml(host)).querySelector('.container h1')?.textContent).toBe(
|
|
0 commit comments