diff --git a/apps/ai-game-creator-shell/tests/unhandledRejectionBridge.test.ts b/apps/ai-game-creator-shell/tests/unhandledRejectionBridge.test.ts index 3022d8f4e..9b796227d 100644 --- a/apps/ai-game-creator-shell/tests/unhandledRejectionBridge.test.ts +++ b/apps/ai-game-creator-shell/tests/unhandledRejectionBridge.test.ts @@ -71,3 +71,18 @@ test('window 侧读过 reason 的拒绝不再交回原监听器', () => { expect(consumed).toEqual([reason]); expect(forwarded).toEqual([]); }); + +test('交回原监听器时 this 绑成 process,和 Node EventEmitter 一致', () => { + parkProcessListeners(); + const receivers: unknown[] = []; + const listener = function (this: unknown) { + receivers.push(this); + }; + processListeners.push(listener); + process.on('unhandledRejection', listener); + installed.push(installUnhandledRejectionBridge()); + + process.emit('unhandledRejection', new Error('需要交回'), Promise.resolve()); + + expect(receivers).toEqual([process]); +}); diff --git a/apps/ai-game-creator-shell/tests/unhandledRejectionBridge.ts b/apps/ai-game-creator-shell/tests/unhandledRejectionBridge.ts index c61cabea4..7e5a5aac6 100644 --- a/apps/ai-game-creator-shell/tests/unhandledRejectionBridge.ts +++ b/apps/ai-game-creator-shell/tests/unhandledRejectionBridge.ts @@ -27,8 +27,9 @@ function invokeRejectionListener( reason: unknown, promise: Promise, ) { - // once 包装必须调它包裹的原函数:脱离 emitter 直接调用包装会碰到 this 为空。 - (listener.listener ?? listener)(reason, promise); + // once 包装必须调它包裹的原函数;Node 的 EventEmitter 把 this 绑成 process, + // 这里用 Reflect.apply 对齐,别让依赖 this 的原监听器拿到 undefined。 + Reflect.apply(listener.listener ?? listener, process, [reason, promise]); } export function installUnhandledRejectionBridge() {