Skip to content

Instantly share code, notes, and snippets.

@lukastaegert
Last active January 17, 2020 16:06
Show Gist options
  • Save lukastaegert/c09c8e39cb5e724c8bde52350629c843 to your computer and use it in GitHub Desktop.
Save lukastaegert/c09c8e39cb5e724c8bde52350629c843 to your computer and use it in GitHub Desktop.
{
"name": "chokidar-issue",
"scripts": {
"test": "node test.js",
"test-docker": "docker run -v `pwd`:/test -w /test rollupcabal/circleci-node-v10:latest node test.js"
},
"dependencies": {
"chokidar": "^3.3.1"
}
}
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const chokidar = require('chokidar');
const wait = () => new Promise(resolve => setTimeout(resolve, 500));
const file = path.resolve('test.txt');
fs.writeFileSync(file, 'Initial');
const events = [];
const watcher = chokidar
.watch([], {ignoreInitial: true, disableGlobbing: true})
.on('all', event => events.push(event))
.add(file);
return wait()
.then(() => {
fs.unlinkSync(file);
fs.writeFileSync(file, 'First');
})
.then(wait)
.then(() => {
console.log('Events after first update (should not be empty):', events);
assert.strictEqual(events[0], 'change');
events.length = 0;
// Unwatching and adding the file again on any change is a workaround
// watcher.unwatch(file);
// watcher.add(file);
})
.then(wait)
.then(() => {
fs.unlinkSync(file);
fs.writeFileSync(file, 'Second');
})
.then(wait)
.then(() => {
console.log('Events after second update (should not be empty):', events);
watcher.close();
if (events[0] !== 'change') {
console.error('Did not receive expected "change" event');
process.exit(1);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment