diff options
Diffstat (limited to 'test/js')
-rw-r--r-- | test/js/bun/test/mock-test.test.ts | 93 |
1 files changed, 91 insertions, 2 deletions
diff --git a/test/js/bun/test/mock-test.test.ts b/test/js/bun/test/mock-test.test.ts index e51d9fcc7..8c998942c 100644 --- a/test/js/bun/test/mock-test.test.ts +++ b/test/js/bun/test/mock-test.test.ts @@ -1,5 +1,19 @@ import { test, mock, expect, spyOn, jest } from "bun:test"; +test("mockResolvedValue", async () => { + const fn = mock.mockResolvedValueOnce(42).mockResolvedValue(43); + expect(await fn()).toBe(42); + expect(await fn()).toBe(43); + expect(await fn()).toBe(43); +}); + +test("mockRejectedValue", async () => { + const fn = mock.mockRejectedValue(42); + expect(await fn()).toBe(42); + fn.mockRejectedValue(43); + expect(await fn()).toBe(43); +}); + test("are callable", () => { const fn = mock(() => 42); expect(fn()).toBe(42); @@ -15,6 +29,83 @@ test("are callable", () => { expect(fn.mock.calls[1]).toBeEmpty(); }); +test(".call works", () => { + const fn = mock(function hey() { + // @ts-expect-error + return this; + }); + expect(fn.call(123)).toBe(123); + expect(fn).toHaveBeenCalled(); + expect(fn).toHaveBeenCalledTimes(1); + expect(fn.mock.calls).toHaveLength(1); + expect(fn.mock.calls[0]).toBeEmpty(); + + expect(fn()).toBe(undefined); + expect(fn).toHaveBeenCalledTimes(2); + + expect(fn.mock.calls).toHaveLength(2); + expect(fn.mock.calls[1]).toBeEmpty(); +}); + +test(".apply works", () => { + const fn = mock(function hey() { + // @ts-expect-error + return this; + }); + expect(fn.apply(123)).toBe(123); + expect(fn).toHaveBeenCalled(); + expect(fn).toHaveBeenCalledTimes(1); + expect(fn.mock.calls).toHaveLength(1); + expect(fn.mock.calls[0]).toBeEmpty(); + + expect(fn.apply(undefined)).toBe(undefined); + expect(fn).toHaveBeenCalledTimes(2); + + expect(fn.mock.calls).toHaveLength(2); + expect(fn.mock.calls[1]).toBeEmpty(); +}); + +test(".bind works", () => { + const fn = mock(function hey() { + // @ts-expect-error + return this; + }); + expect(fn.bind(123)()).toBe(123); + expect(fn).toHaveBeenCalled(); + expect(fn).toHaveBeenCalledTimes(1); + expect(fn.mock.calls).toHaveLength(1); + expect(fn.mock.calls[0]).toBeEmpty(); + + expect(fn.bind(undefined)()).toBe(undefined); + expect(fn).toHaveBeenCalledTimes(2); + + expect(fn.mock.calls).toHaveLength(2); + expect(fn.mock.calls[1]).toBeEmpty(); +}); + +test(".name works", () => { + const fn = mock(function hey() { + // @ts-expect-error + return this; + }); + expect(fn.name).toBe("hey"); +}); + +test(".name throwing doesnt segfault", () => { + function baddie() { + // @ts-expect-error + return this; + } + Object.defineProperty(baddie, "name", { + get() { + throw new Error("foo"); + }, + }); + + const fn = mock(baddie); + fn.name; +}); + test("include arguments", () => { const fn = mock(f => f); expect(fn(43)).toBe(43); @@ -131,5 +222,3 @@ test("spyOn works on globalThis", () => { obj.original; expect(fn).not.toHaveBeenCalled(); }); - -// spyOn does not work with getters/setters yet. |