Compare commits

...

2 Commits

Author SHA1 Message Date
Claude Bot
a192015db7 test: hoist domain require to module scope
Address review feedback: move repeated require("domain") calls
to a single module-scope declaration.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-19 11:05:49 +00:00
Claude Bot
351b3048ab fix(domain): return values from bind(), intercept(), and run()
domain.bind() and domain.intercept() were missing return statements,
causing bound functions to always return undefined. domain.run() was
returning `this` instead of the function's return value. This broke
gulp 4.x async task detection (via async-done) since the returned
Promise from async functions was lost.

Closes #24287

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-19 10:27:40 +00:00
2 changed files with 53 additions and 4 deletions

View File

@@ -37,7 +37,7 @@ domain.createDomain = domain.create = function () {
return function () {
var args = Array.prototype.slice.$call(arguments);
try {
fn.$apply(null, args);
return fn.$apply(null, args);
} catch (err) {
emitError(err);
}
@@ -50,7 +50,7 @@ domain.createDomain = domain.create = function () {
} else {
var args = Array.prototype.slice.$call(arguments, 1);
try {
fn.$apply(null, args);
return fn.$apply(null, args);
} catch (err) {
emitError(err);
}
@@ -59,11 +59,10 @@ domain.createDomain = domain.create = function () {
};
d.run = function (fn) {
try {
fn();
return fn();
} catch (err) {
emitError(err);
}
return this;
};
d.dispose = function () {
this.removeAllListeners();

View File

@@ -0,0 +1,50 @@
import { expect, test } from "bun:test";
const domain = require("domain");
test("domain.bind() returns the original function's return value", () => {
const d = domain.create();
const bound = d.bind(() => 42);
expect(bound()).toBe(42);
});
test("domain.bind() returns the original async function's promise", async () => {
const d = domain.create();
const bound = d.bind(async () => "hello");
const result = bound();
expect(result).toBeInstanceOf(Promise);
expect(await result).toBe("hello");
});
test("domain.intercept() returns the original function's return value", () => {
const d = domain.create();
const intercepted = d.intercept((...args: any[]) => args.join(","));
expect(intercepted(null, "a", "b")).toBe("a,b");
});
test("domain.intercept() returns the original async function's promise", async () => {
const d = domain.create();
const intercepted = d.intercept(async () => "world");
const result = intercepted(null);
expect(result).toBeInstanceOf(Promise);
expect(await result).toBe("world");
});
test("domain.run() returns the function's return value", () => {
const d = domain.create();
const result = d.run(() => 99);
expect(result).toBe(99);
});
test("domain.run() returns the async function's promise", async () => {
const d = domain.create();
const result = d.run(async () => "done");
expect(result).toBeInstanceOf(Promise);
expect(await result).toBe("done");
});