Skip to content

Commit f0b9f45

Browse files
authored
test: fix flaky watching file cases (#1345)
1 parent 0ee8416 commit f0b9f45

File tree

2 files changed

+50
-30
lines changed

2 files changed

+50
-30
lines changed

tests/integration/cli/build-watch/build.test.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { spawn } from 'node:child_process';
21
import path from 'node:path';
32
import { stripVTControlCharacters as stripAnsi } from 'node:util';
43
import { describe, expect, onTestFinished, test } from '@rstest/core';
54
import fse from 'fs-extra';
65
import {
6+
expectBuildEnd,
77
expectFile,
8-
expectFileChanges,
9-
rslibBinPath,
8+
expectFileWithContent,
109
runCli,
1110
} from 'test-helper';
1211

@@ -91,8 +90,9 @@ export default defineConfig({
9190
},
9291
},
9392
performance: {
93+
buildCache: false,
9494
printFileSize: false,
95-
}
95+
},
9696
});
9797
`,
9898
);
@@ -104,21 +104,19 @@ export default defineConfig({
104104
const distFooFile = path.join(__dirname, 'dist/esm/foo.js');
105105
const distFoo2File = path.join(__dirname, 'dist/esm/foo2.js');
106106

107-
const child = spawn(
108-
'node',
109-
[rslibBinPath, 'build', '--watch', '-c', tempConfigFile],
110-
{
111-
cwd: __dirname,
112-
stdio: 'pipe',
113-
shell: true,
114-
},
115-
);
116-
await expectFile(distIndexFile);
107+
const { child } = runCli(`build --watch -c ${tempConfigFile}`, {
108+
cwd: __dirname,
109+
});
110+
await expectFileWithContent(distIndexFile, 'index');
117111

118112
fse.outputFileSync(srcFooFile, `export const foo = 'foo';`);
113+
await expectBuildEnd(child);
114+
await expectFileWithContent(distFooFile, `'foo'`);
115+
119116
fse.outputFileSync(srcFoo2File, `export const foo2 = 'foo2';`);
120-
await expectFile(distFooFile);
121-
await expectFile(distFoo2File);
117+
await expectBuildEnd(child);
118+
await expectFileWithContent(distFoo2File, 'foo2');
119+
122120
const content1 = await fse.readFile(distFooFile, 'utf-8');
123121
expect(content1!).toMatchInlineSnapshot(`
124122
"const foo = 'foo';
@@ -137,12 +135,13 @@ export default defineConfig({
137135
fse.removeSync(srcIndexFile);
138136

139137
// change
140-
fse.outputFileSync(srcFooFile, `export const foo = 'foo1';`);
141-
await expectFileChanges(distFooFile, content1, 'foo1');
138+
fse.outputFileSync(srcFooFile, `export const foo1 = 'foo1';`);
139+
await expectBuildEnd(child);
140+
await expectFileWithContent(distFooFile, 'foo1');
142141
const content3 = await fse.readFile(distFooFile, 'utf-8');
143142
expect(content3!).toMatchInlineSnapshot(`
144-
"const foo = 'foo1';
145-
export { foo };
143+
"const foo1 = 'foo1';
144+
export { foo1 };
146145
"
147146
`);
148147

tests/scripts/helper.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import type { ChildProcess } from 'node:child_process';
12
import fs from 'node:fs';
23
import { platform } from 'node:os';
34
import { join } from 'node:path';
5+
import { stripVTControlCharacters as stripAnsi } from 'node:util';
46
import { expect } from '@playwright/test';
57
import fse from 'fs-extra';
68
import { convertPathToPattern, type GlobOptions, glob } from 'tinyglobby';
@@ -83,19 +85,38 @@ export const expectFile = (dir: string) =>
8385
expectPoll(() => fs.existsSync(dir)).toBeTruthy();
8486

8587
/**
86-
* Expect a file to be changed and contain newContent
88+
* Expect a file to exist and include specified content
8789
*/
88-
export const expectFileChanges = async (
89-
file: string,
90-
oldContent: string,
91-
newContent: string,
92-
) => {
93-
await expectPoll(() => {
90+
export const expectFileWithContent = (
91+
filePath: string,
92+
expectedContent: string,
93+
) =>
94+
expectPoll(() => {
9495
try {
95-
const content = fse.readFileSync(file, 'utf-8');
96-
return content !== oldContent && content.includes(newContent);
96+
if (!fs.existsSync(filePath)) {
97+
return false;
98+
}
99+
const content = fs.readFileSync(filePath, 'utf-8');
100+
return content.includes(expectedContent);
97101
} catch {
98102
return false;
99103
}
100104
}).toBeTruthy();
101-
};
105+
106+
/**
107+
* Expect log output from child process
108+
*/
109+
export const expectLog = (child: ChildProcess, log: string) =>
110+
new Promise<void>((resolve) => {
111+
const listener = (chunk: Buffer) => {
112+
if (stripAnsi(chunk.toString()).includes(log)) {
113+
resolve();
114+
}
115+
};
116+
117+
child.stdout?.on('data', listener);
118+
child.stderr?.on('data', listener);
119+
});
120+
121+
export const expectBuildEnd = (child: ChildProcess) =>
122+
expectLog(child, 'built in');

0 commit comments

Comments
 (0)