diff --git a/lib/internal/fs/cp/cp-sync.js b/lib/internal/fs/cp/cp-sync.js index fe342f2dda19ec..9ec27f60662c92 100644 --- a/lib/internal/fs/cp/cp-sync.js +++ b/lib/internal/fs/cp/cp-sync.js @@ -46,7 +46,6 @@ const { parse, resolve, } = require('path'); -const { isPromise } = require('util/types'); function cpSyncFn(src, dest, opts) { // Warn about using preserveTimestamps on 32-bit node @@ -64,7 +63,7 @@ function cpSyncFn(src, dest, opts) { function checkPathsSync(src, dest, opts) { if (opts.filter) { const shouldCopy = opts.filter(src, dest); - if (isPromise(shouldCopy)) { + if (typeof shouldCopy !== 'boolean') { throw new ERR_INVALID_RETURN_VALUE('boolean', 'filter', shouldCopy); } if (!shouldCopy) return { __proto__: null, skipped: true }; diff --git a/test/parallel/test-fs-cp.mjs b/test/parallel/test-fs-cp.mjs index 63bc813ae226c7..27dee0ac085555 100644 --- a/test/parallel/test-fs-cp.mjs +++ b/test/parallel/test-fs-cp.mjs @@ -826,6 +826,22 @@ if (!isWindows) { cpSync(src, dest, opts); } +// It will throw error for invalid return value from filter +{ + const src = nextdir(); + const dest = nextdir(); + + const opts = { + filter: (path) => { + // Undefined is not a valid return value. + }, + }; + assert.throws( + () => cpSync(src, dest, opts), + { code: 'ERR_INVALID_RETURN_VALUE' } + ); +} + // Copy should not throw exception if dest is invalid but filtered out. { // Create dest as a file.