diff --git a/packages/bun-types/test.d.ts b/packages/bun-types/test.d.ts index a3f7f9f8fe0474..1e96327290f068 100644 --- a/packages/bun-types/test.d.ts +++ b/packages/bun-types/test.d.ts @@ -146,7 +146,7 @@ declare module "bun:test" { export function spyOn( obj: T, methodOrPropertyValue: K, - ): Mock any ? T[K] : never>; + ): Mock any>>; interface FunctionLike { readonly name: string; diff --git a/test/integration/bun-types/fixture/test.ts b/test/integration/bun-types/fixture/test.ts index 968f07f960e087..eb4f710b65488e 100644 --- a/test/integration/bun-types/fixture/test.ts +++ b/test/integration/bun-types/fixture/test.ts @@ -1,9 +1,6 @@ -import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, spyOn, test } from "bun:test"; +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, type Mock, spyOn, test } from "bun:test"; import { expectType } from "./utilities"; -const spy = spyOn(console, "log"); -expectType(spy.mock.calls); - const hooks = [beforeAll, beforeEach, afterAll, afterEach]; for (const hook of hooks) { @@ -123,3 +120,24 @@ describe.each(dataAsConst)("test.each", (a, b, c) => { expectType(b); expectType<5 | "asdf">(c); }); + +const mySpyOnObjectWithOptionalMethod: { + optionalMethod?: (input: { question: string }) => { answer: string }; +} = { + optionalMethod: input => ({ answer: `Aswer to ${input.question}` }), +}; + +const mySpiedMethodOfOptional = spyOn(mySpyOnObjectWithOptionalMethod, "optionalMethod"); +mySpiedMethodOfOptional({ question: "asdf" }); +expectType { answer: string }>>(mySpiedMethodOfOptional); + +const myNormalSpyOnObject = { + normalMethod: (name: string) => `Hello ${name}`, +}; + +const myNormalSpiedMethod = spyOn(myNormalSpyOnObject, "normalMethod"); +myNormalSpiedMethod("asdf"); +expectType string>>(myNormalSpiedMethod); + +const spy = spyOn(console, "log"); +expectType(spy.mock.calls).is<[message?: any, ...optionalParams: any[]][]>(); diff --git a/test/regression/issue/18547.test.ts b/test/regression/issue/18547.test.ts index d489177ccd78ad..31980159d00160 100644 --- a/test/regression/issue/18547.test.ts +++ b/test/regression/issue/18547.test.ts @@ -16,7 +16,6 @@ test("18547", async () => { expect(request.cookies.get("sessionToken")).toEqual("123456"); expect(clone.cookies.get("sessionToken")).toEqual("654321"); - return new Response("OK"); }, },