Skip to content

Commit

Permalink
test: wait for timer to avoid hanging tests
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j committed Sep 11, 2024
1 parent 962d959 commit 8feeb0a
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,27 @@ const chokidar_watch = (path = currentDir, opts) => {
return wt;
};

const waitFor = async (spies) => {
const waitFor = (spies) => {
if (spies.length === 0) throw new TypeError('SPies zero');
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error('timeout'));
}, TEST_TIMEOUT);
let checkTimer;
const isSpyReady = (spy) => {
if (Array.isArray(spy)) {
return spy[0].callCount >= spy[1];
}
return spy.callCount >= 1;
};
const checkSpiesReady = () => {
clearTimeout(checkTimer);

if (spies.every(isSpyReady)) {
clearTimeout(timeout);
resolve();
} else {
setTimeout(checkSpiesReady, 20);
checkTimer = setTimeout(checkSpiesReady, 20);
}
};
checkSpiesReady();
Expand Down Expand Up @@ -1399,10 +1402,13 @@ const runTests = (baseopts) => {
await fs_mkdir(testDir, PERM_ARR);
const watcher = chokidar_watch('.', options);

setTimeout(() => {
watcher.on(EV.ADD_DIR, spy);
fs_rename(testDir, renamedDir);
}, 1000);
await new Promise((resolve) => {
setTimeout(() => {
watcher.on(EV.ADD_DIR, spy);
fs_rename(testDir, renamedDir);
resolve();
}, 1000);
});

await waitFor([spy]);
spy.should.have.been.calledOnce;
Expand Down

0 comments on commit 8feeb0a

Please sign in to comment.