Skip to content

Commit 5be2237

Browse files
authored
test(e2e): fix flaky watching file cases (#5815)
1 parent cea08f1 commit 5be2237

File tree

6 files changed

+50
-34
lines changed

6 files changed

+50
-34
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3-
import { expectFile, rspackOnlyTest, runCli } from '@e2e/helper';
3+
import { expectFileWithContent, rspackOnlyTest, runCli } from '@e2e/helper';
44
import { expect } from '@playwright/test';
55
import fse, { remove } from 'fs-extra';
66

@@ -22,14 +22,12 @@ rspackOnlyTest(
2222
cwd: __dirname,
2323
});
2424

25-
await expectFile(distIndexFile);
26-
expect(fs.readFileSync(distIndexFile, 'utf-8')).toContain('foo1bar1');
25+
await expectFileWithContent(distIndexFile, 'foo1bar1');
2726
await remove(distIndexFile);
2827

2928
// should watch foo.js
3029
fse.outputFileSync(fooFile, `export const foo = 'foo2';`);
31-
await expectFile(distIndexFile);
32-
expect(fs.readFileSync(distIndexFile, 'utf-8')).toContain('foo2bar1');
30+
await expectFileWithContent(distIndexFile, 'foo2bar1');
3331

3432
// should not watch bar.js
3533
fse.outputFileSync(barFile, `export const bar = 'bar2';`);

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import fs from 'node:fs';
21
import path from 'node:path';
3-
import { expectFile, rspackOnlyTest, runCli } from '@e2e/helper';
4-
import { expect } from '@playwright/test';
2+
import { expectFileWithContent, rspackOnlyTest, runCli } from '@e2e/helper';
53
import fse, { remove } from 'fs-extra';
64

75
rspackOnlyTest('should support restart build when config changed', async () => {
@@ -27,8 +25,7 @@ rspackOnlyTest('should support restart build when config changed', async () => {
2725
cwd: __dirname,
2826
});
2927

30-
await expectFile(distIndexFile);
31-
expect(fs.readFileSync(distIndexFile, 'utf-8')).toContain('hello!');
28+
await expectFileWithContent(distIndexFile, 'hello!');
3229
await remove(distIndexFile);
3330

3431
fse.outputFileSync(
@@ -42,13 +39,11 @@ rspackOnlyTest('should support restart build when config changed', async () => {
4239
`,
4340
);
4441

45-
await expectFile(distIndexFile);
46-
expect(fs.readFileSync(distIndexFile, 'utf-8')).toContain('hello!');
42+
await expectFileWithContent(distIndexFile, 'hello!');
4743
await remove(distIndexFile);
4844

4945
fse.outputFileSync(indexFile, `console.log('hello2!');`);
50-
await expectFile(distIndexFile);
51-
expect(fs.readFileSync(distIndexFile, 'utf-8')).toContain('hello2!');
46+
await expectFileWithContent(distIndexFile, 'hello2!');
5247

5348
childProcess.kill();
5449
});
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import fs from 'node:fs';
21
import path from 'node:path';
3-
import { expectFile, rspackOnlyTest, runCli } from '@e2e/helper';
4-
import { expect } from '@playwright/test';
2+
import { expectFileWithContent, rspackOnlyTest, runCli } from '@e2e/helper';
53
import fse, { remove } from 'fs-extra';
64

75
rspackOnlyTest('should support watch mode for build command', async () => {
@@ -16,13 +14,11 @@ rspackOnlyTest('should support watch mode for build command', async () => {
1614
cwd: __dirname,
1715
});
1816

19-
await expectFile(distIndexFile);
20-
expect(fs.readFileSync(distIndexFile, 'utf-8')).toContain('hello!');
17+
await expectFileWithContent(distIndexFile, 'hello!');
2118
await remove(distIndexFile);
2219

2320
fse.outputFileSync(indexFile, `console.log('hello2!');`);
24-
await expectFile(distIndexFile);
25-
expect(fs.readFileSync(distIndexFile, 'utf-8')).toContain('hello2!');
21+
await expectFileWithContent(distIndexFile, 'hello2!');
2622

2723
childProcess.kill();
2824
});

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3-
import { expectFile, getRandomPort, rspackOnlyTest, runCli } from '@e2e/helper';
4-
import { expect } from '@playwright/test';
3+
import {
4+
expectFileWithContent,
5+
getRandomPort,
6+
rspackOnlyTest,
7+
runCli,
8+
} from '@e2e/helper';
59
import { remove } from 'fs-extra';
610

711
rspackOnlyTest(
@@ -38,14 +42,10 @@ rspackOnlyTest(
3842
},
3943
});
4044

41-
await expectFile(distIndex);
42-
expect(fs.readFileSync(distIndex, 'utf-8')).toContain('jack');
43-
45+
await expectFileWithContent(distIndex, 'jack');
4446
await remove(distIndex);
45-
4647
fs.writeFileSync(envLocalFile, 'PUBLIC_NAME=rose');
47-
await expectFile(distIndex);
48-
expect(fs.readFileSync(distIndex, 'utf-8')).toContain('rose');
48+
await expectFileWithContent(distIndex, 'rose');
4949

5050
devProcess.kill();
5151
},

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3-
import { expectFile, getRandomPort, rspackOnlyTest, runCli } from '@e2e/helper';
4-
import { expect, test } from '@playwright/test';
3+
import {
4+
expectFile,
5+
expectFileWithContent,
6+
getRandomPort,
7+
rspackOnlyTest,
8+
runCli,
9+
} from '@e2e/helper';
10+
import { test } from '@playwright/test';
511
import { remove } from 'fs-extra';
612

713
const dist = path.join(__dirname, 'dist');
@@ -32,14 +38,15 @@ rspackOnlyTest(
3238

3339
// the first build
3440
await expectFile(distIndexFile);
41+
await expectFileWithContent(tempOutputFile, '1');
3542

3643
await remove(tempOutputFile);
3744
// temp config changed and trigger rebuild
3845
fs.writeFileSync(extraConfigFile, 'export default 2;');
3946

4047
// rebuild and generate dist files
4148
await expectFile(tempOutputFile);
42-
expect(fs.readFileSync(tempOutputFile, 'utf-8')).toEqual('2');
49+
await expectFileWithContent(tempOutputFile, '2');
4350

4451
childProcess.kill();
4552
},
@@ -59,13 +66,14 @@ rspackOnlyTest(
5966
});
6067

6168
await expectFile(distIndexFile);
69+
await expectFileWithContent(tempOutputFile, '1');
6270

6371
await remove(distIndexFile);
6472
// temp config changed
6573
fs.writeFileSync(extraConfigFile, 'export default 2;');
6674

6775
await expectFile(tempOutputFile);
68-
expect(fs.readFileSync(tempOutputFile, 'utf-8')).toEqual('1');
76+
await expectFileWithContent(tempOutputFile, '1');
6977

7078
childProcess.kill();
7179
},
@@ -92,7 +100,7 @@ rspackOnlyTest(
92100
fs.writeFileSync(extraConfigFile, 'export default 2;');
93101

94102
await expectFile(tempOutputFile);
95-
expect(fs.readFileSync(tempOutputFile, 'utf-8')).toEqual('1');
103+
await expectFileWithContent(tempOutputFile, '1');
96104

97105
childProcess.kill();
98106
},

e2e/scripts/helper.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,25 @@ export const readDirContents = async (path: string, options?: GlobOptions) => {
7373
export const expectFile = (dir: string) =>
7474
expectPoll(() => fs.existsSync(dir)).toBeTruthy();
7575

76+
/**
77+
* Expect a file to exist and include specified content
78+
*/
79+
export const expectFileWithContent = (
80+
filePath: string,
81+
expectedContent: string,
82+
) =>
83+
expectPoll(() => {
84+
try {
85+
if (!fs.existsSync(filePath)) {
86+
return false;
87+
}
88+
const content = fs.readFileSync(filePath, 'utf-8');
89+
return content.includes(expectedContent);
90+
} catch {
91+
return false;
92+
}
93+
}).toBeTruthy();
94+
7695
export type ProxyConsoleOptions = {
7796
types?: ConsoleType | ConsoleType[];
7897
keepAnsi?: boolean;

0 commit comments

Comments
 (0)