Skip to content

Commit a2fb115

Browse files
committed
test(e2e): fix flaky CLI shortcuts cases
1 parent 369d44a commit a2fb115

7 files changed

Lines changed: 65 additions & 104 deletions

File tree

e2e/cases/alias/tsconfig-paths-reload/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ rspackOnlyTest(
2525
await fse.copy(join(__dirname, 'tsconfig.json'), tempConfig);
2626

2727
const port = await getRandomPort();
28-
const { childProcess, close } = runCli('dev', {
28+
const { close } = runCli('dev', {
2929
cwd: __dirname,
3030
env: {
3131
...process.env,

e2e/cases/cli/build-watch-options/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ rspackOnlyTest(
1818
await remove(distIndexFile);
1919
await fse.copy(srcDir, tempDir);
2020

21-
const { childProcess, close } = runCli('build --watch', {
21+
const { close } = runCli('build --watch', {
2222
cwd: __dirname,
2323
});
2424

e2e/cases/cli/build-watch-restart/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ rspackOnlyTest('should support restart build when config changed', async () => {
2121

2222
fse.outputFileSync(indexFile, `console.log('hello!');`);
2323

24-
const { childProcess, close } = runCli(`build --watch -c ${tempConfigFile}`, {
24+
const { close } = runCli(`build --watch -c ${tempConfigFile}`, {
2525
cwd: __dirname,
2626
});
2727

e2e/cases/cli/reload-config/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ rspackOnlyTest(
2929
};`,
3030
);
3131

32-
const { childProcess, close } = runCli('dev', {
32+
const { close } = runCli('dev', {
3333
cwd: __dirname,
3434
});
3535

Lines changed: 54 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,84 @@
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';
42

53
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+
);
1610

1711
// 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');
2515

2616
// 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:');
3220

3321
// 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();
4428
});
4529

4630
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+
);
5737

5838
// 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');
6642

6743
// 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:');
7347

74-
devProcess.kill();
48+
close();
7549
});
7650

7751
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+
);
8158

82-
let logs: string[] = [];
59+
await expectLog('press h + enter to show shortcuts');
8360

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!');
8864

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();
10066
});
10167

10268
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+
);
11375

11476
// 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');
11878

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!');
12482

125-
devProcess.kill();
83+
close();
12684
});

e2e/cases/cli/watch-files-array/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ rspackOnlyTest(
1616
await remove(dist);
1717
fs.writeFileSync(extraConfigFile, 'export default { foo: 1 };');
1818

19-
const { childProcess, close } = runCli('dev', {
19+
const { close } = runCli('dev', {
2020
cwd: __dirname,
2121
env: {
2222
...process.env,

e2e/scripts/shared.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import type {
1717
import { pluginSwc } from '@rsbuild/plugin-webpack-swc';
1818
import type { Page } from 'playwright';
1919
import {
20-
expectPoll,
2120
type ProxyConsoleOptions,
2221
proxyConsole,
2322
readDirContents,
@@ -373,8 +372,8 @@ export function runCliSync(command: string, options?: ExecSyncOptions) {
373372
return execSync(`node ${rsbuildBinPath} ${command}`, options);
374373
}
375374

376-
export function runCli(command: string, options?: ExecOptions) {
377-
const childProcess = exec(`node ${rsbuildBinPath} ${command}`, options);
375+
export function runCommand(command: string, options?: ExecOptions) {
376+
const childProcess = exec(command, options);
378377

379378
let logs: string[] = [];
380379
const logPatterns = new Set<{
@@ -442,3 +441,7 @@ export function runCli(command: string, options?: ExecOptions) {
442441
expectBuildEnd,
443442
};
444443
}
444+
445+
export function runCli(command: string, options?: ExecOptions) {
446+
return runCommand(`node ${rsbuildBinPath} ${command}`, options);
447+
}

0 commit comments

Comments
 (0)