@effectionx/bdd 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/LICENSE +9 -0
  2. package/bdd.test.ts +12 -0
  3. package/bdd.ts +124 -0
  4. package/dist/bdd.d.ts +41 -0
  5. package/dist/bdd.d.ts.map +1 -0
  6. package/dist/bdd.js +77 -0
  7. package/dist/mod.d.ts +2 -0
  8. package/dist/mod.d.ts.map +1 -0
  9. package/dist/mod.js +3 -0
  10. package/dist/mod.node.d.ts +10 -0
  11. package/dist/mod.node.d.ts.map +1 -0
  12. package/dist/mod.node.js +8 -0
  13. package/dist/tsconfig.tsbuildinfo +1 -0
  14. package/mod.deno.ts +128 -0
  15. package/mod.node.ts +10 -0
  16. package/mod.ts +3 -0
  17. package/package.json +19 -17
  18. package/tsconfig.json +14 -0
  19. package/esm/_dnt.shims.d.ts +0 -2
  20. package/esm/_dnt.shims.d.ts.map +0 -1
  21. package/esm/_dnt.shims.js +0 -57
  22. package/esm/bdd/mod.d.ts +0 -26
  23. package/esm/bdd/mod.d.ts.map +0 -1
  24. package/esm/bdd/mod.js +0 -78
  25. package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.d.ts +0 -26
  26. package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.d.ts.map +0 -1
  27. package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.js +0 -30
  28. package/esm/deps/jsr.io/@std/internal/1.0.12/assertion_state.d.ts +0 -150
  29. package/esm/deps/jsr.io/@std/internal/1.0.12/assertion_state.d.ts.map +0 -1
  30. package/esm/deps/jsr.io/@std/internal/1.0.12/assertion_state.js +0 -237
  31. package/esm/deps/jsr.io/@std/testing/1.0.16/_test_suite.d.ts +0 -78
  32. package/esm/deps/jsr.io/@std/testing/1.0.16/_test_suite.d.ts.map +0 -1
  33. package/esm/deps/jsr.io/@std/testing/1.0.16/_test_suite.js +0 -385
  34. package/esm/deps/jsr.io/@std/testing/1.0.16/bdd.d.ts +0 -336
  35. package/esm/deps/jsr.io/@std/testing/1.0.16/bdd.d.ts.map +0 -1
  36. package/esm/deps/jsr.io/@std/testing/1.0.16/bdd.js +0 -1057
  37. package/esm/package.json +0 -3
  38. package/esm/test-adapter/box.d.ts +0 -4
  39. package/esm/test-adapter/box.d.ts.map +0 -1
  40. package/esm/test-adapter/box.js +0 -15
  41. package/esm/test-adapter/mod.d.ts +0 -77
  42. package/esm/test-adapter/mod.d.ts.map +0 -1
  43. package/esm/test-adapter/mod.js +0 -89
  44. package/script/_dnt.shims.d.ts +0 -2
  45. package/script/_dnt.shims.d.ts.map +0 -1
  46. package/script/_dnt.shims.js +0 -60
  47. package/script/bdd/mod.d.ts +0 -26
  48. package/script/bdd/mod.d.ts.map +0 -1
  49. package/script/bdd/mod.js +0 -84
  50. package/script/deps/jsr.io/@std/assert/1.0.16/assertion_error.d.ts +0 -26
  51. package/script/deps/jsr.io/@std/assert/1.0.16/assertion_error.d.ts.map +0 -1
  52. package/script/deps/jsr.io/@std/assert/1.0.16/assertion_error.js +0 -34
  53. package/script/deps/jsr.io/@std/internal/1.0.12/assertion_state.d.ts +0 -150
  54. package/script/deps/jsr.io/@std/internal/1.0.12/assertion_state.d.ts.map +0 -1
  55. package/script/deps/jsr.io/@std/internal/1.0.12/assertion_state.js +0 -275
  56. package/script/deps/jsr.io/@std/testing/1.0.16/_test_suite.d.ts +0 -78
  57. package/script/deps/jsr.io/@std/testing/1.0.16/_test_suite.d.ts.map +0 -1
  58. package/script/deps/jsr.io/@std/testing/1.0.16/_test_suite.js +0 -389
  59. package/script/deps/jsr.io/@std/testing/1.0.16/bdd.d.ts +0 -336
  60. package/script/deps/jsr.io/@std/testing/1.0.16/bdd.d.ts.map +0 -1
  61. package/script/deps/jsr.io/@std/testing/1.0.16/bdd.js +0 -1068
  62. package/script/package.json +0 -3
  63. package/script/test-adapter/box.d.ts +0 -4
  64. package/script/test-adapter/box.d.ts.map +0 -1
  65. package/script/test-adapter/box.js +0 -19
  66. package/script/test-adapter/mod.d.ts +0 -77
  67. package/script/test-adapter/mod.d.ts.map +0 -1
  68. package/script/test-adapter/mod.js +0 -92
@@ -1,1057 +0,0 @@
1
- // Copyright 2018-2025 the Deno authors. MIT license.
2
- /**
3
- * A {@link https://en.wikipedia.org/wiki/Behavior-driven_development | BDD} interface
4
- * to `Deno.test()` API.
5
- *
6
- * With `@std/testing/bdd` module you can write your tests in a familiar format for
7
- * grouping tests and adding setup/teardown hooks used by other JavaScript testing
8
- * frameworks like Jasmine, Jest, and Mocha.
9
- *
10
- * The `describe` function creates a block that groups together several related
11
- * tests. The `it` function registers an individual test case.
12
- *
13
- * ## Hooks
14
- *
15
- * There are 4 types of hooks available for test suites. A test suite can have
16
- * multiples of each type of hook, they will be called in the order that they are
17
- * registered. The `afterEach` and `afterAll` hooks will be called whether or not
18
- * the test case passes. The *All hooks will be called once for the whole group
19
- * while the *Each hooks will be called for each individual test case.
20
- *
21
- * - `beforeAll`: Runs before all of the tests in the group.
22
- * - `afterAll`: Runs after all of the tests in the group finish.
23
- * - `beforeEach`: Runs before each of the individual test cases in the group.
24
- * - `afterEach`: Runs after each of the individual test cases in the group.
25
- *
26
- * If a hook is registered at the top level, a global test suite will be registered
27
- * and all tests will belong to it. Hooks registered at the top level must be
28
- * registered before any individual test cases or test suites.
29
- *
30
- * ## Focusing tests
31
- *
32
- * If you would like to run only specific test cases, you can do so by calling
33
- * `it.only` instead of `it`. If you would like to run only specific test suites,
34
- * you can do so by calling `describe.only` instead of `describe`.
35
- *
36
- * There is one limitation to this when using the flat test grouping style. When
37
- * `describe` is called without being nested, it registers the test with
38
- * `Deno.test`. If a child test case or suite is registered with `it.only` or
39
- * `describe.only`, it will be scoped to the top test suite instead of the file. To
40
- * make them the only tests that run in the file, you would need to register the
41
- * top test suite with `describe.only` too.
42
- *
43
- * ## Ignoring tests
44
- *
45
- * If you would like to not run specific individual test cases, you can do so by
46
- * calling `it.ignore` instead of `it`. If you would like to not run specific test
47
- * suites, you can do so by calling `describe.ignore` instead of `describe`.
48
- *
49
- * ## Sanitization options
50
- *
51
- * Like `Deno.TestDefinition`, the `DescribeDefinition` and `ItDefinition` have
52
- * sanitization options. They work in the same way.
53
- *
54
- * - `sanitizeExit`: Ensure the test case does not prematurely cause the process to
55
- * exit, for example via a call to Deno.exit. Defaults to true.
56
- * - `sanitizeOps`: Check that the number of async completed ops after the test is
57
- * the same as number of dispatched ops. Defaults to true.
58
- * - `sanitizeResources`: Ensure the test case does not "leak" resources - ie. the
59
- * resource table after the test has exactly the same contents as before the
60
- * test. Defaults to true.
61
- *
62
- * ## Permissions option
63
- *
64
- * Like `Deno.TestDefinition`, the `DescribeDefinition` and `ItDefinition` have a
65
- * `permissions` option. They specify the permissions that should be used to run an
66
- * individual test case or test suite. Set this to `"inherit"` to keep the calling
67
- * thread's permissions. Set this to `"none"` to revoke all permissions.
68
- *
69
- * This setting defaults to `"inherit"`.
70
- *
71
- * There is currently one limitation to this, you cannot use the permissions option
72
- * on an individual test case or test suite that belongs to another test suite.
73
- * That's because internally those tests are registered with `t.step` which does
74
- * not support the permissions option.
75
- *
76
- * ## Comparing to Deno\.test
77
- *
78
- * The default way of writing tests is using `Deno.test` and `t.step`. The
79
- * `describe` and `it` functions have similar call signatures to `Deno.test`,
80
- * making it easy to switch between the default style and the behavior-driven
81
- * development style of writing tests. Internally, `describe` and `it` are
82
- * registering tests with `Deno.test` and `t.step`.
83
- *
84
- * Below is an example of a test file using `Deno.test` and `t.step`. In the
85
- * following sections there are examples of how the same test could be written with
86
- * `describe` and `it` using nested test grouping, flat test grouping, or a mix of
87
- * both styles.
88
- *
89
- * ```ts
90
- * import {
91
- * assertEquals,
92
- * assertStrictEquals,
93
- * assertThrows,
94
- * } from "@std/assert";
95
- *
96
- * class User {
97
- * static users: Map<string, User> = new Map();
98
- * name: string;
99
- * age?: number;
100
- *
101
- * constructor(name: string) {
102
- * if (User.users.has(name)) {
103
- * throw new Deno.errors.AlreadyExists(`User ${name} already exists`);
104
- * }
105
- * this.name = name;
106
- * User.users.set(name, this);
107
- * }
108
- *
109
- * getAge(): number {
110
- * if (!this.age) {
111
- * throw new Error("Age unknown");
112
- * }
113
- * return this.age;
114
- * }
115
- *
116
- * setAge(age: number) {
117
- * this.age = age;
118
- * }
119
- * }
120
- *
121
- * Deno.test("User.users initially empty", () => {
122
- * assertEquals(User.users.size, 0);
123
- * });
124
- *
125
- * Deno.test("User constructor", () => {
126
- * try {
127
- * const user = new User("Kyle");
128
- * assertEquals(user.name, "Kyle");
129
- * assertStrictEquals(User.users.get("Kyle"), user);
130
- * } finally {
131
- * User.users.clear();
132
- * }
133
- * });
134
- *
135
- * Deno.test("User age", async (t) => {
136
- * const user = new User("Kyle");
137
- *
138
- * await t.step("getAge", () => {
139
- * assertThrows(() => user.getAge(), Error, "Age unknown");
140
- * user.age = 18;
141
- * assertEquals(user.getAge(), 18);
142
- * });
143
- *
144
- * await t.step("setAge", () => {
145
- * user.setAge(18);
146
- * assertEquals(user.getAge(), 18);
147
- * });
148
- * });
149
- * ```
150
- *
151
- * ### Nested test grouping
152
- *
153
- * Tests created within the callback of a `describe` function call will belong to
154
- * the new test suite it creates. The hooks can be created within it or be added to
155
- * the options argument for describe.
156
- *
157
- * ```ts
158
- * import {
159
- * assertEquals,
160
- * assertStrictEquals,
161
- * assertThrows,
162
- * } from "@std/assert";
163
- * import {
164
- * afterEach,
165
- * beforeEach,
166
- * describe,
167
- * it,
168
- * } from "@std/testing/bdd";
169
- *
170
- * class User {
171
- * static users: Map<string, User> = new Map();
172
- * name: string;
173
- * age?: number;
174
- *
175
- * constructor(name: string) {
176
- * if (User.users.has(name)) {
177
- * throw new Deno.errors.AlreadyExists(`User ${name} already exists`);
178
- * }
179
- * this.name = name;
180
- * User.users.set(name, this);
181
- * }
182
- *
183
- * getAge(): number {
184
- * if (!this.age) {
185
- * throw new Error("Age unknown");
186
- * }
187
- * return this.age;
188
- * }
189
- *
190
- * setAge(age: number) {
191
- * this.age = age;
192
- * }
193
- * }
194
- *
195
- * describe("User", () => {
196
- * it("users initially empty", () => {
197
- * assertEquals(User.users.size, 0);
198
- * });
199
- *
200
- * it("constructor", () => {
201
- * try {
202
- * const user = new User("Kyle");
203
- * assertEquals(user.name, "Kyle");
204
- * assertStrictEquals(User.users.get("Kyle"), user);
205
- * } finally {
206
- * User.users.clear();
207
- * }
208
- * });
209
- *
210
- * describe("age", () => {
211
- * let user: User;
212
- *
213
- * beforeEach(() => {
214
- * user = new User("Kyle");
215
- * });
216
- *
217
- * afterEach(() => {
218
- * User.users.clear();
219
- * });
220
- *
221
- * it("getAge", function () {
222
- * assertThrows(() => user.getAge(), Error, "Age unknown");
223
- * user.age = 18;
224
- * assertEquals(user.getAge(), 18);
225
- * });
226
- *
227
- * it("setAge", function () {
228
- * user.setAge(18);
229
- * assertEquals(user.getAge(), 18);
230
- * });
231
- * });
232
- * });
233
- * ```
234
- *
235
- * ### Flat test grouping
236
- *
237
- * The `describe` function returns a unique symbol that can be used to reference
238
- * the test suite for adding tests to it without having to create them within a
239
- * callback. The gives you the ability to have test grouping without any extra
240
- * indentation in front of the grouped tests.
241
- *
242
- * ```ts
243
- * import {
244
- * assertEquals,
245
- * assertStrictEquals,
246
- * assertThrows,
247
- * } from "@std/assert";
248
- * import {
249
- * describe,
250
- * it,
251
- * } from "@std/testing/bdd";
252
- *
253
- * class User {
254
- * static users: Map<string, User> = new Map();
255
- * name: string;
256
- * age?: number;
257
- *
258
- * constructor(name: string) {
259
- * if (User.users.has(name)) {
260
- * throw new Deno.errors.AlreadyExists(`User ${name} already exists`);
261
- * }
262
- * this.name = name;
263
- * User.users.set(name, this);
264
- * }
265
- *
266
- * getAge(): number {
267
- * if (!this.age) {
268
- * throw new Error("Age unknown");
269
- * }
270
- * return this.age;
271
- * }
272
- *
273
- * setAge(age: number) {
274
- * this.age = age;
275
- * }
276
- * }
277
- *
278
- * const userTests = describe("User");
279
- *
280
- * it(userTests, "users initially empty", () => {
281
- * assertEquals(User.users.size, 0);
282
- * });
283
- *
284
- * it(userTests, "constructor", () => {
285
- * try {
286
- * const user = new User("Kyle");
287
- * assertEquals(user.name, "Kyle");
288
- * assertStrictEquals(User.users.get("Kyle"), user);
289
- * } finally {
290
- * User.users.clear();
291
- * }
292
- * });
293
- *
294
- * const ageTests = describe({
295
- * name: "age",
296
- * suite: userTests,
297
- * beforeEach(this: { user: User }) {
298
- * this.user = new User("Kyle");
299
- * },
300
- * afterEach() {
301
- * User.users.clear();
302
- * },
303
- * });
304
- *
305
- * it(ageTests, "getAge", function () {
306
- * const { user } = this;
307
- * assertThrows(() => user.getAge(), Error, "Age unknown");
308
- * user.age = 18;
309
- * assertEquals(user.getAge(), 18);
310
- * });
311
- *
312
- * it(ageTests, "setAge", function () {
313
- * const { user } = this;
314
- * user.setAge(18);
315
- * assertEquals(user.getAge(), 18);
316
- * });
317
- * ```
318
- *
319
- * ### Mixed test grouping
320
- *
321
- * Both nested test grouping and flat test grouping can be used together. This can
322
- * be useful if you'd like to create deep groupings without all the extra
323
- * indentation in front of each line.
324
- *
325
- * ```ts
326
- * import {
327
- * assertEquals,
328
- * assertStrictEquals,
329
- * assertThrows,
330
- * } from "@std/assert";
331
- * import {
332
- * describe,
333
- * it,
334
- * } from "@std/testing/bdd";
335
- *
336
- * class User {
337
- * static users: Map<string, User> = new Map();
338
- * name: string;
339
- * age?: number;
340
- *
341
- * constructor(name: string) {
342
- * if (User.users.has(name)) {
343
- * throw new Deno.errors.AlreadyExists(`User ${name} already exists`);
344
- * }
345
- * this.name = name;
346
- * User.users.set(name, this);
347
- * }
348
- *
349
- * getAge(): number {
350
- * if (!this.age) {
351
- * throw new Error("Age unknown");
352
- * }
353
- * return this.age;
354
- * }
355
- *
356
- * setAge(age: number) {
357
- * this.age = age;
358
- * }
359
- * }
360
- *
361
- * describe("User", () => {
362
- * it("users initially empty", () => {
363
- * assertEquals(User.users.size, 0);
364
- * });
365
- *
366
- * it("constructor", () => {
367
- * try {
368
- * const user = new User("Kyle");
369
- * assertEquals(user.name, "Kyle");
370
- * assertStrictEquals(User.users.get("Kyle"), user);
371
- * } finally {
372
- * User.users.clear();
373
- * }
374
- * });
375
- *
376
- * const ageTests = describe({
377
- * name: "age",
378
- * beforeEach(this: { user: User }) {
379
- * this.user = new User("Kyle");
380
- * },
381
- * afterEach() {
382
- * User.users.clear();
383
- * },
384
- * });
385
- *
386
- * it(ageTests, "getAge", function () {
387
- * const { user } = this;
388
- * assertThrows(() => user.getAge(), Error, "Age unknown");
389
- * user.age = 18;
390
- * assertEquals(user.getAge(), 18);
391
- * });
392
- *
393
- * it(ageTests, "setAge", function () {
394
- * const { user } = this;
395
- * user.setAge(18);
396
- * assertEquals(user.getAge(), 18);
397
- * });
398
- * });
399
- * ```
400
- *
401
- * @module
402
- */
403
- import { getAssertionState } from "../../internal/1.0.12/assertion_state.js";
404
- import { AssertionError } from "../../assert/1.0.16/assertion_error.js";
405
- import { globalSanitizersState, TestSuiteInternal, } from "./_test_suite.js";
406
- /** Generates an ItDefinition from ItArgs. */
407
- function itDefinition(...args) {
408
- let [suiteOptionsOrNameOrFn, optionsOrNameOrFn, optionsOrFn, fn,] = args;
409
- let suite = undefined;
410
- let name;
411
- let options;
412
- if (typeof suiteOptionsOrNameOrFn === "object" &&
413
- typeof suiteOptionsOrNameOrFn.symbol === "symbol") {
414
- suite = suiteOptionsOrNameOrFn;
415
- }
416
- else {
417
- fn = optionsOrFn;
418
- optionsOrFn = optionsOrNameOrFn;
419
- optionsOrNameOrFn = suiteOptionsOrNameOrFn;
420
- }
421
- if (typeof optionsOrNameOrFn === "string") {
422
- name = optionsOrNameOrFn;
423
- if (typeof optionsOrFn === "function") {
424
- fn = optionsOrFn;
425
- options = {};
426
- }
427
- else {
428
- options = optionsOrFn;
429
- if (!fn)
430
- fn = options.fn;
431
- }
432
- }
433
- else if (typeof optionsOrNameOrFn === "function") {
434
- fn = optionsOrNameOrFn;
435
- name = fn.name;
436
- options = {};
437
- }
438
- else {
439
- options = optionsOrNameOrFn;
440
- if (typeof optionsOrFn === "function") {
441
- fn = optionsOrFn;
442
- }
443
- else {
444
- fn = options.fn;
445
- }
446
- name = options.name ?? fn.name;
447
- }
448
- return {
449
- ...(suite !== undefined ? { suite } : {}),
450
- ...options,
451
- name,
452
- fn,
453
- };
454
- }
455
- /**
456
- * Registers an individual test case.
457
- *
458
- * @example Usage
459
- * ```ts
460
- * import { describe, it } from "@std/testing/bdd";
461
- * import { assertEquals } from "@std/assert";
462
- *
463
- * describe("example", () => {
464
- * it("should pass", () => {
465
- * // test case
466
- * assertEquals(2 + 2, 4);
467
- * });
468
- * });
469
- * ```
470
- *
471
- * @typeParam T The self type of the function to implement the test case
472
- * @param args The test case
473
- */
474
- export function it(...args) {
475
- if (TestSuiteInternal.runningCount > 0) {
476
- throw new Error("Cannot register new test cases after already registered test cases start running");
477
- }
478
- const assertionState = getAssertionState();
479
- const options = itDefinition(...args);
480
- const { suite } = options;
481
- const testSuite = suite
482
- ? TestSuiteInternal.suites.get(suite.symbol)
483
- : TestSuiteInternal.current;
484
- if (!TestSuiteInternal.started)
485
- TestSuiteInternal.started = true;
486
- if (testSuite) {
487
- TestSuiteInternal.addStep(testSuite, options);
488
- }
489
- else {
490
- const { name, fn, ignore, only, permissions, sanitizeExit = globalSanitizersState.sanitizeExit, sanitizeOps = globalSanitizersState.sanitizeOps, sanitizeResources = globalSanitizersState.sanitizeResources, } = options;
491
- const opts = {
492
- name,
493
- async fn(t) {
494
- TestSuiteInternal.runningCount++;
495
- try {
496
- await fn.call({}, t);
497
- }
498
- finally {
499
- TestSuiteInternal.runningCount--;
500
- }
501
- try {
502
- if (assertionState.checkAssertionErrorState()) {
503
- throw new AssertionError("Expected at least one assertion to be called but received none");
504
- }
505
- if (assertionState.checkAssertionCountSatisfied()) {
506
- throw new AssertionError(`Expected exactly ${assertionState.assertionCount} ${assertionState.assertionCount === 1 ? "assertion" : "assertions"} to be called, ` +
507
- `but received ${assertionState.assertionTriggeredCount}`);
508
- }
509
- }
510
- finally {
511
- assertionState.resetAssertionState();
512
- }
513
- },
514
- };
515
- if (ignore !== undefined) {
516
- opts.ignore = ignore;
517
- }
518
- if (only !== undefined) {
519
- opts.only = only;
520
- }
521
- if (permissions !== undefined) {
522
- opts.permissions = permissions;
523
- }
524
- if (sanitizeExit !== undefined) {
525
- opts.sanitizeExit = sanitizeExit;
526
- }
527
- if (sanitizeOps !== undefined) {
528
- opts.sanitizeOps = sanitizeOps;
529
- }
530
- if (sanitizeResources !== undefined) {
531
- opts.sanitizeResources = sanitizeResources;
532
- }
533
- TestSuiteInternal.registerTest(opts);
534
- }
535
- }
536
- /**
537
- * Only execute this test case.
538
- *
539
- * @example Usage
540
- * ```ts
541
- * import { describe, it } from "@std/testing/bdd";
542
- * import { assertEquals } from "@std/assert";
543
- *
544
- * describe("example", () => {
545
- * it("should pass", () => {
546
- * assertEquals(2 + 2, 4);
547
- * });
548
- *
549
- * it.only("should pass too", () => {
550
- * assertEquals(3 + 4, 7);
551
- * });
552
- * });
553
- * ```
554
- *
555
- * @param args The test case
556
- */
557
- it.only = function itOnly(...args) {
558
- const options = itDefinition(...args);
559
- return it({
560
- ...options,
561
- only: true,
562
- });
563
- };
564
- /**
565
- * Ignore this test case.
566
- *
567
- * @example Usage
568
- * ```ts
569
- * import { describe, it } from "@std/testing/bdd";
570
- * import { assertEquals } from "@std/assert";
571
- *
572
- * describe("example", () => {
573
- * it("should pass", () => {
574
- * assertEquals(2 + 2, 4);
575
- * });
576
- *
577
- * it.ignore("should pass too", () => {
578
- * assertEquals(3 + 4, 7);
579
- * });
580
- * });
581
- * ```
582
- *
583
- * @param args The test case
584
- */
585
- it.ignore = function itIgnore(...args) {
586
- const options = itDefinition(...args);
587
- return it({
588
- ...options,
589
- ignore: true,
590
- });
591
- };
592
- /** Skip this test case.
593
- *
594
- * @example Usage
595
- * ```ts
596
- * import { describe, it } from "@std/testing/bdd";
597
- * import { assertEquals } from "@std/assert";
598
- *
599
- * describe("example", () => {
600
- * it("should pass", () => {
601
- * assertEquals(2 + 2, 4);
602
- * });
603
- *
604
- * it.skip("should pass too", () => {
605
- * assertEquals(3 + 4, 7);
606
- * });
607
- * });
608
- * ```
609
- *
610
- * @param args The test case
611
- */
612
- it.skip = function itSkip(...args) {
613
- it.ignore(...args);
614
- };
615
- /**
616
- * Alias of {@linkcode it}
617
- *
618
- * Registers an individual test case.
619
- *
620
- * @example Usage
621
- * ```ts
622
- * import { test } from "@std/testing/bdd";
623
- * import { assertEquals } from "@std/assert";
624
- *
625
- * test("a test case", () => {
626
- * // test case
627
- * assertEquals(2 + 2, 4);
628
- * });
629
- * ```
630
- *
631
- * @typeParam T The self type of the function to implement the test case
632
- * @param args The test case
633
- */
634
- export function test(...args) {
635
- it(...args);
636
- }
637
- /**
638
- * Only execute this test case.
639
- *
640
- * @example Usage
641
- * ```ts
642
- * import { describe, test } from "@std/testing/bdd";
643
- * import { assertEquals } from "@std/assert";
644
- *
645
- * describe("example", () => {
646
- * test("should pass", () => {
647
- * assertEquals(2 + 2, 4);
648
- * });
649
- *
650
- * test.only("should pass too", () => {
651
- * assertEquals(3 + 4, 7);
652
- * });
653
- * });
654
- * ```
655
- *
656
- * @param args The test case
657
- */
658
- test.only = function itOnly(...args) {
659
- it.only(...args);
660
- };
661
- /**
662
- * Ignore this test case.
663
- *
664
- * @example Usage
665
- * ```ts
666
- * import { describe, test } from "@std/testing/bdd";
667
- * import { assertEquals } from "@std/assert";
668
- *
669
- * describe("example", () => {
670
- * test("should pass", () => {
671
- * assertEquals(2 + 2, 4);
672
- * });
673
- *
674
- * test.ignore("should pass too", () => {
675
- * assertEquals(3 + 4, 7);
676
- * });
677
- * });
678
- * ```
679
- *
680
- * @param args The test case
681
- */
682
- test.ignore = function itIgnore(...args) {
683
- it.ignore(...args);
684
- };
685
- /** Skip this test case.
686
- *
687
- * @example Usage
688
- * ```ts
689
- * import { describe, test } from "@std/testing/bdd";
690
- * import { assertEquals } from "@std/assert";
691
- *
692
- * describe("example", () => {
693
- * test("should pass", () => {
694
- * assertEquals(2 + 2, 4);
695
- * });
696
- *
697
- * test.skip("should pass too", () => {
698
- * assertEquals(3 + 4, 7);
699
- * });
700
- * });
701
- * ```
702
- *
703
- * @param args The test case
704
- */
705
- test.skip = function itSkip(...args) {
706
- it.ignore(...args);
707
- };
708
- function addHook(name, fn) {
709
- if (!TestSuiteInternal.current) {
710
- if (TestSuiteInternal.started) {
711
- throw new Error("Cannot add global hooks after a global test is registered");
712
- }
713
- TestSuiteInternal.current = new TestSuiteInternal({
714
- name: "global",
715
- [name]: fn,
716
- });
717
- }
718
- else {
719
- TestSuiteInternal.setHook(TestSuiteInternal.current, name, fn);
720
- }
721
- }
722
- /**
723
- * Run some shared setup before all of the tests in the group.
724
- * Useful for async setup in `describe` blocks. Outside them,
725
- * top-level initialization code should be used instead.
726
- *
727
- * @example Usage
728
- * ```ts
729
- * import { describe, it, beforeAll } from "@std/testing/bdd";
730
- * import { assertEquals } from "@std/assert";
731
- *
732
- * beforeAll(() => {
733
- * console.log("beforeAll");
734
- * });
735
- *
736
- * describe("example", () => {
737
- * it("should pass", () => {
738
- * // test case
739
- * assertEquals(2 + 2, 4);
740
- * });
741
- * });
742
- * ```
743
- *
744
- * @typeParam T The self type of the function
745
- * @param fn The function to implement the setup behavior.
746
- */
747
- export function beforeAll(fn) {
748
- addHook("beforeAll", fn);
749
- }
750
- /**
751
- * Alias of {@linkcode beforeAll}
752
- *
753
- * Run some shared setup before all of the tests in the suite.
754
- *
755
- * @example Usage
756
- * ```ts
757
- * import { describe, it, before } from "@std/testing/bdd";
758
- * import { assertEquals } from "@std/assert";
759
- *
760
- * before(() => {
761
- * console.log("before");
762
- * });
763
- *
764
- * describe("example", () => {
765
- * it("should pass", () => {
766
- * // test case
767
- * assertEquals(2 + 2, 4);
768
- * });
769
- * });
770
- * ```
771
- *
772
- * @typeParam T The self type of the function
773
- * @param fn The function to implement the setup behavior.
774
- */
775
- export function before(fn) {
776
- beforeAll(fn);
777
- }
778
- /**
779
- * Run some shared teardown after all of the tests in the suite.
780
- *
781
- * @example Usage
782
- * ```ts
783
- * import { describe, it, afterAll } from "@std/testing/bdd";
784
- * import { assertEquals } from "@std/assert";
785
- *
786
- * afterAll(() => {
787
- * console.log("afterAll");
788
- * });
789
- *
790
- * describe("example", () => {
791
- * it("should pass", () => {
792
- * // test case
793
- * assertEquals(2 + 2, 4);
794
- * });
795
- * });
796
- * ```
797
- *
798
- * @typeParam T The self type of the function
799
- * @param fn The function to implement the teardown behavior.
800
- */
801
- export function afterAll(fn) {
802
- addHook("afterAll", fn);
803
- }
804
- /**
805
- * Alias of {@linkcode afterAll}.
806
- *
807
- * Run some shared teardown after all of the tests in the suite.
808
- *
809
- * @example Usage
810
- * ```ts
811
- * import { describe, it, after } from "@std/testing/bdd";
812
- * import { assertEquals } from "@std/assert";
813
- *
814
- * after(() => {
815
- * console.log("after");
816
- * });
817
- *
818
- * describe("example", () => {
819
- * it("should pass", () => {
820
- * // test case
821
- * assertEquals(2 + 2, 4);
822
- * });
823
- * });
824
- * ```
825
- *
826
- * @typeParam T The self type of the function
827
- * @param fn The function to implement the teardown behavior.
828
- */
829
- export function after(fn) {
830
- afterAll(fn);
831
- }
832
- /**
833
- * Run some shared setup before each test in the suite.
834
- *
835
- * @example Usage
836
- * ```ts
837
- * import { describe, it, beforeEach } from "@std/testing/bdd";
838
- * import { assertEquals } from "@std/assert";
839
- *
840
- * beforeEach(() => {
841
- * console.log("beforeEach");
842
- * });
843
- *
844
- * describe("example", () => {
845
- * it("should pass", () => {
846
- * // test case
847
- * assertEquals(2 + 2, 4);
848
- * });
849
- * });
850
- * ```
851
- *
852
- * @typeParam T The self type of the function
853
- * @param fn The function to implement the shared setup behavior
854
- */
855
- export function beforeEach(fn) {
856
- addHook("beforeEach", fn);
857
- }
858
- /**
859
- * Run some shared teardown after each test in the suite.
860
- *
861
- * @example Usage
862
- * ```ts
863
- * import { describe, it, afterEach } from "@std/testing/bdd";
864
- * import { assertEquals } from "@std/assert";
865
- *
866
- * afterEach(() => {
867
- * console.log("afterEach");
868
- * });
869
- *
870
- * describe("example", () => {
871
- * it("should pass", () => {
872
- * // test case
873
- * assertEquals(2 + 2, 4);
874
- * });
875
- * });
876
- * ```
877
- *
878
- * @typeParam T The self type of the function
879
- * @param fn The function to implement the shared teardown behavior
880
- */
881
- export function afterEach(fn) {
882
- addHook("afterEach", fn);
883
- }
884
- /** Generates a DescribeDefinition from DescribeArgs. */
885
- function describeDefinition(...args) {
886
- let [suiteOptionsOrNameOrFn, optionsOrNameOrFn, optionsOrFn, fn,] = args;
887
- let suite = undefined;
888
- let name;
889
- let options;
890
- if (typeof suiteOptionsOrNameOrFn === "object" &&
891
- typeof suiteOptionsOrNameOrFn.symbol === "symbol") {
892
- suite = suiteOptionsOrNameOrFn;
893
- }
894
- else {
895
- fn = optionsOrFn;
896
- optionsOrFn = optionsOrNameOrFn;
897
- optionsOrNameOrFn = suiteOptionsOrNameOrFn;
898
- }
899
- if (typeof optionsOrNameOrFn === "string") {
900
- name = optionsOrNameOrFn;
901
- if (typeof optionsOrFn === "function") {
902
- fn = optionsOrFn;
903
- options = {};
904
- }
905
- else {
906
- options = optionsOrFn ?? {};
907
- if (fn === undefined) {
908
- fn = options.fn;
909
- }
910
- }
911
- }
912
- else if (typeof optionsOrNameOrFn === "function") {
913
- fn = optionsOrNameOrFn;
914
- name = fn.name;
915
- options = {};
916
- }
917
- else {
918
- options = optionsOrNameOrFn ?? {};
919
- if (typeof optionsOrFn === "function") {
920
- fn = optionsOrFn;
921
- }
922
- else {
923
- fn = options.fn;
924
- }
925
- name = options.name ?? fn?.name ?? "";
926
- }
927
- if (suite === undefined) {
928
- suite = options.suite;
929
- }
930
- if (suite === undefined && TestSuiteInternal.current) {
931
- const { symbol } = TestSuiteInternal.current;
932
- suite = { symbol };
933
- }
934
- return {
935
- ...options,
936
- suite: suite,
937
- name,
938
- fn: fn,
939
- };
940
- }
941
- /**
942
- * Registers a test suite.
943
- *
944
- * @example Usage
945
- * ```ts
946
- * import { describe, it } from "@std/testing/bdd";
947
- * import { assertEquals } from "@std/assert";
948
- *
949
- * describe("example", () => {
950
- * it("should pass", () => {
951
- * // test case
952
- * assertEquals(2 + 2, 4);
953
- * });
954
- * });
955
- * ```
956
- *
957
- * @typeParam T The self type of the test suite body.
958
- * @param args The test suite body.
959
- * @returns The test suite
960
- */
961
- export function describe(...args) {
962
- if (TestSuiteInternal.runningCount > 0) {
963
- throw new Error("Cannot register new test suites after already registered test cases start running");
964
- }
965
- const options = describeDefinition(...args);
966
- if (!TestSuiteInternal.started)
967
- TestSuiteInternal.started = true;
968
- const { symbol } = new TestSuiteInternal(options);
969
- return { symbol };
970
- }
971
- /**
972
- * Only execute this test suite.
973
- *
974
- * @example Usage
975
- * ```ts
976
- * import { describe, it, beforeAll } from "@std/testing/bdd";
977
- * import { assertEquals } from "@std/assert";
978
- *
979
- * describe("example", () => {
980
- * it("should pass", () => {
981
- * assertEquals(2 + 2, 4);
982
- * });
983
- * });
984
- *
985
- * // Only this test suite will run
986
- * describe.only("example 2", () => {
987
- * it("should pass too", () => {
988
- * assertEquals(3 + 4, 7);
989
- * });
990
- * });
991
- * ```
992
- *
993
- * @param args The test suite body
994
- */
995
- describe.only = function describeOnly(...args) {
996
- const options = describeDefinition(...args);
997
- return describe({
998
- ...options,
999
- only: true,
1000
- });
1001
- };
1002
- /**
1003
- * Ignore the test suite.
1004
- *
1005
- * @example Usage
1006
- * ```ts
1007
- * import { describe, it, beforeAll } from "@std/testing/bdd";
1008
- * import { assertEquals } from "@std/assert";
1009
- *
1010
- * describe("example", () => {
1011
- * it("should pass", () => {
1012
- * assertEquals(2 + 2, 4);
1013
- * });
1014
- * });
1015
- *
1016
- * describe.ignore("example 2", () => {
1017
- * it("should pass too", () => {
1018
- * assertEquals(3 + 4, 7);
1019
- * });
1020
- * });
1021
- * ```
1022
- *
1023
- * @param args The test suite body
1024
- */
1025
- describe.ignore = function describeIgnore(...args) {
1026
- const options = describeDefinition(...args);
1027
- return describe({
1028
- ...options,
1029
- ignore: true,
1030
- });
1031
- };
1032
- /**
1033
- * Skip the test suite.
1034
- *
1035
- * @example Usage
1036
- * ```ts
1037
- * import { describe, it, beforeAll } from "@std/testing/bdd";
1038
- * import { assertEquals } from "@std/assert";
1039
- *
1040
- * describe("example", () => {
1041
- * it("should pass", () => {
1042
- * assertEquals(2 + 2, 4);
1043
- * });
1044
- * });
1045
- *
1046
- * describe.skip("example 2", () => {
1047
- * it("should pass too", () => {
1048
- * assertEquals(3 + 4, 7);
1049
- * });
1050
- * });
1051
- * ```
1052
- *
1053
- * @param args The test suite body
1054
- */
1055
- describe.skip = function describeSkip(...args) {
1056
- return describe.ignore(...args);
1057
- };