Skip to content

Commit

Permalink
Fix close tests
Browse files Browse the repository at this point in the history
Avoid nested async/await and Promise constructors that caused parts of
the old tests to continue running and interfere with subsequent tests
  • Loading branch information
zqianem committed Aug 19, 2022
1 parent 6147487 commit 62b19c9
Showing 1 changed file with 29 additions and 43 deletions.
72 changes: 29 additions & 43 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2176,51 +2176,37 @@ const runTests = (baseopts) => {

describe('close', () => {
it('should ignore further events on close', async () => {
return new Promise((resolve) => {
const spy = sinon.spy();
const watcher = chokidar_watch(currentDir, options);
watcher.once(EV_ADD, () => {
watcher.once(EV_ADD, async () => {
await watcher.on(EV_ADD, spy).close();
await delay(900);
await write(getFixturePath('add.txt'), dateNow());
spy.should.not.have.been.called;
resolve();
});
});
(async () => {
await waitForWatcher(watcher);
await delay(300);
await write(getFixturePath('add.txt'), 'hello');
await delay(300);
await fs_unlink(getFixturePath('add.txt'));
})();
});
const spy = sinon.spy();
const watcher = chokidar_watch(currentDir, options);
await waitForWatcher(watcher);

watcher.on(EV_ALL, spy);
await watcher.close();

await write(getFixturePath('add.txt'), dateNow());
await write(getFixturePath('add.txt'), 'hello');
await delay(300);
await fs_unlink(getFixturePath('add.txt'));

spy.should.not.have.been.called;
});
it('should not ignore further events on close with existing watchers', async () => {
return new Promise((resolve) => {
const watcher1 = chokidar_watch(currentDir);
const watcher2 = chokidar_watch(currentDir);
// The EV_ADD event should be called on the second watcher even if the first watcher is closed
watcher2.on(EV_ADD, () => {
watcher2.on(EV_ADD, (path) => {
if (path.endsWith('add.txt')) {
resolve();
}
})
});
(async () => {
await waitForWatcher(watcher1);
await waitForWatcher(watcher2);
// Watcher 1 is closed to ensure events only happen on watcher 2
await watcher1.close();
// Write a new file into the fixtures to test the EV_ADD event
await write(getFixturePath('add.txt'), 'hello');
// Ensures EV_ADD is called. Immediately removing the file causes it to be skipped
await delay(200);
await fs_unlink(getFixturePath('add.txt'));
})()
})
const spy = sinon.spy();
const watcher1 = chokidar_watch(currentDir);
const watcher2 = chokidar_watch(currentDir);
await waitForWatcher(watcher1);
await waitForWatcher(watcher2);

// The EV_ADD event should be called on the second watcher even if the first watcher is closed
watcher2.on(EV_ADD, spy);
await watcher1.close();

await write(getFixturePath('add.txt'), 'hello');
// Ensures EV_ADD is called. Immediately removing the file causes it to be skipped
await delay(200);
await fs_unlink(getFixturePath('add.txt'));

spy.should.have.been.calledWith(sinon.match('add.txt'));
});
it('should not prevent the process from exiting', async () => {
const scriptFile = getFixturePath('script.js');
Expand Down

0 comments on commit 62b19c9

Please sign in to comment.