node/test/parallel/test-watch-file-shared-dependency.mjs
Trivikram Kamat b734ff562f
test: tolerate duplicate watch change events
Allow test-watch-file-shared-dependency to observe multiple watcher
events while still asserting that the expected owner set is reached
exactly once.

Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com>
Assisted-by: openai:gpt-5.5
PR-URL: https://github.com/nodejs/node/pull/63937
Refs: https://github.com/nodejs/node/actions/runs/27462359210/job/81178399610
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
2026-06-21 08:47:04 +00:00

76 lines
2.5 KiB
JavaScript

// Flags: --expose-internals
import * as common from '../common/index.mjs';
import { describe, it } from 'node:test';
import assert from 'node:assert';
import tmpdir from '../common/tmpdir.js';
import watcher from 'internal/watch_mode/files_watcher';
import { writeFileSync } from 'node:fs';
if (common.isIBMi)
common.skip('IBMi does not support `fs.watch()`');
if (common.isAIX)
common.skip('folder watch capability is limited in AIX.');
const { FilesWatcher } = watcher;
tmpdir.refresh();
// Set up test files and dependencies
const fixtureContent = {
'dependency.js': 'module.exports = {};',
'test.js': 'require(\'./dependency.js\');',
'test-2.js': 'require(\'./dependency.js\');',
};
const fixturePaths = Object.fromEntries(Object.keys(fixtureContent)
.map((file) => [file, tmpdir.resolve(file)]));
Object.entries(fixtureContent)
.forEach(([file, content]) => writeFileSync(fixturePaths[file], content));
describe('watch file with shared dependency', () => {
it('should not remove shared dependencies when unfiltering an owner', (t, done) => {
const controller = new AbortController();
const watcher = new FilesWatcher({ signal: controller.signal });
const onExpectedOwners = common.mustCall(({ owners }) => {
assert.ok(owners.has(fixturePaths['test.js']));
assert.ok(owners.has(fixturePaths['test-2.js']));
controller.abort();
done();
});
function onChanged({ owners }) {
if (owners.size !== 2) return;
// If this code is never reached the test times out.
watcher.removeListener('changed', onChanged);
onExpectedOwners({ owners });
}
watcher.on('changed', onChanged);
watcher.filterFile(fixturePaths['test.js']);
watcher.filterFile(fixturePaths['test-2.js']);
watcher.filterFile(fixturePaths['dependency.js'], fixturePaths['test.js']);
watcher.filterFile(fixturePaths['dependency.js'], fixturePaths['test-2.js']);
watcher.unfilterFilesOwnedBy([fixturePaths['test.js']]);
watcher.filterFile(fixturePaths['test.js']);
watcher.filterFile(fixturePaths['dependency.js'], fixturePaths['test.js']);
if (common.isMacOS) {
// Do the write with a delay to ensure that the OS is ready to notify us.
setTimeout(() => {
writeFileSync(
fixturePaths['dependency.js'],
'module.exports = { modified: true };'
);
}, common.platformTimeout(200));
} else {
writeFileSync(
fixturePaths['dependency.js'],
'module.exports = { modified: true };'
);
}
});
});