Skip to content
This repository was archived by the owner on Apr 18, 2022. It is now read-only.

Commit a8b5e81

Browse files
authored
Merge pull request #32 from udibo/dev
Add support for multiple of each test hook type
2 parents fee3973 + 0d7ec26 commit a8b5e81

File tree

6 files changed

+127
-93
lines changed

6 files changed

+127
-93
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Test Suite
22

3-
[![version](https://img.shields.io/badge/release-0.13.0-success)](https://deno.land/x/test_suite@0.13.0)
4-
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/test_suite@0.13.0/mod.ts)
3+
[![version](https://img.shields.io/badge/release-0.14.0-success)](https://deno.land/x/test_suite@0.14.0)
4+
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/test_suite@0.14.0/mod.ts)
55
[![CI](https://github.com/udibo/test_suite/workflows/CI/badge.svg)](https://github.com/udibo/test_suite/actions?query=workflow%3ACI)
66
[![codecov](https://codecov.io/gh/udibo/test_suite/branch/main/graph/badge.svg?token=EFKGY72AAV)](https://codecov.io/gh/udibo/test_suite)
77
[![license](https://img.shields.io/github/license/udibo/test_suite)](https://github.com/udibo/test_suite/blob/master/LICENSE)
@@ -26,12 +26,12 @@ also be imported directly from GitHub using raw content URLs.
2626

2727
```ts
2828
// Import from Deno's third party module registry
29-
import { describe, it } from "https://deno.land/x/test_suite@0.13.0/mod.ts";
29+
import { describe, it } from "https://deno.land/x/test_suite@0.14.0/mod.ts";
3030
// Import from GitHub
3131
import {
3232
describe,
3333
it,
34-
} from "https://raw.githubusercontent.com/udibo/test_suite/0.13.0/mod.ts";
34+
} from "https://raw.githubusercontent.com/udibo/test_suite/0.14.0/mod.ts";
3535
```
3636

3737
## Usage
@@ -54,7 +54,7 @@ except they are called before and after each individual test.
5454
Below are some examples of how to use `describe` and `it` in tests.
5555

5656
See
57-
[deno docs](https://doc.deno.land/https/deno.land/x/test_suite@0.13.0/mod.ts)
57+
[deno docs](https://doc.deno.land/https/deno.land/x/test_suite@0.14.0/mod.ts)
5858
for more information.
5959

6060
### Nested test grouping
@@ -67,13 +67,13 @@ import {
6767
beforeEach,
6868
describe,
6969
it,
70-
} from "https://deno.land/x/test_suite@0.13.0/mod.ts";
71-
import { assertEquals } from "https://deno.land/std@0.129.0/testing/asserts.ts";
70+
} from "https://deno.land/x/test_suite@0.14.0/mod.ts";
71+
import { assertEquals } from "https://deno.land/std@0.130.0/testing/asserts.ts";
7272
import {
7373
getUser,
7474
resetUsers,
7575
User,
76-
} from "https://deno.land/x/test_suite@0.13.0/examples/user.ts";
76+
} from "https://deno.land/x/test_suite@0.14.0/examples/user.ts";
7777

7878
describe("user describe", () => {
7979
let user: User;
@@ -131,13 +131,13 @@ test result: ok. 1 passed (5 steps); 0 failed; 0 ignored; 0 measured; 0 filtered
131131
The example below can be found [here](examples/user_flat_test.ts).
132132

133133
```ts
134-
import { describe, it } from "https://deno.land/x/test_suite@0.13.0/mod.ts";
135-
import { assertEquals } from "https://deno.land/std@0.129.0/testing/asserts.ts";
134+
import { describe, it } from "https://deno.land/x/test_suite@0.14.0/mod.ts";
135+
import { assertEquals } from "https://deno.land/std@0.130.0/testing/asserts.ts";
136136
import {
137137
getUser,
138138
resetUsers,
139139
User,
140-
} from "https://deno.land/x/test_suite@0.13.0/examples/user.ts";
140+
} from "https://deno.land/x/test_suite@0.14.0/examples/user.ts";
141141

142142
interface UserContext {
143143
user: User;

describe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
export type { DescribeDefinition, ItDefinition, TestSuite };
99

1010
/** The arguments for an ItFunction. */
11-
type ItArgs<T> =
11+
export type ItArgs<T> =
1212
| [options: ItDefinition<T>]
1313
| [
1414
name: string,
@@ -237,7 +237,7 @@ export function afterEach<T>(
237237
}
238238

239239
/** The arguments for a DescribeFunction. */
240-
type DescribeArgs<T> =
240+
export type DescribeArgs<T> =
241241
| [options: DescribeDefinition<T>]
242242
| [name: string]
243243
| [

describe_test.ts

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,13 @@ import {
1313
it,
1414
} from "./describe.ts";
1515
import { TestSuiteInternal } from "./test_suite.ts";
16-
import {
17-
assertSpyCall,
18-
assertSpyCalls,
19-
FakeTime,
20-
Spy,
21-
spy,
22-
stub,
23-
} from "./test_deps.ts";
16+
import { assertSpyCall, assertSpyCalls, Spy, spy, stub } from "./test_deps.ts";
2417

2518
Deno.test("global", async (t) => {
2619
class TestContext implements Deno.TestContext {
2720
steps: TestContext[];
2821
spies: {
29-
step: Spy<void>;
22+
step: Spy;
3023
};
3124

3225
constructor() {
@@ -78,32 +71,37 @@ Deno.test("global", async (t) => {
7871
eachTimer: number;
7972
}
8073

81-
function beforeAllFns() {
74+
let timerIdx = 1;
75+
const timers = new Map<number, number>();
76+
function hookFns() {
77+
timerIdx = 1;
78+
timers.clear();
8279
return {
8380
beforeAllFn: spy(async function (this: GlobalContext) {
8481
await Promise.resolve();
85-
this.allTimer = setTimeout(() => {}, Number.MAX_SAFE_INTEGER);
82+
this.allTimer = timerIdx++;
83+
timers.set(this.allTimer, setTimeout(() => {}, 10000));
8684
}),
8785
afterAllFn: spy(async function (this: GlobalContext) {
8886
await Promise.resolve();
89-
clearTimeout(this.allTimer);
87+
clearTimeout(timers.get(this.allTimer));
9088
}),
9189
beforeEachFn: spy(async function (this: GlobalContext) {
9290
await Promise.resolve();
93-
this.eachTimer = setTimeout(() => {}, Number.MAX_SAFE_INTEGER);
91+
this.eachTimer = timerIdx++;
92+
timers.set(this.eachTimer, setTimeout(() => {}, 10000));
9493
}),
9594
afterEachFn: spy(async function (this: GlobalContext) {
9695
await Promise.resolve();
97-
clearTimeout(this.eachTimer);
96+
clearTimeout(timers.get(this.eachTimer));
9897
}),
9998
};
10099
}
101100

102101
await t.step("global hooks", async () => {
103102
const test = stub(Deno, "test"),
104-
time = new FakeTime(),
105103
fns = [spy(), spy()],
106-
{ beforeAllFn, afterAllFn, beforeEachFn, afterEachFn } = beforeAllFns();
104+
{ beforeAllFn, afterAllFn, beforeEachFn, afterEachFn } = hookFns();
107105

108106
try {
109107
beforeAll(beforeAllFn);
@@ -131,7 +129,6 @@ Deno.test("global", async (t) => {
131129
} finally {
132130
TestSuiteInternal.reset();
133131
test.restore();
134-
time.restore();
135132
}
136133

137134
let fn = fns[0];
@@ -159,7 +156,7 @@ Deno.test("global", async (t) => {
159156
await t.step("it", async (t) => {
160157
async function assertOptions<T>(
161158
expectedOptions: Omit<Deno.TestDefinition, "name" | "fn">,
162-
cb: (fn: Spy<void>) => void,
159+
cb: (fn: Spy) => void,
163160
): Promise<void> {
164161
const test = stub(Deno, "test");
165162
const fn = spy();
@@ -195,13 +192,13 @@ Deno.test("global", async (t) => {
195192
}
196193

197194
async function assertMinimumOptions(
198-
cb: (fn: Spy<void>) => void,
195+
cb: (fn: Spy) => void,
199196
): Promise<void> {
200197
await assertOptions({}, cb);
201198
}
202199

203200
async function assertAllOptions(
204-
cb: (fn: Spy<void>) => void,
201+
cb: (fn: Spy) => void,
205202
): Promise<void> {
206203
await assertOptions(baseOptions, cb);
207204
}
@@ -334,13 +331,13 @@ Deno.test("global", async (t) => {
334331

335332
await t.step("only", async (t) => {
336333
async function assertMinimumOptions(
337-
cb: (fn: Spy<void>) => void,
334+
cb: (fn: Spy) => void,
338335
): Promise<void> {
339336
await assertOptions({ only: true }, cb);
340337
}
341338

342339
async function assertAllOptions(
343-
cb: (fn: Spy<void>) => void,
340+
cb: (fn: Spy) => void,
344341
): Promise<void> {
345342
await assertOptions({ ...baseOptions, only: true }, cb);
346343
}
@@ -480,13 +477,13 @@ Deno.test("global", async (t) => {
480477

481478
await t.step("ignore", async (t) => {
482479
async function assertMinimumOptions(
483-
cb: (fn: Spy<void>) => void,
480+
cb: (fn: Spy) => void,
484481
): Promise<void> {
485482
await assertOptions({ ignore: true }, cb);
486483
}
487484

488485
async function assertAllOptions(
489-
cb: (fn: Spy<void>) => void,
486+
cb: (fn: Spy) => void,
490487
): Promise<void> {
491488
await assertOptions({ ...baseOptions, ignore: true }, cb);
492489
}
@@ -628,7 +625,7 @@ Deno.test("global", async (t) => {
628625
await t.step("describe", async (t) => {
629626
async function assertOptions(
630627
expectedOptions: Omit<Deno.TestDefinition, "name" | "fn">,
631-
cb: (fns: Spy<void>[]) => void,
628+
cb: (fns: Spy[]) => void,
632629
): Promise<void> {
633630
const test = stub(Deno, "test");
634631
const fns = [spy(), spy()];
@@ -676,13 +673,13 @@ Deno.test("global", async (t) => {
676673
}
677674

678675
async function assertMinimumOptions(
679-
cb: (fns: Spy<void>[]) => void,
676+
cb: (fns: Spy[]) => void,
680677
): Promise<void> {
681678
await assertOptions({}, cb);
682679
}
683680

684681
async function assertAllOptions(
685-
cb: (fns: Spy<void>[]) => void,
682+
cb: (fns: Spy[]) => void,
686683
): Promise<void> {
687684
await assertOptions({ ...baseOptions }, cb);
688685
}
@@ -851,13 +848,13 @@ Deno.test("global", async (t) => {
851848

852849
await t.step("only", async (t) => {
853850
async function assertMinimumOptions(
854-
cb: (fns: Spy<void>[]) => void,
851+
cb: (fns: Spy[]) => void,
855852
): Promise<void> {
856853
await assertOptions({ only: true }, cb);
857854
}
858855

859856
async function assertAllOptions(
860-
cb: (fns: Spy<void>[]) => void,
857+
cb: (fns: Spy[]) => void,
861858
): Promise<void> {
862859
await assertOptions({ ...baseOptions, only: true }, cb);
863860
}
@@ -1042,13 +1039,13 @@ Deno.test("global", async (t) => {
10421039

10431040
await t.step("ignore", async (t) => {
10441041
async function assertMinimumOptions(
1045-
cb: (fns: Spy<void>[]) => void,
1042+
cb: (fns: Spy[]) => void,
10461043
): Promise<void> {
10471044
await assertOptions({ ignore: true }, cb);
10481045
}
10491046

10501047
async function assertAllOptions(
1051-
cb: (fns: Spy<void>[]) => void,
1048+
cb: (fns: Spy[]) => void,
10521049
): Promise<void> {
10531050
await assertOptions({ ...baseOptions, ignore: true }, cb);
10541051
}
@@ -1233,7 +1230,7 @@ Deno.test("global", async (t) => {
12331230

12341231
await t.step("nested only", async (t) => {
12351232
async function assertOnly(
1236-
cb: (fns: Spy<void>[]) => void,
1233+
cb: (fns: Spy[]) => void,
12371234
): Promise<void> {
12381235
const test = stub(Deno, "test");
12391236
const fns = [spy(), spy(), spy()];
@@ -1327,19 +1324,17 @@ Deno.test("global", async (t) => {
13271324
async function assertHooks(
13281325
cb: (
13291326
options: {
1330-
beforeAllFn: Spy<void>;
1331-
afterAllFn: Spy<void>;
1332-
beforeEachFn: Spy<void>;
1333-
afterEachFn: Spy<void>;
1334-
fns: Spy<void>[];
1327+
beforeAllFn: Spy;
1328+
afterAllFn: Spy;
1329+
beforeEachFn: Spy;
1330+
afterEachFn: Spy;
1331+
fns: Spy[];
13351332
},
13361333
) => void,
13371334
) {
13381335
const test = stub(Deno, "test"),
1339-
time = new FakeTime(),
13401336
fns = [spy(), spy()],
1341-
{ beforeAllFn, afterAllFn, beforeEachFn, afterEachFn } =
1342-
beforeAllFns();
1337+
{ beforeAllFn, afterAllFn, beforeEachFn, afterEachFn } = hookFns();
13431338

13441339
try {
13451340
cb({ beforeAllFn, afterAllFn, beforeEachFn, afterEachFn, fns });
@@ -1360,7 +1355,6 @@ Deno.test("global", async (t) => {
13601355
} finally {
13611356
TestSuiteInternal.reset();
13621357
test.restore();
1363-
time.restore();
13641358
}
13651359

13661360
let fn = fns[0];
@@ -1434,10 +1428,8 @@ Deno.test("global", async (t) => {
14341428
"nested",
14351429
async () => {
14361430
const test = stub(Deno, "test"),
1437-
time = new FakeTime(),
14381431
fns = [spy(), spy()],
1439-
{ beforeAllFn, afterAllFn, beforeEachFn, afterEachFn } =
1440-
beforeAllFns();
1432+
{ beforeAllFn, afterAllFn, beforeEachFn, afterEachFn } = hookFns();
14411433

14421434
try {
14431435
describe("example", () => {
@@ -1474,7 +1466,6 @@ Deno.test("global", async (t) => {
14741466
} finally {
14751467
TestSuiteInternal.reset();
14761468
test.restore();
1477-
time.restore();
14781469
}
14791470

14801471
let fn = fns[0];
@@ -1509,34 +1500,34 @@ Deno.test("global", async (t) => {
15091500
"nested with hooks",
15101501
async () => {
15111502
const test = stub(Deno, "test"),
1512-
time = new FakeTime(),
15131503
fns = [spy(), spy()],
1514-
{ beforeAllFn, afterAllFn, beforeEachFn, afterEachFn } =
1515-
beforeAllFns(),
1504+
{ beforeAllFn, afterAllFn, beforeEachFn, afterEachFn } = hookFns(),
15161505
beforeAllFnNested = spy(async function (this: NestedContext) {
15171506
await Promise.resolve();
1518-
this.allTimerNested = setTimeout(
1519-
() => {},
1520-
Number.MAX_SAFE_INTEGER,
1507+
this.allTimerNested = timerIdx++;
1508+
timers.set(
1509+
this.allTimerNested,
1510+
setTimeout(() => {}, 10000),
15211511
);
15221512
}),
15231513
afterAllFnNested = spy(
15241514
async function (this: NestedContext) {
15251515
await Promise.resolve();
1526-
clearTimeout(this.allTimerNested);
1516+
clearTimeout(timers.get(this.allTimerNested));
15271517
},
15281518
),
15291519
beforeEachFnNested = spy(async function (this: NestedContext) {
15301520
await Promise.resolve();
1531-
this.eachTimerNested = setTimeout(
1532-
() => {},
1533-
Number.MAX_SAFE_INTEGER,
1521+
this.eachTimerNested = timerIdx++;
1522+
timers.set(
1523+
this.eachTimerNested,
1524+
setTimeout(() => {}, 10000),
15341525
);
15351526
}),
15361527
afterEachFnNested = spy(
15371528
async function (this: NestedContext) {
15381529
await Promise.resolve();
1539-
clearTimeout(this.eachTimerNested);
1530+
clearTimeout(timers.get(this.eachTimerNested));
15401531
},
15411532
);
15421533

@@ -1581,7 +1572,6 @@ Deno.test("global", async (t) => {
15811572
} finally {
15821573
TestSuiteInternal.reset();
15831574
test.restore();
1584-
time.restore();
15851575
}
15861576

15871577
let fn = fns[0];

0 commit comments

Comments
 (0)