From e632941c520e9346fc706bb12d0434974c3f5a98 Mon Sep 17 00:00:00 2001 From: Ashcon Partovi Date: Wed, 31 May 2023 23:12:04 -0700 Subject: Small improvements to `bun test` (#3071) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Change status icon for skipped tests from "-" to "ยป" * Show file path instead of filename in `bun test` * Emit collapsable logs when running `bun test` in Github Actions https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines * Add fallback for test icons when emojis are not available * Only check for GITHUB_ACTIONS when running `bun test` * Emit error annotations when running `bun test` in Github Actions https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message * Remove ANSI output from Github annotation, it doesn't work * Remove outdated code from internal test runner * Add GithubActionFormatter to handle cases where error name or message is already ANSI * Fix formatting of test * Fix #3070 * Implement `bun test --run-todo` By default, `test.todo()` is no longer run, unless `--run-todo` is specified. * Fix test that relies on test.todo() being run * Support vitest-style test options * Disable GITHUB_ACTION in test harness * Add types for TestOptions * Fix bug where test.skip() actually ran * Implement `test.skipIf()` and `describe.skipIf()` * Implement `test.runIf()` * Move DiffFormatter to its own file * Fix bug where Bun.inspect() would emit a Github annotation * Introduce `bun test --only`, rename `--run-todo` to `--todo` * Implement `test.if()`, `describe.if()`, and other test fixes * Remove unwanted files from last commit * Fix last reference to --run-todo * Fix memory issues with printing github actions text * Update bindings.zig * Fix bug with `test.only()` * Remove debug test * Make the github annotations better * Improve .vscode/launch.json * Implement `expect().toBeNil()` * Remove .only() from test * Implement toBeBoolean(), toBeTrue(), toBeFalse() * Add lots of matchers * toBeNil() * toBeBoolean() * toBeTrue() * toBeFalse() * toBeNumber() * toBeInteger() * toBeFinite() * toBePositive() * toBeNegative() * toBeWithin() * toBeSymbol() * toBeFunction() * toBeDate() * toBeString() * toInclude() * toStartWith() * toEndWith() * Fix #3135 * Reduce verbosity of test * Fix snapshot bug --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- src/bun.js/javascript.zig | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src/bun.js/javascript.zig') diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index 5c158a4fb..e09b609cb 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -442,6 +442,8 @@ pub const VirtualMachine = struct { onUnhandledRejectionCtx: ?*anyopaque = null, unhandled_error_counter: usize = 0, + on_exception: ?*const OnException = null, + modules: ModuleLoader.AsyncModule.Queue = .{}, aggressive_garbage_collection: GCLevel = GCLevel.none, @@ -449,6 +451,16 @@ pub const VirtualMachine = struct { pub const OnUnhandledRejection = fn (*VirtualMachine, globalObject: *JSC.JSGlobalObject, JSC.JSValue) void; + pub const OnException = fn (*ZigException) void; + + pub fn setOnException(this: *VirtualMachine, callback: *const OnException) void { + this.on_exception = callback; + } + + pub fn clearOnException(this: *VirtualMachine) void { + this.on_exception = null; + } + const VMHolder = struct { pub threadlocal var vm: ?*VirtualMachine = null; }; @@ -2068,6 +2080,9 @@ pub const VirtualMachine = struct { var exception = exception_holder.zigException(); this.remapZigException(exception, error_instance, exception_list); this.had_errors = true; + defer if (this.on_exception) |cb| { + cb(exception); + }; var line_numbers = exception.stack.source_lines_numbers[0..exception.stack.source_lines_len]; var max_line: i32 = -1; @@ -2093,6 +2108,7 @@ pub const VirtualMachine = struct { var name = exception.name; const message = exception.message; + var did_print_name = false; if (source_lines.next()) |source| brk: { if (source.text.len == 0) break :brk; @@ -2228,7 +2244,7 @@ pub const VirtualMachine = struct { } if (show.syscall) { - try writer.print(comptime Output.prettyFmt("syscall: \"{s}\"\n", allow_ansi_color), .{exception.syscall}); + try writer.print(comptime Output.prettyFmt(" syscall: \"{s}\"\n", allow_ansi_color), .{exception.syscall}); add_extra_line = true; } @@ -2236,7 +2252,7 @@ pub const VirtualMachine = struct { if (show.syscall) { try writer.writeAll(" "); } - try writer.print(comptime Output.prettyFmt("errno: {d}\n", allow_ansi_color), .{exception.errno}); + try writer.print(comptime Output.prettyFmt(" errno: {d}\n", allow_ansi_color), .{exception.errno}); add_extra_line = true; } -- cgit v1.2.3