Skip to content

Commit 56880cd

Browse files
committed
reduce reproduction code
1 parent 5ed6d08 commit 56880cd

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

packages/codegen/src/runner.test.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,37 @@ import { writeFile } from 'node:fs/promises';
33
import { join } from 'node:path';
44
import { platform } from 'node:process';
55
import chokidar from 'chokidar';
6-
import { describe, test, vi } from 'vitest';
6+
import { describe, test } from 'vitest';
77

88
async function sleep(ms: number): Promise<void> {
99
// eslint-disable-next-line no-promise-executor-return
1010
return new Promise((resolve) => setTimeout(resolve, ms));
1111
}
1212

13+
async function waitFor(fn: () => void) {
14+
return new Promise<void>((resolve, reject) => {
15+
let error: unknown = null;
16+
const intervalTimer = setInterval(() => {
17+
try {
18+
fn();
19+
clearInterval(intervalTimer);
20+
clearTimeout(timeoutTimer);
21+
resolve();
22+
} catch (e) {
23+
error = e;
24+
}
25+
}, 50);
26+
const timeoutTimer = setTimeout(() => {
27+
clearInterval(intervalTimer);
28+
if (error) {
29+
reject(new Error('Timeout waiting for condition', { cause: error }));
30+
} else {
31+
reject(new Error('unreachable'));
32+
}
33+
}, 1000);
34+
});
35+
}
36+
1337
describe('runCMKInWatchMode', () => {
1438
test('reports system error occurs during watching', async () => {
1539
const fixturePath = join(process.cwd(), 'fixtures');
@@ -43,7 +67,7 @@ describe('runCMKInWatchMode', () => {
4367

4468
console.log('update file');
4569
await writeFile(textFilePath, '1');
46-
await vi.waitFor(() => {
70+
await waitFor(() => {
4771
assert(globalThis.changeCount === 1, `Expected changeCount to be 1, but got ${globalThis.changeCount}`);
4872
});
4973

0 commit comments

Comments
 (0)