Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(internal,expect,testing): @std/expect jest compatible fn and unified mock experience with @std/testing/mock #6317

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
36 changes: 0 additions & 36 deletions expect/_to_have_returned_with.ts

This file was deleted.

51 changes: 51 additions & 0 deletions expect/_unstable_asserts_compability_test.ts
Original file line number Diff line number Diff line change
@@ -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));
});
Loading
Loading