This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
describe.ts
274 lines (253 loc) · 8.14 KB
/
describe.ts
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import {
test,
TestDefinition,
TestSuite,
TestSuiteDefinition,
} from "./test_suite.ts";
export interface DescribeDefinition {
/** The name of the test suite will be prepended to the names of tests in the fn. */
name: string;
/** The callback for the test suite. */
fn: () => void;
/** Ignore all tests in suite if set to true. */
ignore?: boolean;
/**
* If at least one test suite or test has only set to true,
* only run test suites and tests that have only set to true.
*/
only?: boolean;
/**
* Check that the number of async completed ops after each test in the suite
* is the same as the number of dispatched ops. Defaults to true.
*/
sanitizeOps?: boolean;
/**
* Ensure the test cases in the suite do not "leak" resources - ie. the resource table
* after each test has exactly the same contents as before each test. Defaults to true.
*/
sanitizeResources?: boolean;
}
export interface ItDefinition {
/** The name of the test. */
name: string;
/** The test function. */
fn:
| (() => void)
| (() => Promise<void>);
/** Ignore test if set to true. */
ignore?: boolean;
/**
* If at least one test suite or test has only set to true,
* only run test suites and tests that have only set to true.
*/
only?: boolean;
/**
* Check that the number of async completed ops after the test is the same as
* the number of dispatched ops after the test. Defaults to true.
*/
sanitizeOps?: boolean;
/**
* Ensure the test case does not "leak" resources - ie. the resource table after the test
* has exectly the same contents as before the test. Defaults to true.
*/
sanitizeResources?: boolean;
}
interface Context {
beforeEach: boolean;
afterEach: boolean;
beforeAll: boolean;
afterAll: boolean;
}
const hooksLocked: boolean[] = [];
function areHooksLocked(): boolean {
return hooksLocked[hooksLocked.length - 1] ?? false;
}
function lockHooks(): void {
if (hooksLocked.length === 0) {
throw new Error("cannot lock hooks on global context");
}
hooksLocked[hooksLocked.length - 1] = true;
}
let currentContext: Context | null = null;
let currentSuite: TestSuite<void> | null = null;
/**
* A group of tests. A test suite can include child test suites.
* The name of the test suite is prepended to the name of each test within it.
* Tests belonging to a suite will inherit options from it.
*/
function describe(name: string, fn: () => void): void;
function describe(options: DescribeDefinition): void;
function describe(a: string | DescribeDefinition, fn?: () => void): void {
let options: TestSuiteDefinition<void> | void;
if (typeof a === "string") {
options = { name: a };
} else {
fn = a.fn;
options = { name: a.name };
if (typeof a.ignore !== "undefined") options.ignore = a.ignore;
if (typeof a.only !== "undefined") options.only = a.only;
if (typeof a.sanitizeOps !== "undefined") {
options.sanitizeOps = a.sanitizeOps;
}
if (typeof a.sanitizeResources !== "undefined") {
options.sanitizeResources = a.sanitizeResources;
}
}
const parent: TestSuite<void> | null = currentSuite;
if (parent) options.suite = parent;
const suite: TestSuite<void> = new TestSuite(options);
const parentContext: Context | null = currentContext;
currentContext = {
beforeEach: false,
afterEach: false,
beforeAll: false,
afterAll: false,
};
if (currentSuite) lockHooks();
hooksLocked.push(false);
currentSuite = suite;
try {
fn!();
} finally {
hooksLocked.pop();
currentContext = parentContext;
currentSuite = parent;
}
}
export type FocusDescribeDefinition = Omit<
DescribeDefinition,
"ignore" | "only"
>;
function focusDescribe(
ignore: boolean,
only: boolean,
a: string | FocusDescribeDefinition,
fn?: () => void,
): void {
let options: DescribeDefinition | void;
if (typeof a === "string") {
options = { name: a, fn: fn! };
} else {
options = { ...a };
}
options.ignore = ignore;
options.only = only;
describe(options);
}
/**
* A focused group of tests. A test suite can include child test suites.
* The name of the test suite is prepended to the name of each test within it.
* Tests belonging to a suite will inherit options from it.
*/
function fdescribe(name: string, fn: () => void): void;
function fdescribe(options: FocusDescribeDefinition): void;
function fdescribe(a: string | FocusDescribeDefinition, fn?: () => void): void {
focusDescribe(false, true, a, fn);
}
/**
* An ignored group of tests. A test suite can include child test suites.
* The name of the test suite is prepended to the name of each test within it.
* Tests belonging to a suite will inherit options from it.
*/
function xdescribe(name: string, fn: () => void): void;
function xdescribe(options: FocusDescribeDefinition): void;
function xdescribe(a: string | FocusDescribeDefinition, fn?: () => void): void {
focusDescribe(true, false, a, fn);
}
/**
* Register a test which will run when `deno test` is used on the command line
* and the containing module looks like a test module.
*/
function it(name: string, fn: () => void): void;
function it(options: ItDefinition): void;
function it(
a: string | ItDefinition,
fn?: (() => void) | (() => Promise<void>),
): void {
if (currentSuite) lockHooks();
let options: TestDefinition<void> | void;
if (typeof a === "string") {
options = { name: a, fn: fn! };
} else {
options = { name: a.name, fn: a.fn };
if (typeof a.ignore !== "undefined") options.ignore = a.ignore;
if (typeof a.only !== "undefined") options.only = a.only;
if (typeof a.sanitizeOps !== "undefined") {
options.sanitizeOps = a.sanitizeOps;
}
if (typeof a.sanitizeResources !== "undefined") {
options.sanitizeResources = a.sanitizeResources;
}
}
if (currentSuite) options.suite = currentSuite;
test(options);
}
export type FocusItDefinition = Omit<ItDefinition, "ignore" | "only">;
function focusIt(
ignore: boolean,
only: boolean,
a: string | FocusItDefinition,
fn?: () => void,
): void {
let options: ItDefinition | void;
if (typeof a === "string") {
options = { name: a, fn: fn! };
} else {
options = { ...a };
}
options.ignore = ignore;
options.only = only;
it(options);
}
/**
* Register a focused test which will run when `deno test` is used on the command line
* and the containing module looks like a test module.
*/
function fit(name: string, fn: () => void): void;
function fit(options: FocusItDefinition): void;
function fit(a: string | FocusItDefinition, fn?: () => void): void {
focusIt(false, true, a, fn);
}
/**
* Register an ignored test which will run when `deno test` is used on the command line
* and the containing module looks like a test module.
*/
function xit(name: string, fn: () => void): void;
function xit(options: FocusItDefinition): void;
function xit(a: string | FocusItDefinition, fn?: () => void): void {
focusIt(true, false, a, fn);
}
function setHook(
key: "beforeEach" | "afterEach" | "beforeAll" | "afterAll",
fn: (() => void) | (() => Promise<void>),
) {
if (!currentSuite) throw new Error(`${key} not allowed globally`);
if (areHooksLocked()) {
throw new Error(
`${key} must be called before child suites and tests in suite`,
);
}
if (currentContext![key]) {
throw new Error(`${key} already called for suite`);
}
currentContext![key] = true;
const suite: TestSuite<void> = currentSuite;
TestSuite.setHooks(suite, { [key]: fn });
}
/** Run some shared setup before each test in the suite. */
export function beforeEach(fn: (() => void) | (() => Promise<void>)): void {
setHook("beforeEach", fn);
}
/** Run some shared teardown after each test in the suite. */
export function afterEach(fn: (() => void) | (() => Promise<void>)): void {
setHook("afterEach", fn);
}
/** Run some shared setup before all of the tests in the suite. */
export function beforeAll(fn: (() => void) | (() => Promise<void>)): void {
setHook("beforeAll", fn);
}
/** Run some shared teardown after all of the tests in the suite. */
export function afterAll(fn: (() => void) | (() => Promise<void>)): void {
setHook("afterAll", fn);
}
export { describe, fdescribe, fit, it, xdescribe, xit };