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

Commit be29e39

Browse files
committed
Replace context arg with this
1 parent 810a912 commit be29e39

File tree

6 files changed

+118
-117
lines changed

6 files changed

+118
-117
lines changed

README.md

Lines changed: 20 additions & 18 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.12.0-success)](https://deno.land/x/test_suite@0.12.0)
4-
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/test_suite@0.12.0/mod.ts)
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)
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.12.0/mod.ts";
29+
import { describe, it } from "https://deno.land/x/test_suite@0.13.0/mod.ts";
3030
// Import from GitHub
3131
import {
3232
describe,
3333
it,
34-
} from "https://raw.githubusercontent.com/udibo/test_suite/0.12.0/mod.ts";
34+
} from "https://raw.githubusercontent.com/udibo/test_suite/0.13.0/mod.ts";
3535
```
3636

3737
## Usage
@@ -44,15 +44,17 @@ specifically set them.
4444
The beforeAll and afterAll hook options can be used to do something before and
4545
after all the tests in the suite run. If you would like to set values for all
4646
tests within the suite, you can create a context interface that defines all the
47-
values available to the tests that are defined in the beforeAll function.
47+
values available to the tests that are defined in the beforeAll function using
48+
the this argument. An example of this can be found in the section for
49+
[flat test grouping](#flat-test-grouping).
4850

4951
The beforeEach and afterEach hook options are similar to beforeAll and afterAll
5052
except they are called before and after each individual test.
5153

5254
Below are some examples of how to use `describe` and `it` in tests.
5355

5456
See
55-
[deno docs](https://doc.deno.land/https/deno.land/x/test_suite@0.12.0/mod.ts)
57+
[deno docs](https://doc.deno.land/https/deno.land/x/test_suite@0.13.0/mod.ts)
5658
for more information.
5759

5860
### Nested test grouping
@@ -65,13 +67,13 @@ import {
6567
beforeEach,
6668
describe,
6769
it,
68-
} from "https://deno.land/x/test_suite@0.12.0/mod.ts";
69-
import { assertEquals } from "https://deno.land/std@0.128.0/testing/asserts.ts";
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";
7072
import {
7173
getUser,
7274
resetUsers,
7375
User,
74-
} from "https://deno.land/x/test_suite@0.12.0/examples/user.ts";
76+
} from "https://deno.land/x/test_suite@0.13.0/examples/user.ts";
7577

7678
describe("user describe", () => {
7779
let user: User;
@@ -129,22 +131,22 @@ test result: ok. 1 passed (5 steps); 0 failed; 0 ignored; 0 measured; 0 filtered
129131
The example below can be found [here](examples/user_flat_test.ts).
130132

131133
```ts
132-
import { describe, it } from "https://deno.land/x/test_suite@0.12.0/mod.ts";
133-
import { assertEquals } from "https://deno.land/std@0.128.0/testing/asserts.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";
134136
import {
135137
getUser,
136138
resetUsers,
137139
User,
138-
} from "https://deno.land/x/test_suite@0.12.0/examples/user.ts";
140+
} from "https://deno.land/x/test_suite@0.13.0/examples/user.ts";
139141

140142
interface UserContext {
141143
user: User;
142144
}
143145

144146
const userSuite = describe({
145147
name: "user",
146-
beforeEach(context: UserContext) {
147-
context.user = new User("Kyle June");
148+
beforeEach(this: UserContext) {
149+
this.user = new User("Kyle June");
148150
},
149151
afterEach() {
150152
resetUsers();
@@ -165,12 +167,12 @@ it(getUserSuite, "user does not exist", () => {
165167
assertEquals(getUser("John Doe"), undefined);
166168
});
167169

168-
it(getUserSuite, "user exists", (context: UserContext) => {
169-
assertEquals(getUser("Kyle June"), context.user);
170+
it(getUserSuite, "user exists", function (this: UserContext) {
171+
assertEquals(getUser("Kyle June"), this.user);
170172
});
171173

172-
it(userSuite, "resetUsers", (context: UserContext) => {
173-
assertEquals(getUser("Kyle June"), context.user);
174+
it(userSuite, "resetUsers", function (this: UserContext) {
175+
assertEquals(getUser("Kyle June"), this.user);
174176
resetUsers();
175177
assertEquals(getUser("Kyle June"), undefined);
176178
});

describe.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ type ItArgs<T> =
1616
]
1717
| [
1818
name: string,
19-
fn: (context: T) => void | Promise<void>,
19+
fn: (this: T) => void | Promise<void>,
2020
]
21-
| [fn: (context: T) => void | Promise<void>]
21+
| [fn: (this: T) => void | Promise<void>]
2222
| [
2323
name: string,
2424
options: Omit<ItDefinition<T>, "fn" | "name">,
25-
fn: (context: T) => void | Promise<void>,
25+
fn: (this: T) => void | Promise<void>,
2626
]
2727
| [
2828
options: Omit<ItDefinition<T>, "fn">,
29-
fn: (context: T) => void | Promise<void>,
29+
fn: (this: T) => void | Promise<void>,
3030
]
3131
| [
3232
options: Omit<ItDefinition<T>, "fn" | "name">,
33-
fn: (context: T) => void | Promise<void>,
33+
fn: (this: T) => void | Promise<void>,
3434
]
3535
| [
3636
suite: TestSuite<T>,
@@ -40,27 +40,27 @@ type ItArgs<T> =
4040
| [
4141
suite: TestSuite<T>,
4242
name: string,
43-
fn: (context: T) => void | Promise<void>,
43+
fn: (this: T) => void | Promise<void>,
4444
]
4545
| [
4646
suite: TestSuite<T>,
47-
fn: (context: T) => void | Promise<void>,
47+
fn: (this: T) => void | Promise<void>,
4848
]
4949
| [
5050
suite: TestSuite<T>,
5151
name: string,
5252
options: Omit<ItDefinition<T>, "fn" | "name" | "suite">,
53-
fn: (context: T) => void | Promise<void>,
53+
fn: (this: T) => void | Promise<void>,
5454
]
5555
| [
5656
suite: TestSuite<T>,
5757
options: Omit<ItDefinition<T>, "fn" | "suite">,
58-
fn: (context: T) => void | Promise<void>,
58+
fn: (this: T) => void | Promise<void>,
5959
]
6060
| [
6161
suite: TestSuite<T>,
6262
options: Omit<ItDefinition<T>, "fn" | "name" | "suite">,
63-
fn: (context: T) => void | Promise<void>,
63+
fn: (this: T) => void | Promise<void>,
6464
];
6565

6666
/** Generates an ItDefinition from ItArgs. */
@@ -165,9 +165,9 @@ export function it<T>(...args: ItArgs<T>): void {
165165
sanitizeExit,
166166
sanitizeOps,
167167
sanitizeResources,
168-
fn: async () => {
168+
async fn() {
169169
if (!TestSuiteInternal.running) TestSuiteInternal.running = true;
170-
await fn!({} as T);
170+
await fn.call({} as T);
171171
},
172172
});
173173
}
@@ -191,7 +191,7 @@ it.ignore = function itIgnore<T>(...args: ItArgs<T>): void {
191191

192192
function addHook<T>(
193193
name: HookNames,
194-
fn: (context: T) => void | Promise<void>,
194+
fn: (this: T) => void | Promise<void>,
195195
): void {
196196
if (!TestSuiteInternal.current) {
197197
if (TestSuiteInternal.started) {
@@ -210,28 +210,28 @@ function addHook<T>(
210210

211211
/** Run some shared setup before all of the tests in the suite. */
212212
export function beforeAll<T>(
213-
fn: (context: T) => void | Promise<void>,
213+
fn: (this: T) => void | Promise<void>,
214214
): void {
215215
addHook("beforeAll", fn);
216216
}
217217

218218
/** Run some shared teardown after all of the tests in the suite. */
219219
export function afterAll<T>(
220-
fn: (context: T) => void | Promise<void>,
220+
fn: (this: T) => void | Promise<void>,
221221
): void {
222222
addHook("afterAll", fn);
223223
}
224224

225225
/** Run some shared setup before each test in the suite. */
226226
export function beforeEach<T>(
227-
fn: (context: T) => void | Promise<void>,
227+
fn: (this: T) => void | Promise<void>,
228228
): void {
229229
addHook("beforeEach", fn);
230230
}
231231

232232
/** Run some shared teardown after each test in the suite. */
233233
export function afterEach<T>(
234-
fn: (context: T) => void | Promise<void>,
234+
fn: (this: T) => void | Promise<void>,
235235
): void {
236236
addHook("afterEach", fn);
237237
}

0 commit comments

Comments
 (0)