aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/test/jest.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-12 12:56:03 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-12 13:11:05 -0800
commit1a4685213b2eec3e5a817b4825c86a5ac8a25b31 (patch)
treeda8821a6abf2da487b24a792d8c052989dfa8053 /src/bun.js/test/jest.zig
parent76e6a178e3a0f1f12f42cd9980349e18145258f2 (diff)
downloadbun-1a4685213b2eec3e5a817b4825c86a5ac8a25b31.tar.gz
bun-1a4685213b2eec3e5a817b4825c86a5ac8a25b31.tar.zst
bun-1a4685213b2eec3e5a817b4825c86a5ac8a25b31.zip
[bun:test] Support `async` in `expect(async () => { await 1; }).toThrow()`
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|