|
| 1 | +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. |
| 2 | + |
| 3 | +import { |
| 4 | + assertSpyCall, |
| 5 | + assertSpyCallArg, |
| 6 | + assertSpyCallArgs, |
| 7 | + assertSpyCallAsync, |
| 8 | + assertSpyCalls, |
| 9 | + spy, |
| 10 | +} from "@std/testing/unstable-mock"; |
| 11 | +import { expect } from "./unstable_expect.ts"; |
| 12 | +import { fn } from "./unstable_fn.ts"; |
| 13 | + |
| 14 | +Deno.test("@std/expect/fn should be compatible with @std/testing/mock asserts", async () => { |
| 15 | + const mockFn = fn((a: number, b: number) => a + b); |
| 16 | + mockFn(1, 1); |
| 17 | + mockFn(1, 2); |
| 18 | + |
| 19 | + assertSpyCalls(mockFn, 2); |
| 20 | + assertSpyCall(mockFn, 0, { args: [1, 1], returned: 2 }); |
| 21 | + assertSpyCallArgs(mockFn, 1, [1, 2]); |
| 22 | + assertSpyCallArg(mockFn, 0, 0, 1); |
| 23 | + |
| 24 | + const mockAsyncFn = fn((a: number, b: number) => Promise.resolve(a + b)); |
| 25 | + await mockAsyncFn(1, 1); |
| 26 | + await assertSpyCallAsync(mockAsyncFn, 0, { |
| 27 | + args: [1, 1], |
| 28 | + returned: 2, |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +Deno.test("@std/testing/mock should be compatible with @std/expect", () => { |
| 33 | + const sum = (a: number, b: number) => a + b; |
| 34 | + |
| 35 | + const value = { sum }; |
| 36 | + const methodFn = spy(value, "sum"); |
| 37 | + value.sum(1, 1); |
| 38 | + expect(methodFn).toHaveBeenCalledWith(1, 1); |
| 39 | + expect(methodFn).toHaveReturnedWith(2); |
| 40 | + |
| 41 | + const spyFn = spy(sum); |
| 42 | + spyFn(1, 1); |
| 43 | + spyFn(1, 2); |
| 44 | + expect(spyFn).toHaveBeenCalledTimes(2); |
| 45 | + expect(spyFn).toHaveBeenLastCalledWith(1, 2); |
| 46 | + |
| 47 | + class A {} |
| 48 | + const constructorFn = spy(A); |
| 49 | + expect(new constructorFn()).toBeInstanceOf(A); |
| 50 | + expect(constructorFn).toHaveReturnedWith(expect.any(A)); |
| 51 | +}); |
0 commit comments