diff --git a/test/parallel/test-fs-promises-file-handle-close.js b/test/parallel/test-fs-promises-file-handle-close.js index ee1ab50200b1dd..656df97a341088 100644 --- a/test/parallel/test-fs-promises-file-handle-close.js +++ b/test/parallel/test-fs-promises-file-handle-close.js @@ -27,13 +27,15 @@ async function doOpen() { return fh; } -// Perform the file open assignment within a block. -// When the block scope exits, the file handle will -// be eligible for garbage collection. -{ - doOpen().then(common.mustCall((fd) => { - assert.strictEqual(typeof fd, 'object'); - })); -} - -setTimeout(() => global.gc(), 10); +doOpen().then(common.mustCall((fd) => { + assert.strictEqual(typeof fd, 'object'); +})).then(common.mustCall(() => { + setImmediate(() => { + // The FileHandle should be out-of-scope and no longer accessed now. + global.gc(); + + // Wait an extra event loop turn, as the warning is emitted from the + // native layer in an unref()'ed setImmediate() callback. + setImmediate(common.mustCall()); + }); +}));