|
| 1 | +import { describe, it, expect } from "bun:test"; |
| 2 | +import assert, { AssertionError } from "assert"; |
| 3 | + |
| 4 | +describe("assert.rejects", () => { |
| 5 | + it("accepts a rejecting function", async () => { |
| 6 | + const rejectingFn = async () => { |
| 7 | + throw new AssertionError({ |
| 8 | + message: "Failed", |
| 9 | + operator: "fail", |
| 10 | + }); |
| 11 | + }; |
| 12 | + |
| 13 | + await expect( |
| 14 | + assert.rejects(rejectingFn, { |
| 15 | + name: "AssertionError", |
| 16 | + message: "Failed", |
| 17 | + }), |
| 18 | + ).resolves.toBeUndefined(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("accepts a rejecting promise", async () => { |
| 22 | + const rejectingPromise = Promise.reject( |
| 23 | + new AssertionError({ |
| 24 | + message: "Failed", |
| 25 | + operator: "fail", |
| 26 | + }), |
| 27 | + ); |
| 28 | + |
| 29 | + await expect( |
| 30 | + assert.rejects(rejectingPromise, { |
| 31 | + name: "AssertionError", |
| 32 | + message: "Failed", |
| 33 | + }), |
| 34 | + ).resolves.toBeUndefined(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("handles thenable objects when cast to Promise", async () => { |
| 38 | + // Create a Promise from a thenable to make TypeScript happy |
| 39 | + const thenablePromise = Promise.resolve().then(() => { |
| 40 | + return Promise.reject({ name: "CustomError" }); |
| 41 | + }); |
| 42 | + |
| 43 | + await expect(assert.rejects(thenablePromise, { name: "CustomError" })).resolves.toBeUndefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("rejects when promise resolves instead of rejecting", async () => { |
| 47 | + await expect(assert.rejects(Promise.resolve())).rejects.toMatchObject({ |
| 48 | + message: "Missing expected rejection.", |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it("rejects with correct error when validation function returns non-boolean", async () => { |
| 53 | + const err = new Error("foobar"); |
| 54 | + const validate = () => "baz"; |
| 55 | + |
| 56 | + await expect(assert.rejects(Promise.reject(err), validate)).rejects.toMatchObject({ |
| 57 | + message: expect.stringContaining( |
| 58 | + 'The "validate" validation function is expected to return "true". Received \'baz\'', |
| 59 | + ), |
| 60 | + actual: err, |
| 61 | + expected: validate, |
| 62 | + name: "AssertionError", |
| 63 | + operator: "rejects", |
| 64 | + }); |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +describe("assert.doesNotReject", () => { |
| 69 | + it("resolves when promise resolves", async () => { |
| 70 | + await expect(assert.doesNotReject(Promise.resolve())).resolves.toBeUndefined(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("resolves when async function resolves", async () => { |
| 74 | + await expect(assert.doesNotReject(async () => {})).resolves.toBeUndefined(); |
| 75 | + }); |
| 76 | + |
| 77 | + it("handles thenable objects with proper Promise cast", async () => { |
| 78 | + // Create a proper Promise from a thenable pattern |
| 79 | + const thenablePromise = Promise.resolve().then(() => { |
| 80 | + return "success"; |
| 81 | + }); |
| 82 | + |
| 83 | + await expect(assert.doesNotReject(thenablePromise)).resolves.toBeUndefined(); |
| 84 | + }); |
| 85 | + |
| 86 | + it("documents Node.js behavior with invalid thenables", async () => { |
| 87 | + const invalidThenable = { |
| 88 | + then: (fulfill, reject) => { |
| 89 | + fulfill(); |
| 90 | + }, |
| 91 | + }; |
| 92 | + |
| 93 | + await expect(assert.doesNotReject(invalidThenable as any)).rejects.toMatchObject({ |
| 94 | + message: expect.stringContaining('The "promiseFn" argument must be of type function or an instance of Promise'), |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it("rejects when promise rejects", async () => { |
| 99 | + await expect(assert.doesNotReject(Promise.reject(new Error("Failed")))).rejects.toMatchObject({ |
| 100 | + message: expect.stringContaining("Got unwanted rejection"), |
| 101 | + operator: "doesNotReject", |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + it("rejects when async function rejects", async () => { |
| 106 | + const rejectingFn = async () => { |
| 107 | + throw new Error("Failed"); |
| 108 | + }; |
| 109 | + |
| 110 | + await expect(assert.doesNotReject(rejectingFn)).rejects.toMatchObject({ |
| 111 | + message: expect.stringContaining("Got unwanted rejection"), |
| 112 | + operator: "doesNotReject", |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it("rejects with invalid argument types", async () => { |
| 117 | + await expect(assert.doesNotReject(123 as any)).rejects.toMatchObject({ |
| 118 | + message: expect.stringContaining('The "promiseFn" argument must be of type function or an instance of Promise'), |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments