Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 52 additions & 62 deletions tests/unit/test.watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,82 +23,72 @@ describe('watcher', () => {
watchFile,
touchedFile,
}: AssertWatchedParams = {}) => withTempDir(
(tmpDir) => {
async (tmpDir) => {
const artifactsDir = path.join(tmpDir.path(), 'web-ext-artifacts');
const someFile = path.join(tmpDir.path(), touchedFile);

if (watchFile) {
watchFile = watchFile.map((f) => path.join(tmpDir.path(), f));
}

var resolveChange;
let resolveChange;
const whenFilesChanged = new Promise((resolve) => {
resolveChange = resolve;
});
const onChange = sinon.spy(() => {
resolveChange();
});

let watchedFilePath;
let watchedDirPath;

return fs.writeFile(someFile, '<contents>')
.then(() => {
return onSourceChange({
sourceDir: tmpDir.path(),
watchFile,
artifactsDir,
onChange,
shouldWatchFile: () => true,
});
})
.then((watcher) => {
const {fileWatchers, directoryWatchers} = watcher;
let watchedFile;
let watchedDir;

if (fileWatchers?.size > 0) {
watchedFile = Array.from(fileWatchers.values())[0];
}

if (directoryWatchers?.size > 0) {
watchedDir = Array.from(directoryWatchers.values())[0];
}

watchedFilePath = watchedFile && watchedFile.path;
watchedDirPath = watchedDir && watchedDir.path;

return watcher;
})
.then((watcher) => {
return fs.utimes(someFile, Date.now() / 1000, Date.now() / 1000)
.then(() => watcher);
}).then((watcher) => {
const assertParams = {
onChange,
watchedFilePath,
watchedDirPath,
tmpDirPath: tmpDir.path(),
};

return Promise.race([
whenFilesChanged
.then(() => {
watcher.close();
// This delay seems to avoid stat errors from the watcher
// which can happen when the temp dir is deleted (presumably
// before watcher.close() has removed all listeners).
return new Promise((resolve) => {
setTimeout(resolve, 2, assertParams);
});
}),
// Time out if no files are changed
new Promise((resolve) => setTimeout(() => {
watcher.close();
resolve(assertParams);
}, 500)),
]);
});
await fs.writeFile(someFile, '<contents>');
const watcher = onSourceChange({
sourceDir: tmpDir.path(),
watchFile,
artifactsDir,
onChange,
shouldWatchFile: () => true,
});

// $FlowIgnore: retrieve internal Watchpack properties for testing purpose.
const {fileWatchers, directoryWatchers} = watcher;
let watchedFile;
let watchedDir;

if (fileWatchers?.size > 0) {
watchedFile = Array.from(fileWatchers.values())[0];
}

if (directoryWatchers?.size > 0) {
watchedDir = Array.from(directoryWatchers.values())[0];
}

const watchedFilePath = watchedFile && watchedFile.path;
const watchedDirPath = watchedDir && watchedDir.path;

await fs.utimes(someFile, Date.now() / 1000, Date.now() / 1000);
const assertParams = {
onChange,
watchedFilePath,
watchedDirPath,
tmpDirPath: tmpDir.path(),
};

return Promise.race([
whenFilesChanged
.then(() => {
watcher.close();
// This delay seems to avoid stat errors from the watcher
// which can happen when the temp dir is deleted (presumably
// before watcher.close() has removed all listeners).
return new Promise((resolve) => {
setTimeout(resolve, 2, assertParams);
});
}),
// Time out if no files are changed
new Promise((resolve) => setTimeout(() => {
watcher.close();
resolve(assertParams);
}, 500)),
]);
}
);

Expand Down