diff --git a/ext/node/polyfills/internal/timers.mjs b/ext/node/polyfills/internal/timers.mjs index 382c9e9b8ac8e8..0bfc57da0d1b05 100644 --- a/ext/node/polyfills/internal/timers.mjs +++ b/ext/node/polyfills/internal/timers.mjs @@ -341,11 +341,14 @@ export const runImmediates = core.runImmediates; export class Immediate { constructor(unboundCallback, ...args) { const asyncContext = getAsyncContext(); + // Match Node's `immediate._onImmediate(...argv)` invocation: the callback's + // `this` is the Immediate instance, not the global. + const self = this; const callback = (...argv) => { const oldContext = getAsyncContext(); try { setAsyncContext(asyncContext); - return ReflectApply(unboundCallback, globalThis, argv); + return ReflectApply(unboundCallback, self, argv); } finally { setAsyncContext(oldContext); } diff --git a/tests/node_compat/config.jsonc b/tests/node_compat/config.jsonc index be8d242b787157..18a4117c69eb44 100644 --- a/tests/node_compat/config.jsonc +++ b/tests/node_compat/config.jsonc @@ -3114,6 +3114,7 @@ "parallel/test-timers-same-timeout-wrong-list-deleted.js": {}, "parallel/test-timers-setimmediate-infinite-loop.js": {}, "parallel/test-timers-socket-timeout-removes-other-socket-unref-timer.js": {}, + "parallel/test-timers-this.js": {}, "parallel/test-timers-throw-when-cb-not-function.js": {}, "parallel/test-timers-timeout-promisified.js": {}, "parallel/test-timers-timeout-to-interval.js": {},