diff --git a/expect/_unstable_asserts_compability_test.ts b/expect/_unstable_asserts_compability_test.ts new file mode 100644 index 000000000000..1f407499f47c --- /dev/null +++ b/expect/_unstable_asserts_compability_test.ts @@ -0,0 +1,51 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +import { + assertSpyCall, + assertSpyCallArg, + assertSpyCallArgs, + assertSpyCallAsync, + assertSpyCalls, + spy, +} from "@std/testing/unstable-mock"; +import { expect } from "./unstable_expect.ts"; +import { fn } from "./unstable_fn.ts"; + +Deno.test("@std/expect/fn should be compatible with @std/testing/mock asserts", async () => { + const mockFn = fn((a: number, b: number) => a + b); + mockFn(1, 1); + mockFn(1, 2); + + assertSpyCalls(mockFn, 2); + assertSpyCall(mockFn, 0, { args: [1, 1], returned: 2 }); + assertSpyCallArgs(mockFn, 1, [1, 2]); + assertSpyCallArg(mockFn, 0, 0, 1); + + const mockAsyncFn = fn((a: number, b: number) => Promise.resolve(a + b)); + await mockAsyncFn(1, 1); + await assertSpyCallAsync(mockAsyncFn, 0, { + args: [1, 1], + returned: 2, + }); +}); + +Deno.test("@std/testing/mock should be compatible with @std/expect", () => { + const sum = (a: number, b: number) => a + b; + + const value = { sum }; + const methodFn = spy(value, "sum"); + value.sum(1, 1); + expect(methodFn).toHaveBeenCalledWith(1, 1); + expect(methodFn).toHaveReturnedWith(2); + + const spyFn = spy(sum); + spyFn(1, 1); + spyFn(1, 2); + expect(spyFn).toHaveBeenCalledTimes(2); + expect(spyFn).toHaveBeenLastCalledWith(1, 2); + + class A {} + const constructorFn = spy(A); + expect(new constructorFn()).toBeInstanceOf(A); + expect(constructorFn).toHaveReturnedWith(expect.any(A)); +}); diff --git a/expect/_unstable_mock_instance.ts b/expect/_unstable_mock_instance.ts index ee1eac1acca1..a41a358accf1 100644 --- a/expect/_unstable_mock_instance.ts +++ b/expect/_unstable_mock_instance.ts @@ -60,7 +60,7 @@ export function createMockInstance< args, timestamp: Date.now(), result: "returned", - returned, + returned: returned as never, }); return returned; } catch (error) { diff --git a/expect/_unstable_mock_utils.ts b/expect/_unstable_mock_utils.ts index 862b0bf7617d..e56dd2ffea41 100644 --- a/expect/_unstable_mock_utils.ts +++ b/expect/_unstable_mock_utils.ts @@ -5,13 +5,9 @@ import { MOCK_SYMBOL, type MockCall } from "@std/internal/unstable_mock"; export { isMockFunction, MOCK_SYMBOL } from "@std/internal/unstable_mock"; export type ExpectMockCall = - & Omit< - MockCall, - "returned" - > + & MockCall & { timestamp: number; - returned?: Return | undefined; }; export interface ExpectMockInternals { readonly calls: ExpectMockCall[];