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

Commit c61f094

Browse files
authored
Merge pull request #12 from udibo/dev
Fix issues with beforeAll and afterAll hooks
2 parents 3aeda46 + 229db94 commit c61f094

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

README.md

Lines changed: 9 additions & 9 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.9.2-success)](https://deno.land/x/test_suite@0.9.2)
4-
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/test_suite@0.9.2/mod.ts)
3+
[![version](https://img.shields.io/badge/release-0.9.3-success)](https://deno.land/x/test_suite@0.9.3)
4+
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/test_suite@0.9.3/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/master/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,16 +26,16 @@ also be imported directly from GitHub using raw content URLs.
2626

2727
```ts
2828
// Import from Deno's third party module registry
29-
import { TestSuite, test } from "https://deno.land/x/test_suite@0.9.2/mod.ts";
29+
import { TestSuite, test } from "https://deno.land/x/test_suite@0.9.3/mod.ts";
3030
// Import from GitHub
31-
import { TestSuite, test } "https://raw.githubusercontent.com/udibo/test_suite/0.9.2/mod.ts";
31+
import { TestSuite, test } "https://raw.githubusercontent.com/udibo/test_suite/0.9.3/mod.ts";
3232
```
3333
3434
## Usage
3535
3636
Below are some examples of how to use TestSuite and test in tests.
3737
38-
See [deno docs](https://doc.deno.land/https/deno.land/x/test_suite@0.9.2/mod.ts)
38+
See [deno docs](https://doc.deno.land/https/deno.land/x/test_suite@0.9.3/mod.ts)
3939
for more information.
4040
4141
### TestSuite
@@ -56,13 +56,13 @@ except they are called before and after each individual test.
5656
The example test below can be found in the example directory.
5757
5858
```ts
59-
import { test, TestSuite } from "https://deno.land/x/test_suite@0.9.2/mod.ts";
59+
import { test, TestSuite } from "https://deno.land/x/test_suite@0.9.3/mod.ts";
6060
import { assertEquals } from "https://deno.land/std@0.117.0/testing/asserts.ts";
6161
import {
6262
getUser,
6363
resetUsers,
6464
User,
65-
} from "https://deno.land/x/test_suite@0.9.2/example/user.ts";
65+
} from "https://deno.land/x/test_suite@0.9.3/example/user.ts";
6666

6767
interface UserSuiteContext {
6868
user: User;
@@ -139,13 +139,13 @@ import {
139139
beforeEach,
140140
describe,
141141
it,
142-
} from "https://deno.land/x/test_suite@0.9.2/mod.ts";
142+
} from "https://deno.land/x/test_suite@0.9.3/mod.ts";
143143
import { assertEquals } from "https://deno.land/std@0.117.0/testing/asserts.ts";
144144
import {
145145
getUser,
146146
resetUsers,
147147
User,
148-
} from "https://deno.land/x/test_suite@0.9.2/examples/user.ts";
148+
} from "https://deno.land/x/test_suite@0.9.3/examples/user.ts";
149149

150150
describe("user describe", () => {
151151
let user: User;

test_suite.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -323,29 +323,35 @@ export class TestSuite<T> {
323323
this.hooks = {};
324324
TestSuite.setHooks(this, options);
325325
this.beforeAll = async () => {
326-
if (this.suite) {
327-
await this.suite.beforeAll();
328-
this.context = { ...this.suite.context, ...this.context };
329-
}
330-
if (this.sanitizeOps ?? true) {
331-
this.beforeAllMetrics = await getMetrics();
332-
}
333-
if (this.sanitizeResources ?? true) {
334-
this.beforeAllResources = Deno.resources();
326+
try {
327+
if (this.suite && !this.suite.started) {
328+
await this.suite.beforeAll();
329+
this.context = { ...this.suite.context, ...this.context };
330+
}
331+
} finally {
332+
this.started = true;
333+
if (this.sanitizeOps ?? true) {
334+
this.beforeAllMetrics = await getMetrics();
335+
}
336+
if (this.sanitizeResources ?? true) {
337+
this.beforeAllResources = Deno.resources();
338+
}
335339
}
336340
if (this.hooks.beforeAll) await this.hooks.beforeAll(this.context as T);
337-
this.started = true;
338341
};
339342
this.afterAll = async () => {
340-
if (this.hooks.afterAll) await this.hooks.afterAll(this.context as T);
341-
if (this.sanitizeOps ?? true) {
342-
await assertOps("suite", this.beforeAllMetrics!);
343-
}
344-
if (this.sanitizeResources ?? true) {
345-
assertResources("suite", this.beforeAllResources!);
346-
}
347-
if (this.suite && this.suite.last === this.last) {
348-
await this.suite.afterAll();
343+
try {
344+
if (this.hooks.afterAll) await this.hooks.afterAll(this.context as T);
345+
if (this.sanitizeOps ?? true) {
346+
await assertOps("suite", this.beforeAllMetrics!);
347+
}
348+
if (this.sanitizeResources ?? true) {
349+
assertResources("suite", this.beforeAllResources!);
350+
}
351+
} finally {
352+
if (this.suite && this.suite.last === this.last) {
353+
await this.suite.afterAll();
354+
}
349355
}
350356
};
351357
this.beforeEach = async (context: T) => {

0 commit comments

Comments
 (0)