From 5f1a59cb41da19a06e700a768c2d79a665a1a8f0 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Sat, 29 Jan 2022 21:49:26 +0200 Subject: [PATCH] test: make fs watch test more stable PR-URL: https://github.com/nodejs/node/pull/41715 Reviewed-By: Rich Trott Reviewed-By: Colin Ihrig Reviewed-By: Richard Lau Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- test/pummel/test-fs-watch-non-recursive.js | 23 +++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/test/pummel/test-fs-watch-non-recursive.js b/test/pummel/test-fs-watch-non-recursive.js index 49071a965f1737..e0ef20bef0b4be 100644 --- a/test/pummel/test-fs-watch-non-recursive.js +++ b/test/pummel/test-fs-watch-non-recursive.js @@ -28,7 +28,7 @@ if (common.isIBMi) { const path = require('path'); const fs = require('fs'); - +const assert = require('assert'); const tmpdir = require('../common/tmpdir'); tmpdir.refresh(); @@ -38,13 +38,14 @@ const filepath = path.join(testsubdir, 'watch.txt'); fs.mkdirSync(testsubdir, 0o700); -// Need a grace period, else the mkdirSync() above fires off an event. -setTimeout(function() { - const watcher = fs.watch(testDir, { persistent: true }, common.mustNotCall()); - setTimeout(function() { - fs.writeFileSync(filepath, 'test'); - }, 100); - setTimeout(function() { - watcher.close(); - }, 500); -}, 50); +const watcher = fs.watch(testDir, { persistent: true }, (event, filename) => { + // This function may be called with the directory depending on timing but + // must not be called with the file.. + assert.strictEqual(filename, 'testsubdir'); +}); +setTimeout(() => { + fs.writeFileSync(filepath, 'test'); +}, 100); +setTimeout(() => { + watcher.close(); +}, 500);