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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447 |
 
 
x26
x26
 
x26
x26
x26
x26
x26
x26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x26
x26
x130
x130
x26
 
x26
x130
x130
x130
 
x130
x130
x131
x131
 
x131
x130
x130
x130
x130
x130
 
x130
x130
x146
x146
x146
x146
x146
x146
x146
x146
x146
x146
x146
x146
x146
x146
x151
x151
x146
x151
x151
x146
x151
x151
x146
x151
x151
x146
x151
x151
x146
x146
x146
 
x232
x295
x295
x295
 
x295
x295
x297
x297
 
x297
x295
x295
x295
x295
 
x130
x246
x225
x289
x289
x289
x289
x289
x289
x289
x289
x289
x289
x295
x295
x289
x289
x289
x331
x331
x331
x331
x331
x337
x331
x368
x370
x370
x368
x331
x331
x331
x331
x331
x331
x331
x337
x331
x368
x370
x370
x368
x331
x331
x331
x331
x331
x289
x289
x299
x299
x289
x314
x314
x289
x299
x299
x289
x299
x299
x289
x299
x299
x289
x299
x299
x289
x289
x130
 
 
x26
 
 
x52
 
 
 
x52
 
 
 
x52
 
 
x26
 
 
x26
x119
x119
x119
x119
x119
 
 
x26
x462
x154
x230
x230
x154
x252
x252
x154
x233
x233
x154
x252
x252
x154
x252
x252
x154
x252
x252
x154
x154
 
 
x26
x40
x40
x52
x52
x62
x62
x52
x40
x40
 
x40
x40
x40
x40
x44
x44
x40
 
 
x26
x26
x26
x26
x194
x352
x371
x375
x375
x352
x491
x491
x352
 
x194
x194
x194
x354
x354
x194
 
 
x26
x26
x26
x26
x26
x67
x75
x75
x75
x75
x75
x75
x67
x100
x100
x67
 
 
x26
x26
x26
x26
x26
x79
x79
 
 
 
 
 
 
 
x165
x165
x165
x165
x165
x165
x165
x165
x165
 
x165
x165
x165
x251
x252
 
x252
 
x252
x1008
x251
x262
x262
x263
x262
x273
x275
x275
x273
x262
x262
x262
x262
x262
x262
x262
x263
x262
x273
x275
x275
x273
x262
x251
x325
x325
x251
x165
 
 
 
 
 
 
 
 
 
 
 
 
x165
x250
x79
 
x26
x26
x26
x26
x26
x26
x188
x188
x188
x976
x276
x276
x290
x276
x354
x362
x362
x354
x276
x276
x276
x276
x276
x290
x276
x354
x362
x362
x354
x276
x188
x262
x262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x188
x188
x26 |
I
I
I
I
I
I
I
I
|
// Copyright 2018-2025 the Deno authors. MIT license.
import { getAssertionState } from "@std/internal/assertion-state";
import { AssertionError } from "@std/assert/assertion-error";
export const globalSanitizersState = {
sanitizeExit: undefined as boolean | undefined,
sanitizeOps: undefined as boolean | undefined,
sanitizeResources: undefined as boolean | undefined,
};
const assertionState = getAssertionState();
/** The options for creating a test suite with the describe function. */
export interface DescribeDefinition<T> extends Omit<Deno.TestDefinition, "fn"> {
/** The body of the test suite */
fn?: () => void | undefined;
/**
* The `describe` function returns a `TestSuite` representing the group of tests.
* If `describe` is called within another `describe` calls `fn`, the suite will default to that parent `describe` calls returned `TestSuite`.
* If `describe` is not called within another `describe` calls `fn`, the suite will default to the `TestSuite` representing the global group of tests.
*/
suite?: TestSuite<T>;
/** Run some shared setup before all of the tests in the suite. */
beforeAll?:
| ((this: T) => void | Promise<void>)
| ((this: T) => void | Promise<void>)[];
/** Run some shared teardown after all of the tests in the suite. */
afterAll?:
| ((this: T) => void | Promise<void>)
| ((this: T) => void | Promise<void>)[];
/** Run some shared setup before each test in the suite. */
beforeEach?:
| ((this: T) => void | Promise<void>)
| ((this: T) => void | Promise<void>)[];
/** Run some shared teardown after each test in the suite. */
afterEach?:
| ((this: T) => void | Promise<void>)
| ((this: T) => void | Promise<void>)[];
}
/** The options for creating an individual test case with the it function. */
export interface ItDefinition<T> extends Omit<Deno.TestDefinition, "fn"> {
/** The body of the test case */
fn: (this: T, t: Deno.TestContext) => void | Promise<void>;
/**
* The `describe` function returns a `TestSuite` representing the group of tests.
* If `it` is called within a `describe` calls `fn`, the suite will default to that parent `describe` calls returned `TestSuite`.
* If `it` is not called within a `describe` calls `fn`, the suite will default to the `TestSuite` representing the global group of tests.
*/
suite?: TestSuite<T>;
}
/** The names of all the different types of hooks. */
export type HookNames = "beforeAll" | "afterAll" | "beforeEach" | "afterEach";
/**
* A group of tests.
*/
export interface TestSuite<T> {
/** The symbol to use for grouping the test suite */
symbol: symbol;
}
/**
* An internal representation of a group of tests.
*/
export class TestSuiteInternal<T> implements TestSuite<T> {
symbol: symbol;
protected describe: DescribeDefinition<T>;
protected steps: (TestSuiteInternal<T> | ItDefinition<T>)[];
protected hasOnlyStep: boolean;
constructor(describe: DescribeDefinition<T>) {
this.describe = describe;
this.steps = [];
this.hasOnlyStep = false;
const { suite } = describe;
if (suite && !TestSuiteInternal.suites.has(suite.symbol)) {
throw new Error(
"Cannot construct Test Suite: suite does not represent a registered test suite",
);
}
const testSuite = suite
? TestSuiteInternal.suites.get(suite.symbol)
: TestSuiteInternal.current;
this.symbol = Symbol();
TestSuiteInternal.suites.set(this.symbol, this);
const { fn, ignore } = describe;
if (ignore) {
const {
name,
only,
permissions,
sanitizeExit = globalSanitizersState.sanitizeExit,
sanitizeOps = globalSanitizersState.sanitizeOps,
sanitizeResources = globalSanitizersState.sanitizeResources,
} = describe;
const options: Deno.TestDefinition = {
name,
fn: async () => {},
ignore: true,
};
if (only !== undefined) {
options.only = only;
}
if (permissions !== undefined) {
options.permissions = permissions;
}
if (sanitizeExit !== undefined) {
options.sanitizeExit = sanitizeExit;
}
if (sanitizeOps !== undefined) {
options.sanitizeOps = sanitizeOps;
}
if (sanitizeResources !== undefined) {
options.sanitizeResources = sanitizeResources;
}
TestSuiteInternal.registerTest(options);
return;
}
if (fn) {
const temp = TestSuiteInternal.current;
TestSuiteInternal.current = this;
try {
// deno-lint-ignore no-explicit-any
const value = fn() as any;
if (value instanceof Promise) {
throw new Error(
'Returning a Promise from "describe" is not supported: tests must be defined synchronously',
);
}
} finally {
TestSuiteInternal.current = temp;
}
}
if (testSuite) {
TestSuiteInternal.addStep(testSuite, this);
} else {
const {
name,
ignore,
permissions,
sanitizeExit = globalSanitizersState.sanitizeExit,
sanitizeOps = globalSanitizersState.sanitizeOps,
sanitizeResources = globalSanitizersState.sanitizeResources,
} = describe;
let { only } = describe;
if (!ignore && this.hasOnlyStep) {
only = true;
}
const options: Deno.TestDefinition = {
name,
fn: async (t) => {
TestSuiteInternal.runningCount++;
try {
const context = {} as T;
const { beforeAll } = this.describe;
if (typeof beforeAll === "function") {
await beforeAll.call(context);
} else if (beforeAll) {
for (const hook of beforeAll) {
await hook.call(context);
}
}
try {
TestSuiteInternal.active.push(this.symbol);
await TestSuiteInternal.run(this, context, t);
} finally {
TestSuiteInternal.active.pop();
const { afterAll } = this.describe;
if (typeof afterAll === "function") {
await afterAll.call(context);
} else if (afterAll) {
for (const hook of afterAll) {
await hook.call(context);
}
}
}
} finally {
TestSuiteInternal.runningCount--;
}
},
};
if (ignore !== undefined) {
options.ignore = ignore;
}
if (only !== undefined) {
options.only = only;
}
if (permissions !== undefined) {
options.permissions = permissions;
}
if (sanitizeExit !== undefined) {
options.sanitizeExit = sanitizeExit;
}
if (sanitizeOps !== undefined) {
options.sanitizeOps = sanitizeOps;
}
if (sanitizeResources !== undefined) {
options.sanitizeResources = sanitizeResources;
}
TestSuiteInternal.registerTest(options);
}
}
/** Stores how many test suites are executing. */
static runningCount = 0;
/** If a test has been registered yet. Block adding global hooks if a test has been registered. */
static started = false;
/** A map of all test suites by symbol. */
// deno-lint-ignore no-explicit-any
static suites = new Map<symbol, TestSuiteInternal<any>>();
/** The current test suite being registered. */
// deno-lint-ignore no-explicit-any
static current: TestSuiteInternal<any> | null = null;
/** The stack of tests that are actively running. */
static active: symbol[] = [];
/** This is used internally for testing this module. */
static reset() {
TestSuiteInternal.runningCount = 0;
TestSuiteInternal.started = false;
TestSuiteInternal.current = null;
TestSuiteInternal.active = [];
}
/** This is used internally to register tests. */
static registerTest(options: Deno.TestDefinition) {
options = { ...options };
if (options.only === undefined) {
delete options.only;
}
if (options.permissions === undefined) {
delete options.permissions;
}
if (options.ignore === undefined) {
delete options.ignore;
}
if (options.sanitizeExit === undefined) {
delete options.sanitizeExit;
}
if (options.sanitizeOps === undefined) {
delete options.sanitizeOps;
}
if (options.sanitizeResources === undefined) {
delete options.sanitizeResources;
}
Deno.test(options);
}
/** Updates all steps within top level suite to have ignore set to true if only is not set to true on step. */
static addingOnlyStep<T>(suite: TestSuiteInternal<T>) {
if (!suite.hasOnlyStep) {
for (let i = 0; i < suite.steps.length; i++) {
const step = suite.steps[i]!;
if (!(step instanceof TestSuiteInternal) && !step.only) {
suite.steps.splice(i--, 1);
}
}
suite.hasOnlyStep = true;
}
const parentSuite = suite.describe.suite;
const parentTestSuite = parentSuite &&
TestSuiteInternal.suites.get(parentSuite.symbol);
if (parentTestSuite) {
TestSuiteInternal.addingOnlyStep(parentTestSuite);
}
}
/** This is used internally to add steps to a test suite. */
static addStep<T>(
suite: TestSuiteInternal<T>,
step: TestSuiteInternal<T> | ItDefinition<T>,
) {
if (!suite.hasOnlyStep) {
if (step instanceof TestSuiteInternal) {
if (step.hasOnlyStep || step.describe.only) {
TestSuiteInternal.addingOnlyStep(suite);
}
} else {
if (step.only) TestSuiteInternal.addingOnlyStep(suite);
}
}
if (
!(suite.hasOnlyStep && !(step instanceof TestSuiteInternal) && !step.only)
) {
suite.steps.push(step);
}
}
/** This is used internally to add hooks to a test suite. */
static setHook<T>(
suite: TestSuiteInternal<T>,
name: HookNames,
fn: (this: T) => void | Promise<void>,
) {
if (suite.describe[name]) {
if (typeof suite.describe[name] === "function") {
suite.describe[name] = [
suite.describe[name] as ((this: T) => void | Promise<void>),
];
}
(suite.describe[name] as ((this: T) => void | Promise<void>)[]).push(fn);
} else {
suite.describe[name] = fn;
}
}
/** This is used internally to run all steps for a test suite. */
static async run<T>(
suite: TestSuiteInternal<T>,
context: T,
t: Deno.TestContext,
) {
const hasOnly = suite.hasOnlyStep || suite.describe.only || false;
for (const step of suite.steps) {
if (
hasOnly && step instanceof TestSuiteInternal &&
!(step.hasOnlyStep || step.describe.only || false)
) {
continue;
}
const {
name,
fn,
ignore,
permissions,
sanitizeExit = globalSanitizersState.sanitizeExit,
sanitizeOps = globalSanitizersState.sanitizeOps,
sanitizeResources = globalSanitizersState.sanitizeResources,
} = step instanceof TestSuiteInternal ? step.describe : step;
const options: Deno.TestStepDefinition = {
name,
fn: async (t) => {
if (permissions) {
throw new Error(
// deno-lint-ignore deno-style-guide/error-message
"permissions option not available for nested tests",
);
}
context = { ...context };
if (step instanceof TestSuiteInternal) {
const { beforeAll } = step.describe;
if (typeof beforeAll === "function") {
await beforeAll.call(context);
} else if (beforeAll) {
for (const hook of beforeAll) {
await hook.call(context);
}
}
try {
TestSuiteInternal.active.push(step.symbol);
await TestSuiteInternal.run(step, context, t);
} finally {
TestSuiteInternal.active.pop();
const { afterAll } = step.describe;
if (typeof afterAll === "function") {
await afterAll.call(context);
} else if (afterAll) {
for (const hook of afterAll) {
await hook.call(context);
}
}
}
} else {
await TestSuiteInternal.runTest(t, fn!, context);
}
},
};
if (ignore !== undefined) {
options.ignore = ignore;
}
if (sanitizeExit !== undefined) {
options.sanitizeExit = sanitizeExit;
}
if (sanitizeOps !== undefined) {
options.sanitizeOps = sanitizeOps;
}
if (sanitizeResources !== undefined) {
options.sanitizeResources = sanitizeResources;
}
await t.step(options);
}
}
static async runTest<T>(
t: Deno.TestContext,
fn: (this: T, t: Deno.TestContext) => void | Promise<void>,
context: T,
activeIndex = 0,
) {
const suite = TestSuiteInternal.active[activeIndex];
const testSuite = suite && TestSuiteInternal.suites.get(suite);
if (testSuite) {
if (activeIndex === 0) context = { ...context };
const { beforeEach } = testSuite.describe;
if (typeof beforeEach === "function") {
await beforeEach.call(context);
} else if (beforeEach) {
for (const hook of beforeEach) {
await hook.call(context);
}
}
try {
await TestSuiteInternal.runTest(t, fn, context, activeIndex + 1);
} finally {
const { afterEach } = testSuite.describe;
if (typeof afterEach === "function") {
await afterEach.call(context);
} else if (afterEach) {
for (const hook of afterEach) {
await hook.call(context);
}
}
}
} else {
await fn.call(context, t);
}
if (assertionState.checkAssertionErrorState()) {
throw new AssertionError(
"Expected at least one assertion to be called but received none",
);
}
if (assertionState.checkAssertionCountSatisfied()) {
throw new AssertionError(
`Expected at least ${assertionState.assertionCount} assertion to be called, ` +
`but received ${assertionState.assertionTriggeredCount}`,
);
}
assertionState.resetAssertionState();
}
}
|