Skip to content

Commit

Permalink
Implement expect().toHaveBeenCalledOnce() (#15871)
Browse files Browse the repository at this point in the history
Co-authored-by: Dylan Conway <[email protected]>
  • Loading branch information
Jarred-Sumner and dylan-conway authored Dec 20, 2024
1 parent 9164760 commit e3fed49
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/bun.js/test/expect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4060,6 +4060,37 @@ pub const Expect = struct {
return this.throw(globalThis, signature, "\n\n" ++ "Expected number of calls: \\>= <green>1<r>\n" ++ "Received number of calls: <red>{any}<r>\n", .{calls.getLength(globalThis)});
}

pub fn toHaveBeenCalledOnce(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue {
JSC.markBinding(@src());

const thisValue = callframe.this();
defer this.postMatch(globalThis);
const value: JSValue = try this.getValue(globalThis, thisValue, "toHaveBeenCalledOnce", "<green>expected<r>");

incrementExpectCallCounter();

const calls = JSMockFunction__getCalls(value);

if (calls == .zero or !calls.jsType().isArray()) {
return globalThis.throw("Expected value must be a mock function: {}", .{value});
}

var pass = @as(i32, @intCast(calls.getLength(globalThis))) == 1;

const not = this.flags.not;
if (not) pass = !pass;
if (pass) return .undefined;

// handle failure
if (not) {
const signature = comptime getSignature("toHaveBeenCalledOnce", "<green>expected<r>", true);
return this.throw(globalThis, signature, "\n\n" ++ "Expected number of calls: not <green>1<r>\n" ++ "Received number of calls: <red>{d}<r>\n", .{calls.getLength(globalThis)});
}

const signature = comptime getSignature("toHaveBeenCalledOnce", "<green>expected<r>", false);
return this.throw(globalThis, signature, "\n\n" ++ "Expected number of calls: <green>1<r>\n" ++ "Received number of calls: <red>{d}<r>\n", .{calls.getLength(globalThis)});
}

pub fn toHaveBeenCalledTimes(this: *Expect, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue {
JSC.markBinding(@src());

Expand Down
4 changes: 4 additions & 0 deletions src/bun.js/test/jest.classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ export default [
fn: "toHaveBeenCalled",
length: 0,
},
toHaveBeenCalledOnce: {
fn: "toHaveBeenCalledOnce",
length: 0,
},
toHaveBeenCalledTimes: {
fn: "toHaveBeenCalledTimes",
length: 1,
Expand Down
3 changes: 3 additions & 0 deletions test/js/bun/test/mock-fn.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,16 @@ describe("mock()", () => {
}
test("are callable", () => {
const fn = jest.fn(() => 42);
expect(fn).not.toHaveBeenCalledOnce();
expect(fn()).toBe(42);
expect(fn).toHaveBeenCalledOnce();
expect(fn).toHaveBeenCalled();
expect(fn).toHaveBeenCalledTimes(1);
expect(fn.mock.calls).toHaveLength(1);
expect(fn.mock.calls[0]).toBeEmpty();
expect(fn).toHaveBeenLastCalledWith();
expect(fn()).toBe(42);
expect(fn).not.toHaveBeenCalledOnce();
expect(fn).toHaveBeenCalledTimes(2);
expect(fn.mock.calls).toHaveLength(2);
expect(fn.mock.calls[1]).toBeEmpty();
Expand Down

0 comments on commit e3fed49

Please sign in to comment.