1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 |
x1
x1
x1
x1
x1
x1
x1
x1
x1
x5
x5
x1
x1
x5
x5
x5
x5
x5
x5
x5
x1
x3
x3
x3
x3
x12
x3
x3
x3
x3
x3
x3
x3
x15
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x1
x1
x9
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x15
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x9
x3
x3
x3
x3
x3
x3
x3
x1
x1
x9
x3 |
|
// Copyright 2018-2025 the Deno authors. MIT license.
import {
assertEquals,
assertObjectMatch,
assertStrictEquals,
} from "@std/assert";
import { assertSpyCall, assertSpyCalls, type Spy, spy, stub } from "./mock.ts";
import { TestSuiteInternal } from "./_test_suite.ts";
class TestContext implements Deno.TestContext {
name: string;
origin: string;
steps: TestContext[];
spies: {
step: Spy;
};
constructor(name: string) {
this.name = name;
this.origin = "origin";
this.spies = {
step: spy(this, "step"),
};
this.steps = [];
}
async step(t: Deno.TestStepDefinition): Promise<boolean>;
async step(
name: string,
fn: (t: Deno.TestContext) => void | Promise<void>,
): Promise<boolean>;
async step(
fn: (t: Deno.TestContext) => void | Promise<void>,
): Promise<boolean>;
async step(
tOrNameOrFn:
| Deno.TestStepDefinition
| string
| ((t: Deno.TestContext) => void | Promise<void>),
fn?: (t: Deno.TestContext) => void | Promise<void>,
): Promise<boolean> {
let ignore = false;
if (typeof tOrNameOrFn === "function") {
ignore = false;
fn = tOrNameOrFn;
} else if (typeof tOrNameOrFn === "object") {
ignore = tOrNameOrFn.ignore ?? false;
fn = tOrNameOrFn.fn;
}
const name = typeof tOrNameOrFn === "string"
? tOrNameOrFn
: tOrNameOrFn.name;
const context = new TestContext(name);
this.steps.push(context);
if (!ignore) {
await fn!(context);
}
return !ignore;
}
}
async function assertDescribeOptions(
expectedOptions: Omit<Deno.TestDefinition, "name" | "fn">,
cb: (fns: readonly [Spy, Spy]) => void,
) {
using test = stub(Deno, "test");
const fns = [spy(), spy()] as const;
try {
cb(fns);
assertSpyCall(test, 0);
const call = test.calls[0];
const options = call?.args[0] as Deno.TestDefinition;
assertEquals(
Object.keys(options).sort(),
["name", "fn", ...Object.keys(expectedOptions)].sort(),
);
assertObjectMatch(options, {
name: "example",
...expectedOptions,
});
assertSpyCalls(fns[0], 0);
assertSpyCalls(fns[1], 0);
const context = new TestContext("example");
const result = options.fn(context);
assertStrictEquals(Promise.resolve(result), result);
assertEquals(await result, undefined);
assertSpyCalls(context.spies.step, 0);
assertSpyCalls(fns[0], 0);
assertSpyCalls(fns[1], 0);
} finally {
TestSuiteInternal.reset();
}
}
export async function assertMinimumDescribeOptions(
cb: (fn: readonly [Spy, Spy]) => void,
) {
await assertDescribeOptions({ ignore: true }, cb);
}
async function assertItOptions(
expectedOptions: Omit<Deno.TestDefinition, "name" | "fn">,
cb: (fn: Spy) => void,
) {
using test = stub(Deno, "test");
const fn = spy();
try {
cb(fn);
assertSpyCalls(fn, 0);
assertSpyCall(test, 0);
const call = test.calls[0];
const options = call?.args[0] as Deno.TestDefinition;
assertEquals(
Object.keys(options).sort(),
["name", "fn", ...Object.keys(expectedOptions)].sort(),
);
assertObjectMatch(options, {
name: "example",
...expectedOptions,
});
const context = new TestContext("example");
const result = options.fn(context);
assertStrictEquals(Promise.resolve(result), result);
assertEquals(await result, undefined);
assertSpyCalls(context.spies.step, 0);
assertSpyCall(fn, 0, {
self: {},
args: [context],
returned: undefined,
});
assertSpyCalls(fn, 1);
} finally {
TestSuiteInternal.reset();
}
}
export async function assertMinimumItOptions(
cb: (fn: Spy) => void,
) {
await assertItOptions({ ignore: true }, cb);
}
|