|
1 | | -import { exec } from 'node:child_process'; |
2 | | -import { stripVTControlCharacters as stripAnsi } from 'node:util'; |
3 | | -import { expectPoll, rspackOnlyTest } from '@e2e/helper'; |
| 1 | +import { rspackOnlyTest, runCommand } from '@e2e/helper'; |
4 | 2 |
|
5 | 3 | rspackOnlyTest('should display shortcuts as expected in dev', async () => { |
6 | | - const devProcess = exec('node ./dev.mjs', { |
7 | | - cwd: __dirname, |
8 | | - }); |
9 | | - |
10 | | - let logs: string[] = []; |
11 | | - |
12 | | - devProcess.stdout?.on('data', (data) => { |
13 | | - const output = data.toString().trim(); |
14 | | - logs.push(stripAnsi(output)); |
15 | | - }); |
| 4 | + const { childProcess, expectLog, clearLogs, close } = runCommand( |
| 5 | + 'node ./dev.mjs', |
| 6 | + { |
| 7 | + cwd: __dirname, |
| 8 | + }, |
| 9 | + ); |
16 | 10 |
|
17 | 11 | // help |
18 | | - await expectPoll(() => |
19 | | - logs.some((log) => log.includes('press h + enter to show shortcuts')), |
20 | | - ).toBeTruthy(); |
21 | | - devProcess.stdin?.write('h\n'); |
22 | | - await expectPoll(() => |
23 | | - logs.some((log) => log.includes('u + enter show urls')), |
24 | | - ).toBeTruthy(); |
| 12 | + await expectLog('press h + enter to show shortcuts'); |
| 13 | + childProcess.stdin?.write('h\n'); |
| 14 | + await expectLog('u + enter show urls'); |
25 | 15 |
|
26 | 16 | // print urls |
27 | | - logs = []; |
28 | | - devProcess.stdin?.write('u\n'); |
29 | | - await expectPoll(() => |
30 | | - logs.some((log) => log.includes('➜ Local: http://localhost:')), |
31 | | - ).toBeTruthy(); |
| 17 | + clearLogs(); |
| 18 | + childProcess.stdin?.write('u\n'); |
| 19 | + await expectLog('➜ Local: http://localhost:'); |
32 | 20 |
|
33 | 21 | // restart server |
34 | | - logs = []; |
35 | | - devProcess.stdin?.write('r\n'); |
36 | | - await expectPoll(() => |
37 | | - logs.some((log) => log.includes('restarting server')), |
38 | | - ).toBeTruthy(); |
39 | | - await expectPoll(() => |
40 | | - logs.some((log) => log.includes('➜ Local: http://localhost:')), |
41 | | - ).toBeTruthy(); |
42 | | - |
43 | | - devProcess.kill(); |
| 22 | + clearLogs(); |
| 23 | + childProcess.stdin?.write('r\n'); |
| 24 | + await expectLog('restarting server'); |
| 25 | + await expectLog('➜ Local: http://localhost:'); |
| 26 | + |
| 27 | + close(); |
44 | 28 | }); |
45 | 29 |
|
46 | 30 | rspackOnlyTest('should display shortcuts as expected in preview', async () => { |
47 | | - const devProcess = exec('node ./preview.mjs', { |
48 | | - cwd: __dirname, |
49 | | - }); |
50 | | - |
51 | | - let logs: string[] = []; |
52 | | - |
53 | | - devProcess.stdout?.on('data', (data) => { |
54 | | - const output = data.toString().trim(); |
55 | | - logs.push(stripAnsi(output)); |
56 | | - }); |
| 31 | + const { childProcess, expectLog, clearLogs, close } = runCommand( |
| 32 | + 'node ./preview.mjs', |
| 33 | + { |
| 34 | + cwd: __dirname, |
| 35 | + }, |
| 36 | + ); |
57 | 37 |
|
58 | 38 | // help |
59 | | - await expectPoll(() => |
60 | | - logs.some((log) => log.includes('press h + enter to show shortcuts')), |
61 | | - ).toBeTruthy(); |
62 | | - devProcess.stdin?.write('h\n'); |
63 | | - await expectPoll(() => |
64 | | - logs.some((log) => log.includes('u + enter show urls')), |
65 | | - ).toBeTruthy(); |
| 39 | + await expectLog('press h + enter to show shortcuts'); |
| 40 | + childProcess.stdin?.write('h\n'); |
| 41 | + await expectLog('u + enter show urls'); |
66 | 42 |
|
67 | 43 | // print urls |
68 | | - logs = []; |
69 | | - devProcess.stdin?.write('u\n'); |
70 | | - await expectPoll(() => |
71 | | - logs.some((log) => log.includes('➜ Local: http://localhost:')), |
72 | | - ).toBeTruthy(); |
| 44 | + clearLogs(); |
| 45 | + childProcess.stdin?.write('u\n'); |
| 46 | + await expectLog('➜ Local: http://localhost:'); |
73 | 47 |
|
74 | | - devProcess.kill(); |
| 48 | + close(); |
75 | 49 | }); |
76 | 50 |
|
77 | 51 | rspackOnlyTest('should allow to custom shortcuts in dev', async () => { |
78 | | - const devProcess = exec('node ./devCustom.mjs', { |
79 | | - cwd: __dirname, |
80 | | - }); |
| 52 | + const { childProcess, expectLog, clearLogs, close } = runCommand( |
| 53 | + 'node ./devCustom.mjs', |
| 54 | + { |
| 55 | + cwd: __dirname, |
| 56 | + }, |
| 57 | + ); |
81 | 58 |
|
82 | | - let logs: string[] = []; |
| 59 | + await expectLog('press h + enter to show shortcuts'); |
83 | 60 |
|
84 | | - devProcess.stdout?.on('data', (data) => { |
85 | | - const output = data.toString().trim(); |
86 | | - logs.push(stripAnsi(output)); |
87 | | - }); |
| 61 | + clearLogs(); |
| 62 | + childProcess.stdin?.write('s\n'); |
| 63 | + await expectLog('hello world!'); |
88 | 64 |
|
89 | | - await expectPoll(() => |
90 | | - logs.some((log) => log.includes('press h + enter to show shortcuts')), |
91 | | - ).toBeTruthy(); |
92 | | - |
93 | | - logs = []; |
94 | | - devProcess.stdin?.write('s\n'); |
95 | | - await expectPoll(() => |
96 | | - logs.some((log) => log.includes('hello world!')), |
97 | | - ).toBeTruthy(); |
98 | | - |
99 | | - devProcess.kill(); |
| 65 | + close(); |
100 | 66 | }); |
101 | 67 |
|
102 | 68 | rspackOnlyTest('should allow to custom shortcuts in preview', async () => { |
103 | | - const devProcess = exec('node ./previewCustom.mjs', { |
104 | | - cwd: __dirname, |
105 | | - }); |
106 | | - |
107 | | - let logs: string[] = []; |
108 | | - |
109 | | - devProcess.stdout?.on('data', (data) => { |
110 | | - const output = data.toString().trim(); |
111 | | - logs.push(stripAnsi(output)); |
112 | | - }); |
| 69 | + const { childProcess, expectLog, clearLogs, close } = runCommand( |
| 70 | + 'node ./previewCustom.mjs', |
| 71 | + { |
| 72 | + cwd: __dirname, |
| 73 | + }, |
| 74 | + ); |
113 | 75 |
|
114 | 76 | // help |
115 | | - await expectPoll(() => |
116 | | - logs.some((log) => log.includes('press h + enter to show shortcuts')), |
117 | | - ).toBeTruthy(); |
| 77 | + await expectLog('press h + enter to show shortcuts'); |
118 | 78 |
|
119 | | - logs = []; |
120 | | - devProcess.stdin?.write('s\n'); |
121 | | - await expectPoll(() => |
122 | | - logs.some((log) => log.includes('hello world!')), |
123 | | - ).toBeTruthy(); |
| 79 | + clearLogs(); |
| 80 | + childProcess.stdin?.write('s\n'); |
| 81 | + await expectLog('hello world!'); |
124 | 82 |
|
125 | | - devProcess.kill(); |
| 83 | + close(); |
126 | 84 | }); |
0 commit comments