Skip to content

Commit

Permalink
Merge pull request #1242 from zqianem/fix/tests
Browse files Browse the repository at this point in the history
Fix some tests
  • Loading branch information
paulmillr committed Aug 19, 2022
2 parents 6147487 + 66ed188 commit 2654ca6
Showing 1 changed file with 35 additions and 46 deletions.
81 changes: 35 additions & 46 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2058,16 +2058,19 @@ const runTests = (baseopts) => {

const fixturesPathRel = sysPath.join(FIXTURES_PATH_REL, id, 'test-case-1040');
const linkPath = sysPath.join(fixturesPathRel, 'symlinkFolder');
await fs_mkdir(sysPath.resolve(linkPath), { recursive: true });
const packagesPath = sysPath.join(fixturesPathRel, 'packages');
await fs_mkdir(fixturesPathRel);
await fs_mkdir(linkPath);
await fs_mkdir(packagesPath);

// Init chokidar
const watcher = chokidar.watch([]);
const events = [];

// Add more than 10 folders to cap consolidateThreshhold
for (let i = 0 ; i < 20 ; i += 1) {
const folderPath = sysPath.join(fixturesPathRel, 'packages', `folder${i}`);
await fs_mkdir(sysPath.resolve(folderPath), { recursive: true });
const folderPath = sysPath.join(packagesPath, `folder${i}`);
await fs_mkdir(folderPath);
const filePath = sysPath.join(folderPath, `file${i}.js`);
await write(sysPath.resolve(filePath), 'file content');
const symlinkPath = sysPath.join(linkPath, `folder${i}`);
Expand Down Expand Up @@ -2176,51 +2179,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 2654ca6

Please sign in to comment.