Skip to content

Commit

Permalink
feat(expect,testing): add cross tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anion155 committed Dec 30, 2024
1 parent 2314726 commit 605a7a7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
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));
});
2 changes: 1 addition & 1 deletion expect/_unstable_mock_instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function createMockInstance<
args,
timestamp: Date.now(),
result: "returned",
returned,
returned: returned as never,
});
return returned;
} catch (error) {
Expand Down
6 changes: 1 addition & 5 deletions expect/_unstable_mock_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Args extends unknown[], Return> =
& Omit<
MockCall<Args, Return>,
"returned"
>
& MockCall<Args, Return>
& {
timestamp: number;
returned?: Return | undefined;
};
export interface ExpectMockInternals<Args extends unknown[], Return> {
readonly calls: ExpectMockCall<Args, Return>[];
Expand Down

0 comments on commit 605a7a7

Please sign in to comment.