Compare commits

...

6 Commits

Author SHA1 Message Date
Jarred Sumner
a69e7d0437 Merge branch 'main' into pfg/issue-14076 2025-09-02 00:22:45 -07:00
autofix-ci[bot]
370af4a3ef [autofix.ci] apply automated fixes 2025-08-01 01:01:50 +00:00
pfg
0800a4cee5 fix 2025-07-31 17:59:19 -07:00
pfg
5277d38423 09687 2025-07-31 17:41:27 -07:00
pfg
eb3a6fbdd4 Merge branch 'main' into pfg/issue-14076 2025-07-31 17:35:10 -07:00
pfg
aef5c45739 Changes expect().resolves.toThrow behaviour to more closely match jest 2024-10-31 18:45:44 -07:00
3 changed files with 97 additions and 2 deletions

View File

@@ -543,8 +543,16 @@ pub const Expect = struct {
var return_value_from_function: JSValue = .zero;
if (!value.jsType().isFunction()) {
if (this.flags.promise != .none) {
return .{ value, return_value_from_function };
switch (this.flags.promise) {
.none => {},
.rejects => return .{ value, return_value_from_function },
.resolves => {
if (value.isAnyError()) {
return .{ value, return_value_from_function };
} else {
return .{ null, return_value_from_function };
}
},
}
return globalThis.throw("Expected value must be a function", .{});

View File

@@ -0,0 +1,43 @@
import { expect, test } from "bun:test";
test("resolves not toThrow", async () => {
await expect(Promise.resolve("hello, world")).resolves.not.toThrow();
await expect(
(async () => {
await expect(Promise.resolve("hello, world")).resolves.toThrow();
})(),
).rejects.toBeInstanceOf(Error);
await expect(
(async () => {
await expect(Promise.resolve(new Error("abc"))).resolves.not.toThrow("abc");
})(),
).rejects.toBeInstanceOf(Error);
await expect(Promise.resolve(new Error("abc"))).resolves.toThrow("abc");
await expect(Promise.reject(new Error("abc"))).rejects.toThrow("abc");
});
test("doesn't break rejects", () => {
expect(
(async () => {
throw new DOMException("123");
})(),
).rejects.toThrow("123");
});
test("doesn't break rejects null", () => {
expect(
(async () => {
throw null;
})(),
).rejects.toThrow();
});
test("resolves null doesn't throw", () => {
expect(
(async () => {
return null;
})(),
).resolves.not.toThrow();
});

View File

@@ -0,0 +1,44 @@
import { describe, expect, test } from "bun:test";
// https://github.com/oven-sh/bun/issues/9687
describe("issue #9687 - expect().resolves.not.toThrow() fails incorrectly", () => {
test("should not throw for async function that returns nothing", async () => {
async function nop() {}
await expect(nop()).resolves.not.toThrow();
});
test("should not throw for async function that returns a value", async () => {
async function returnsValue() {
return "hello";
}
await expect(returnsValue()).resolves.not.toThrow();
});
test("should not throw for Promise.resolve()", async () => {
await expect(Promise.resolve()).resolves.not.toThrow();
});
test("should not throw for Promise.resolve(undefined)", async () => {
await expect(Promise.resolve(undefined)).resolves.not.toThrow();
});
test("should not throw for Promise.resolve(null)", async () => {
await expect(Promise.resolve(null)).resolves.not.toThrow();
});
test("should correctly detect when promise resolves to a thrown value", async () => {
async function throwsError() {
throw new Error("test error");
}
await expect(throwsError()).rejects.toThrow("test error");
});
test("should pass when expecting a resolved promise to throw", async () => {
// This should fail the assertion
await expect(
(async () => {
await expect(Promise.resolve("value")).resolves.toThrow();
})(),
).rejects.toBeInstanceOf(Error);
});
});