aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/test/jest.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/bun.js/test/jest.zig')
-rw-r--r--src/bun.js/test/jest.zig45
1 files changed, 40 insertions, 5 deletions
diff --git a/src/bun.js/test/jest.zig b/src/bun.js/test/jest.zig
index 37c4c79cb..6ff8d90ff 100644
--- a/src/bun.js/test/jest.zig
+++ b/src/bun.js/test/jest.zig
@@ -1124,12 +1124,37 @@ pub const Expect = struct {
}
const not = this.op.contains(.not);
- const result_ = value.call(globalObject, &.{}).toError();
+
+ const result_: ?JSValue = brk: {
+ var vm = globalObject.bunVM();
+ var scope = vm.unhandledRejectionScope();
+ defer scope.apply(vm);
+ vm.onUnhandledRejection = &VirtualMachine.onQuietUnhandledRejectionHandler;
+ const return_value: JSValue = value.call(globalObject, &.{});
+
+ if (return_value.asAnyPromise()) |promise| {
+ globalObject.bunVM().waitForPromise(promise);
+ const promise_result = promise.result(globalObject.vm());
+
+ switch (promise.status(globalObject.vm())) {
+ .Fulfilled => {
+ break :brk null;
+ },
+ .Rejected => {
+ // since we know for sure it rejected, we should always return the error
+ break :brk promise_result.toError() orelse promise_result;
+ },
+ .Pending => unreachable,
+ }
+ }
+
+ break :brk return_value.toError();
+ };
+
const did_throw = result_ != null;
const matched_expectation = did_throw == !not;
- if (matched_expectation) return thisValue;
- if (expected_value.isEmptyOrUndefinedOrNull()) {
+ if (!matched_expectation) {
if (!not)
globalObject.throw("Expected function to throw", .{})
else {
@@ -1139,7 +1164,14 @@ pub const Expect = struct {
return .zero;
}
- const result = result_.?;
+
+ // If you throw a string, it's treated as the message of an Error
+ // If you are expected not to throw and you didn't throw, then you pass
+ // If you are expected to throw a specific message and you throw a different one, then you fail.
+ if (matched_expectation and (!expected_value.isCell() or not))
+ return thisValue;
+
+ const result = result_ orelse JSC.JSValue.jsUndefined();
const expected_error = expected_value.toError();
@@ -1148,7 +1180,10 @@ pub const Expect = struct {
if (expected_value.isString()) break :brk expected_value;
break :brk expected_error.?.get(globalObject, "message");
};
- const actual = result.get(globalObject, "message");
+ const actual: ?JSValue = if (result.isObject())
+ result.get(globalObject, "message")
+ else
+ null;
// TODO support partial match
const pass = brk: {
if (expected) |expected_message|