Skip to content

Commit 4815e02

Browse files
committed
chore: update
1 parent e0d81ee commit 4815e02

File tree

5 files changed

+31
-63
lines changed

5 files changed

+31
-63
lines changed

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { exec, spawn } from 'node:child_process';
22
import path from 'node:path';
33
import { after } from 'node:test';
44
import fse from 'fs-extra';
5-
import { awaitFileChanges, awaitFileExists } from 'test-helper';
5+
import { expectFile, expectFileChanges } from 'test-helper';
66
import { describe, expect, test } from 'vitest';
77

88
describe('build --watch command', async () => {
@@ -30,7 +30,7 @@ export default defineConfig({
3030
cwd: __dirname,
3131
});
3232

33-
await awaitFileExists(distEsmIndexFile);
33+
await expectFile(distEsmIndexFile);
3434

3535
fse.outputFileSync(
3636
tempConfigFile,
@@ -49,7 +49,7 @@ export default defineConfig({
4949
`,
5050
);
5151

52-
await awaitFileExists(dist1EsmIndexFile);
52+
await expectFile(dist1EsmIndexFile);
5353

5454
process.kill();
5555
});
@@ -103,12 +103,12 @@ export default defineConfig({
103103
shell: true,
104104
},
105105
);
106-
await awaitFileExists(distIndexFile);
106+
await expectFile(distIndexFile);
107107

108108
fse.outputFileSync(srcFooFile, `export const foo = 'foo';`);
109109
fse.outputFileSync(srcFoo2File, `export const foo2 = 'foo2';`);
110-
await awaitFileExists(distFooFile);
111-
await awaitFileExists(distFoo2File);
110+
await expectFile(distFooFile);
111+
await expectFile(distFoo2File);
112112
const content1 = await fse.readFile(distFooFile, 'utf-8');
113113
expect(content1!).toMatchInlineSnapshot(`
114114
"const foo = 'foo';
@@ -127,7 +127,7 @@ export default defineConfig({
127127
fse.removeSync(srcIndexFile);
128128

129129
// change
130-
const wait = await awaitFileChanges(distFooFile);
130+
const wait = await expectFileChanges(distFooFile);
131131
fse.outputFileSync(srcFooFile, `export const foo = 'foo1';`);
132132
await wait();
133133
const content3 = await fse.readFile(distFooFile, 'utf-8');

tests/integration/cli/mf/mf.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { describe } from 'node:test';
44
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
55
import { startMFDevServer } from '@rslib/core';
66
import fse, { existsSync } from 'fs-extra';
7-
import { awaitFileExists } from 'test-helper';
7+
import { expectFile } from 'test-helper';
88
import { expect, test } from 'vitest';
99

1010
describe('mf-dev', () => {
@@ -25,7 +25,7 @@ describe('mf-dev', () => {
2525
},
2626
});
2727

28-
await awaitFileExists(distPath);
28+
await expectFile(distPath);
2929

3030
const rspackConfigContent = await fse.readFile(rspackConfigFile, 'utf-8');
3131
expect(rspackConfigContent).toContain(`nodeEnv: 'development'`);
@@ -51,8 +51,8 @@ describe('mf-dev', () => {
5151
cwd: fixturePath,
5252
});
5353

54-
await awaitFileExists(distPath1);
55-
await awaitFileExists(distPath2);
54+
await expectFile(distPath1);
55+
await expectFile(distPath2);
5656

5757
expect(existsSync(distPath1)).toBe(true);
5858
expect(existsSync(distPath2)).toBe(true);

tests/integration/plugins/index.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { exec } from 'node:child_process';
22
import fs from 'node:fs';
33
import { join } from 'node:path';
4-
import { awaitFileExists, buildAndGetResults } from 'test-helper';
4+
import { buildAndGetResults, expectFile } from 'test-helper';
55
import { expect, test } from 'vitest';
66
import { distIndex } from './basic/rslib.config';
77
import { plugin1Path, plugin2Path } from './mf-dev/rslib.config';
@@ -20,8 +20,8 @@ test('should merge plugins correctly', async () => {
2020
cwd: fixturePath,
2121
});
2222

23-
await awaitFileExists(plugin1Path);
24-
await awaitFileExists(plugin2Path);
23+
await expectFile(plugin1Path);
24+
await expectFile(plugin2Path);
2525

2626
const content1 = fs.readFileSync(plugin1Path, 'utf-8');
2727
const content2 = fs.readFileSync(plugin2Path, 'utf-8');

tests/integration/server/index.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { exec } from 'node:child_process';
22
import { existsSync } from 'node:fs';
33
import { join } from 'node:path';
44
import fse from 'fs-extra';
5-
import { awaitFileExists, buildAndGetResults } from 'test-helper';
5+
import { buildAndGetResults, expectFile } from 'test-helper';
66
import { describe, expect, test } from 'vitest';
77

88
describe('server config', async () => {
@@ -32,7 +32,7 @@ describe('server config', async () => {
3232
},
3333
});
3434

35-
await awaitFileExists(doneFile);
35+
await expectFile(doneFile);
3636

3737
const rsbuildConfigContent = await fse.readFile(rsbuildConfigFile, 'utf-8');
3838
expect(rsbuildConfigContent).toContain('open: true');

tests/scripts/helper.ts

+15-47
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import fs from 'node:fs';
12
import { platform } from 'node:os';
23
import { join } from 'node:path';
4+
import { expect } from '@playwright/test';
35
import fse from 'fs-extra';
46
import { type GlobOptions, convertPathToPattern, glob } from 'tinyglobby';
57

@@ -62,57 +64,23 @@ export const proxyConsole = (
6264
};
6365
};
6466

65-
export const waitFor = async (
66-
fn: () => boolean,
67-
{
68-
maxChecks = 100,
69-
interval = 20,
70-
}: {
71-
maxChecks?: number;
72-
interval?: number;
73-
} = {},
74-
) => {
75-
let checks = 0;
76-
77-
while (checks < maxChecks) {
78-
if (fn()) {
79-
return true;
80-
}
81-
checks++;
82-
await new Promise((resolve) => setTimeout(resolve, interval));
83-
}
84-
85-
return false;
86-
};
87-
88-
export const awaitFileExists = async (dir: string) => {
89-
const result = await waitFor(() => fse.existsSync(dir), {
90-
interval: 50,
91-
maxChecks: 400,
67+
/**
68+
* A faster `expect.poll`
69+
*/
70+
export const expectPoll = (fn: () => boolean): any => {
71+
return expect.poll(fn, {
72+
intervals: [20, 30, 40, 50, 60, 70, 80, 90, 100],
9273
});
93-
if (!result) {
94-
throw new Error(`awaitFileExists failed: ${dir}`);
95-
}
9674
};
9775

98-
export const awaitFileChanges = async (file: string) => {
76+
export const expectFile = (dir: string) =>
77+
expectPoll(() => fs.existsSync(dir)).toBeTruthy();
78+
79+
export const expectFileChanges = async (file: string) => {
9980
const oldContent = await fse.readFile(file, 'utf-8');
10081
return async () => {
101-
const result = await waitFor(
102-
() => {
103-
try {
104-
return fse.readFileSync(file, 'utf-8') !== oldContent;
105-
} catch (e) {
106-
return false;
107-
}
108-
},
109-
{ interval: 50 },
110-
);
111-
112-
if (!result) {
113-
throw new Error(`awaitFileChanges failed: ${file}`);
114-
}
115-
116-
return result;
82+
await expectPoll(
83+
() => fse.readFileSync(file, 'utf-8') !== oldContent,
84+
).toBeTruthy();
11785
};
11886
};

0 commit comments

Comments
 (0)