diff options
author | 2023-06-12 19:55:07 -0700 | |
---|---|---|
committer | 2023-06-12 19:55:07 -0700 | |
commit | dbb2416542ee391fac5a11ba56090bf946117b9d (patch) | |
tree | ab9d840af617c635caf689b1eac43bba2e9de44f /test/js | |
parent | 51c093e24ead041aa9f327ddf9e95bb37086deba (diff) | |
download | bun-dbb2416542ee391fac5a11ba56090bf946117b9d.tar.gz bun-dbb2416542ee391fac5a11ba56090bf946117b9d.tar.zst bun-dbb2416542ee391fac5a11ba56090bf946117b9d.zip |
Make mocks use FunctionPrototype (#3291)
* Make mocks use FunctionPrototype
* Fix static methods
* Fix types
* Update JSMockFunction.cpp
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
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. |