MCPcopy Index your code
hub / github.com/nodejs/node / doWatchTest

Function doWatchTest

test/parallel/test-fs-watch.js:44–82  ·  view source on GitHub ↗
(testCase)

Source from the content-addressed store, hash-verified

42tmpdir.refresh();
43
44function doWatchTest(testCase) {
45 let interval;
46 const pathToWatch = testCase[testCase.field];
47 const watcher = fs.watch(pathToWatch);
48 watcher.on('error', (err) => {
49 if (interval) {
50 clearInterval(interval);
51 interval = null;
52 }
53 assert.fail(err);
54 });
55 watcher.on('close', common.mustCall(() => {
56 watcher.close(); // Closing a closed watcher should be a noop
57 }));
58 watcher.on('change', common.mustCall(function(eventType, argFilename) {
59 if (interval) {
60 clearInterval(interval);
61 interval = null;
62 }
63 if (common.isMacOS)
64 assert.strictEqual(['rename', 'change'].includes(eventType), true);
65 else
66 assert.strictEqual(eventType, 'change');
67 assert.strictEqual(argFilename, testCase.fileName);
68
69 watcher.close();
70
71 // We document that watchers cannot be used anymore when it's closed,
72 // here we turn the methods into noops instead of throwing
73 watcher.close(); // Closing a closed watcher should be a noop
74 }));
75
76 // Long content so it's actually flushed. toUpperCase so there's real change.
77 const content2 = Date.now() + testCase.fileName.toUpperCase().repeat(1e4);
78 interval = setInterval(() => {
79 fs.writeFileSync(testCase.filePath, '');
80 fs.writeFileSync(testCase.filePath, content2);
81 }, 100);
82}
83
84for (const testCase of cases) {
85 if (testCase.shouldSkip) continue;

Callers 1

test-fs-watch.jsFile · 0.85

Calls 9

clearIntervalFunction · 0.85
includesMethod · 0.80
nowMethod · 0.80
closeMethod · 0.65
setIntervalFunction · 0.50
watchMethod · 0.45
onMethod · 0.45
failMethod · 0.45
writeFileSyncMethod · 0.45

Tested by

no test coverage detected