diff options
author | 2022-02-13 16:05:47 -0800 | |
---|---|---|
committer | 2022-02-13 16:05:47 -0800 | |
commit | 6bf3ae5a188bd8412bf7aa16ca3e69db232fb68b (patch) | |
tree | b0ab6ec933837e2a84f7b35bc9b8986a7779f6e4 /src/javascript | |
parent | a6de6d630ac6e0d089ebd5d022c3650b8771306a (diff) | |
download | bun-6bf3ae5a188bd8412bf7aa16ca3e69db232fb68b.tar.gz bun-6bf3ae5a188bd8412bf7aa16ca3e69db232fb68b.tar.zst bun-6bf3ae5a188bd8412bf7aa16ca3e69db232fb68b.zip |
[bun test] Ensure throw doesn't get hidden
Diffstat (limited to 'src/javascript')
-rw-r--r-- | src/javascript/jsc/test/jest.zig | 54 |
1 files changed, 36 insertions, 18 deletions
diff --git a/src/javascript/jsc/test/jest.zig b/src/javascript/jsc/test/jest.zig index 1d933cafe..b5ebebd22 100644 --- a/src/javascript/jsc/test/jest.zig +++ b/src/javascript/jsc/test/jest.zig @@ -552,6 +552,7 @@ pub const TestScope = struct { parent: *DescribeScope, callback: js.JSValueRef, id: TestRunner.Test.ID = 0, + promise: ?*JSInternalPromise = null, pub const Class = NewClass(void, .{ .name = "test" }, .{ .call = call }, .{}); @@ -612,28 +613,45 @@ pub const TestScope = struct { this: *TestScope, ) Result { var vm = VirtualMachine.vm; + defer { + js.JSValueUnprotect(vm.global.ref(), this.callback); + this.callback = null; + } - var promise = JSC.JSInternalPromise.resolvedPromise( - vm.global, - js.JSObjectCallAsFunctionReturnValue(vm.global.ref(), this.callback, null, 0, null), - ); - defer js.JSValueUnprotect(vm.global.ref(), this.callback); - this.callback = null; + const initial_value = js.JSObjectCallAsFunctionReturnValue(vm.global.ref(), this.callback, null, 0, null); - while (promise.status(vm.global.vm()) == JSC.JSPromise.Status.Pending) { - vm.tick(); + if (initial_value.isException(vm.global.vm()) or initial_value.isError() or initial_value.isAggregateError(vm.global)) { + vm.defaultErrorHandler(initial_value, null); + return .{ .fail = this.counter.actual }; } - switch (promise.status(vm.global.vm())) { - .Rejected => { - vm.defaultErrorHandler(promise.result(vm.global.vm()), null); - return .{ .fail = this.counter.actual }; - }, - else => { - // don't care about the result - _ = promise.result(vm.global.vm()); - }, + + if (!initial_value.isUndefinedOrNull()) { + if (this.promise != null) { + return .{ .pending = .{} }; + } + + this.promise = JSC.JSInternalPromise.resolvedPromise(vm.global, initial_value); + defer { + this.promise = null; + } + + while (this.promise.?.status(vm.global.vm()) == JSC.JSPromise.Status.Pending) { + vm.tick(); + } + switch (this.promise.?.status(vm.global.vm())) { + .Rejected => { + vm.defaultErrorHandler(this.promise.?.result(vm.global.vm()), null); + return .{ .fail = this.counter.actual }; + }, + else => { + // don't care about the result + _ = this.promise.?.result(vm.global.vm()); + }, + } } + this.callback = null; + if (this.counter.expected > 0 and this.counter.expected < this.counter.actual) { Output.prettyErrorln("Test fail: {d} / {d} expectations\n (make this better!)", .{ this.counter.actual, @@ -789,7 +807,7 @@ pub const DescribeScope = struct { switch (result) { .pass => |count| Jest.runner.?.reportPass(test_id, count), .fail => |count| Jest.runner.?.reportFailure(test_id, source.path.text, tests[i].label, count), - .pending => unreachable, + .pending => @panic("Unexpected pending test"), } i += 1; |