Skip to content

Commit 605a7a7

Browse files
committed
feat(expect,testing): add cross tests
1 parent 2314726 commit 605a7a7

3 files changed

+53
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
});

expect/_unstable_mock_instance.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function createMockInstance<
6060
args,
6161
timestamp: Date.now(),
6262
result: "returned",
63-
returned,
63+
returned: returned as never,
6464
});
6565
return returned;
6666
} catch (error) {

expect/_unstable_mock_utils.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ import { MOCK_SYMBOL, type MockCall } from "@std/internal/unstable_mock";
55
export { isMockFunction, MOCK_SYMBOL } from "@std/internal/unstable_mock";
66

77
export type ExpectMockCall<Args extends unknown[], Return> =
8-
& Omit<
9-
MockCall<Args, Return>,
10-
"returned"
11-
>
8+
& MockCall<Args, Return>
129
& {
1310
timestamp: number;
14-
returned?: Return | undefined;
1511
};
1612
export interface ExpectMockInternals<Args extends unknown[], Return> {
1713
readonly calls: ExpectMockCall<Args, Return>[];

0 commit comments

Comments
 (0)