Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update expect().rejects to call async functions #16589

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/bun.js/test/expect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,40 @@ pub const Expect = struct {
/// Processes the async flags (resolves/rejects), waiting for the async value if needed.
/// If no flags, returns the original value
/// If either flag is set, waits for the result, and returns either it as a JSValue, or null if the expectation failed (in which case if silent is false, also throws a js exception)
pub fn processPromise(custom_label: bun.String, flags: Expect.Flags, globalThis: *JSGlobalObject, value: JSValue, matcher_name: anytype, matcher_params: anytype, comptime silent: bool) bun.JSError!JSValue {
pub fn processPromise(
custom_label: bun.String,
flags: Expect.Flags,
globalThis: *JSGlobalObject,
inputValue: JSValue,
matcher_name: anytype,
matcher_params: anytype,
comptime silent: bool,
) bun.JSError!JSValue {
var value = inputValue;

// If we have a .rejects or .resolves, and the user passed a function, call it and then use that value in the checks.
if (flags.promise != .none and value.isFunction()) {
const vm = globalThis.vm();
var return_value = JSValue.jsUndefined();

return_value = value.call(globalThis, .undefined, &.{}) catch |err| {
if (!silent) return globalThis.takeException(err);
return error.JSError;
};

if (return_value.asAnyPromise()) |promise| {
globalThis.bunVM().waitForPromise(promise);
promise.setHandled(vm);

value = return_value;
} else {
if (!silent and (flags.promise != .none)) {
return globalThis.throw("Expected a Promise from the async function but did not receive one", .{});
}
return return_value;
}
}

switch (flags.promise) {
inline .resolves, .rejects => |resolution| {
if (value.asAnyPromise()) |promise| {
Expand Down
3 changes: 3 additions & 0 deletions test/js/bun/test/expect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ describe("expect()", () => {
await expect(Promise.reject({ a: 1, b: 2 })).rejects.not.toMatchObject({ c: 1 });
await expect(Promise.reject(new Error("rejectMessage"))).rejects.toMatchObject({ message: "rejectMessage" });
await expect(Promise.reject(new Error())).rejects.toThrow();
await expect(async () => {
throw new Error("rejectMessage");
}).rejects.toThrow("rejectMessage");

// not receiving a rejected promise -> should throw
if (isBun) {
Expand Down