Skip to content
Open
Show file tree
Hide file tree
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
40 changes: 40 additions & 0 deletions test/parallel/test-fs-promises-watch-ignore-function.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as common from '../common/index.mjs';
import { skipIfNoWatch } from '../common/watch.js';

skipIfNoWatch();

// if (common.isSunOS)
// common.skip('`fs.watch()` is not reliable on SunOS.');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be replaced with entries in test/parallel/parallel.status but I can't find a tracking issue.


const assert = await import('node:assert');
const path = await import('node:path');
const tmpdir = await import('../common/tmpdir.js');
const { watch } = await import('node:fs/promises');
const { writeFileSync } = await import('node:fs');

tmpdir.refresh();

const testDir = tmpdir.resolve();
const keepFile = 'visible.txt';
const ignoreFile = '.hidden';
const keepFilePath = path.join(testDir, keepFile);
const ignoreFilePath = path.join(testDir, ignoreFile);

const watcher = watch(testDir, {
ignore: (filename) => filename.startsWith('.'),
});

// Do the write with a delay to ensure that the OS is ready to notify us. See
// https://github.com/nodejs/node/issues/52601.
Comment on lines +27 to +28
Copy link
Member Author

@lpinca lpinca Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if the delay is needed only on macOS. If so, it would be better to conditionally delay writing using common.isMacOS to avoid wasting time on other platforms.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think on macOS at least, this is needed, otherwise it's known to cause flakes due to FSEvents coalescing. Not sure about other platforms.

setTimeout(() => {
writeFileSync(ignoreFilePath, 'ignored');
writeFileSync(keepFilePath, 'content');
}, common.platformTimeout(100));

for await (const { filename } of watcher) {
assert.notStrictEqual(filename, ignoreFile);

if (filename === keepFile) {
break;
}
}
38 changes: 38 additions & 0 deletions test/parallel/test-fs-promises-watch-ignore-glob.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as common from '../common/index.mjs';
import { skipIfNoWatch } from '../common/watch.js';

skipIfNoWatch();

// if (common.isSunOS)
// common.skip('`fs.watch()` is not reliable on SunOS.');

const assert = await import('node:assert');
const path = await import('node:path');
const tmpdir = await import('../common/tmpdir.js');
const { watch } = await import('node:fs/promises');
const { writeFileSync } = await import('node:fs');

tmpdir.refresh();

const testDir = tmpdir.resolve();
const keepFile = 'keep.txt';
const ignoreFile = 'ignore.log';
const keepFilePath = path.join(testDir, keepFile);
const ignoreFilePath = path.join(testDir, ignoreFile);

const watcher = watch(testDir, { ignore: '*.log' });

// Do the write with a delay to ensure that the OS is ready to notify us. See
// https://github.com/nodejs/node/issues/52601.
setTimeout(() => {
writeFileSync(ignoreFilePath, 'ignored');
writeFileSync(keepFilePath, 'content');
}, common.platformTimeout(100));

for await (const { filename } of watcher) {
assert.notStrictEqual(filename, ignoreFile);

if (filename === keepFile) {
break;
}
}
35 changes: 35 additions & 0 deletions test/parallel/test-fs-promises-watch-ignore-invalid.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import '../common/index.mjs';
import { skipIfNoWatch } from '../common/watch.js';

skipIfNoWatch();

const assert = await import('node:assert');
const { watch } = await import('node:fs/promises');

await assert.rejects(
async () => {
const watcher = watch('.', { ignore: 123 });
// eslint-disable-next-line no-unused-vars
for await (const _ of watcher) {
// Will throw before yielding
}
},
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
}
);

await assert.rejects(
async () => {
const watcher = watch('.', { ignore: '' });
// eslint-disable-next-line no-unused-vars
for await (const _ of watcher) {
// Will throw before yielding
}
},
{
code: 'ERR_INVALID_ARG_VALUE',
name: 'TypeError',
}
);
47 changes: 47 additions & 0 deletions test/parallel/test-fs-promises-watch-ignore-mixed.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as common from '../common/index.mjs';
import { skipIfNoWatch } from '../common/watch.js';

skipIfNoWatch();

// if (common.isSunOS)
// common.skip('`fs.watch()` is not reliable on SunOS.');

const assert = await import('node:assert');
const path = await import('node:path');
const tmpdir = await import('../common/tmpdir.js');
const { watch } = await import('node:fs/promises');
const { writeFileSync } = await import('node:fs');

tmpdir.refresh();

const testDir = tmpdir.resolve();
const keepFile = 'keep.txt';
const ignoreLog = 'debug.log';
const ignoreTmp = 'temp.tmp';
const keepFilePath = path.join(testDir, keepFile);
const ignoreLogPath = path.join(testDir, ignoreLog);
const ignoreTmpPath = path.join(testDir, ignoreTmp);

const watcher = watch(testDir, {
ignore: [
'*.log',
/\.tmp$/,
],
});

// Do the write with a delay to ensure that the OS is ready to notify us. See
// https://github.com/nodejs/node/issues/52601.
setTimeout(() => {
writeFileSync(ignoreLogPath, 'ignored');
writeFileSync(ignoreTmpPath, 'ignored');
writeFileSync(keepFilePath, 'content');
}, common.platformTimeout(100));

for await (const { filename } of watcher) {
assert.notStrictEqual(filename, ignoreLog);
assert.notStrictEqual(filename, ignoreTmp);

if (filename === keepFile) {
break;
}
}
38 changes: 38 additions & 0 deletions test/parallel/test-fs-promises-watch-ignore-regexp.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as common from '../common/index.mjs';
import { skipIfNoWatch } from '../common/watch.js';

skipIfNoWatch();

// if (common.isSunOS)
// common.skip('`fs.watch()` is not reliable on SunOS.');

const assert = await import('node:assert');
const path = await import('node:path');
const tmpdir = await import('../common/tmpdir.js');
const { watch } = await import('node:fs/promises');
const { writeFileSync } = await import('node:fs');

tmpdir.refresh();

const testDir = tmpdir.resolve();
const keepFile = 'keep.txt';
const ignoreFile = 'ignore.tmp';
const keepFilePath = path.join(testDir, keepFile);
const ignoreFilePath = path.join(testDir, ignoreFile);

const watcher = watch(testDir, { ignore: /\.tmp$/ });

// Do the write with a delay to ensure that the OS is ready to notify us. See
// https://github.com/nodejs/node/issues/52601.
setTimeout(() => {
writeFileSync(ignoreFilePath, 'ignored');
writeFileSync(keepFilePath, 'content');
}, common.platformTimeout(100));

for await (const { filename } of watcher) {
assert.notStrictEqual(filename, ignoreFile);

if (filename === keepFile) {
break;
}
}
41 changes: 41 additions & 0 deletions test/parallel/test-fs-watch-ignore-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const common = require('../common');
const { skipIfNoWatch } = require('../common/watch.js');

skipIfNoWatch();

// if (common.isSunOS)
// common.skip('`fs.watch()` is not reliable on SunOS.');

const assert = require('assert');
const path = require('path');
const fs = require('fs');

const tmpdir = require('../common/tmpdir');

tmpdir.refresh();

const testFileName = 'visible.txt';
const testFilePath = path.join(tmpdir.path, testFileName);
const ignoredFileName = '.hidden';
const ignoredFilePath = path.join(tmpdir.path, ignoredFileName);

const watcher = fs.watch(tmpdir.path, {
ignore: (filename) => filename.startsWith('.'),
});

watcher.on('change', common.mustCallAtLeast((event, filename) => {
assert.notStrictEqual(filename, ignoredFileName);

if (filename === testFileName) {
watcher.close();
}
}, 1));

// Do the write with a delay to ensure that the OS is ready to notify us. See
// https://github.com/nodejs/node/issues/52601.
setTimeout(() => {
fs.writeFileSync(ignoredFilePath, 'ignored');
fs.writeFileSync(testFilePath, 'content');
}, common.platformTimeout(100));
41 changes: 41 additions & 0 deletions test/parallel/test-fs-watch-ignore-glob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const common = require('../common');
const { skipIfNoWatch } = require('../common/watch.js');

skipIfNoWatch();

// if (common.isSunOS)
// common.skip('`fs.watch()` is not reliable on SunOS.');

const assert = require('assert');
const path = require('path');
const fs = require('fs');

const tmpdir = require('../common/tmpdir');

tmpdir.refresh();

const testFileName = 'file.txt';
const testFilePath = path.join(tmpdir.path, testFileName);
const ignoredFileName = 'file.log';
const ignoredFilePath = path.join(tmpdir.path, ignoredFileName);

const watcher = fs.watch(tmpdir.path, {
ignore: '*.log',
});

watcher.on('change', common.mustCallAtLeast((event, filename) => {
assert.notStrictEqual(filename, ignoredFileName);

if (filename === testFileName) {
watcher.close();
}
}, 1));

// Do the write with a delay to ensure that the OS is ready to notify us. See
// https://github.com/nodejs/node/issues/52601.
setTimeout(() => {
fs.writeFileSync(ignoredFilePath, 'ignored');
fs.writeFileSync(testFilePath, 'content');
}, common.platformTimeout(100));
41 changes: 41 additions & 0 deletions test/parallel/test-fs-watch-ignore-invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

require('../common');
const { skipIfNoWatch } = require('../common/watch.js');

skipIfNoWatch();

const assert = require('assert');
const fs = require('fs');

assert.throws(
() => fs.watch('.', { ignore: 123 }),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
}
);

assert.throws(
() => fs.watch('.', { ignore: '' }),
{
code: 'ERR_INVALID_ARG_VALUE',
name: 'TypeError',
}
);

assert.throws(
() => fs.watch('.', { ignore: [123] }),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
}
);

assert.throws(
() => fs.watch('.', { ignore: [''] }),
{
code: 'ERR_INVALID_ARG_VALUE',
name: 'TypeError',
}
);
53 changes: 53 additions & 0 deletions test/parallel/test-fs-watch-ignore-mixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

const common = require('../common');
const { skipIfNoWatch } = require('../common/watch.js');

skipIfNoWatch();

// if (common.isSunOS)
// common.skip('`fs.watch()` is not reliable on SunOS.');

const assert = require('assert');
const path = require('path');
const fs = require('fs');

const tmpdir = require('../common/tmpdir');

tmpdir.refresh();

const testFileName = 'keep.txt';
const testFilePath = path.join(tmpdir.path, testFileName);
const ignoredLogName = 'debug.log';
const ignoredLogPath = path.join(tmpdir.path, ignoredLogName);
const ignoredTmpName = 'temp.tmp';
const ignoredTmpPath = path.join(tmpdir.path, ignoredTmpName);
const ignoredHiddenName = '.secret';
const ignoredHiddenPath = path.join(tmpdir.path, ignoredHiddenName);

const watcher = fs.watch(tmpdir.path, {
ignore: [
'*.log',
/\.tmp$/,
(filename) => filename.startsWith('.'),
],
});

watcher.on('change', common.mustCallAtLeast((event, filename) => {
assert.notStrictEqual(filename, ignoredLogName);
assert.notStrictEqual(filename, ignoredTmpName);
assert.notStrictEqual(filename, ignoredHiddenName);

if (filename === testFileName) {
watcher.close();
}
}, 1));

// Do the write with a delay to ensure that the OS is ready to notify us. See
// https://github.com/nodejs/node/issues/52601.
setTimeout(() => {
fs.writeFileSync(ignoredLogPath, 'ignored');
fs.writeFileSync(ignoredTmpPath, 'ignored');
fs.writeFileSync(ignoredHiddenPath, 'ignored');
fs.writeFileSync(testFilePath, 'content');
}, common.platformTimeout(100));
Loading
Loading