Compare commits

...

3 Commits

Author SHA1 Message Date
Zack Radisic
c2855f7bef Merge branch 'main' into ma-ts-fix/19952 2025-06-03 14:33:29 -07:00
Zack Radisic
7f81fe3ac1 Update ConsoleObject.ts and tests 2025-06-03 14:29:14 -07:00
Mats Willemsen
82a91eea4d fix: console.trace should go to stderr 2025-05-29 18:10:37 +02:00
5 changed files with 38 additions and 12 deletions

View File

@@ -160,7 +160,7 @@ fn messageWithTypeAndLevel_(
else
Output.enable_ansi_colors_stdout;
var buffered_writer = if (level == .Warning or level == .Error)
var buffered_writer = if (level == .Warning or level == .Error or message_type == .Trace)
&console.error_writer
else
&console.writer;
@@ -218,7 +218,10 @@ fn messageWithTypeAndLevel_(
}
}
if (print_length > 0)
if (print_length > 0) {
if (message_type == .Trace) {
writer.writeAll("Trace: ") catch {};
}
try format2(
level,
global,
@@ -228,8 +231,8 @@ fn messageWithTypeAndLevel_(
Writer,
writer,
print_options,
)
else if (message_type == .Log) {
);
} else if (message_type == .Log) {
_ = console.writer.write("\n") catch 0;
console.writer.flush() catch {};
} else if (message_type != .Trace)

View File

@@ -531,6 +531,13 @@ export function createConsoleConstructor(console: typeof globalThis.console) {
};
}
class TraceError extends Error {
constructor(message: string) {
super(message);
this.name = "Trace";
}
}
const consoleMethods: any = {
log(...args) {
this[kWriteToConsole](kUseStdout, this[kFormatForStdout](args));
@@ -580,10 +587,7 @@ export function createConsoleConstructor(console: typeof globalThis.console) {
},
trace: function trace(...args) {
const err: Error = {
name: "Trace",
message: this[kFormatForStderr](args),
};
const err = new TraceError(this[kFormatForStderr](args));
Error.captureStackTrace(err, trace);
this.error(err.stack);
},

View File

@@ -63,6 +63,16 @@ describe("console.Console", () => {
expect(await outValue()).toBe("hello world!\n");
expect(await errValue()).toBe("uh oh!\n");
});
test("console.trace should output to stderr", async () => {
const [out, outValue] = writable();
const [err, errValue] = writable();
const c = new Console({ stdout: out, stderr: err });
c.trace("hello world!");
out.end();
err.end();
expect(await outValue()).toBe("");
expect(await errValue()).toStartWith("Trace: hello world!\n");
});
});
test("console._stdout", () => {

View File

@@ -119,7 +119,7 @@ Quote"Backslash
"Warning log
warn: console.warn an error
at <file>:56:14
at loadAndEvaluateModule (2:1)
at loadAndEvaluateModule (7:44)
52 | console.group("Different logs");
53 | console.log("Regular log");
@@ -130,7 +130,7 @@ Quote"Backslash
^
error: console.error an error
at <file>:57:15
at loadAndEvaluateModule (2:1)
at loadAndEvaluateModule (7:44)
41 | console.groupEnd(); // Extra
42 | console.groupEnd(); // Extra
@@ -142,12 +142,12 @@ error: console.error an error
NamedError: console.error a named error
at new NamedError (<file>:46:5)
at <file>:58:15
at loadAndEvaluateModule (2:1)
at loadAndEvaluateModule (7:44)
NamedError: console.warn a named error
at new NamedError (<file>:46:5)
at <file>:59:14
at loadAndEvaluateModule (2:1)
at loadAndEvaluateModule (7:44)
Error log"
`);

View File

@@ -0,0 +1,9 @@
import { bunExe } from "harness";
it("console.trace", async () => {
const { stdout, stderr, exitCode } = await Bun.$`${bunExe()} -e "console.trace('hello')"`.quiet();
expect(exitCode).toBe(0);
expect(stdout.toString()).toBeEmpty();
const stderr_string = stderr.toString("utf8");
expect(stderr_string).toStartWith("Trace: hello\n");
});