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

Commit 0427927

Browse files
authored
Merge pull request #15 from udibo/dev
Rewrite describe and it to use test steps
2 parents e334b6e + 7f8a125 commit 0427927

File tree

12 files changed

+2274
-4021
lines changed

12 files changed

+2274
-4021
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ jobs:
2424
if: matrix.os == 'ubuntu-latest'
2525
run: deno lint
2626
- name: Run tests
27-
run: deno test --allow-read --coverage=cov
27+
run: deno test --coverage=cov
2828
- name: Run tests unstable
29-
run: deno test --allow-read --unstable
29+
run: deno test --unstable
3030
- name: Generate lcov
3131
if: |
3232
matrix.os == 'ubuntu-latest' &&

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"request": "launch",
88
"cwd": "${workspaceFolder}",
99
"runtimeExecutable": "deno",
10-
"runtimeArgs": ["run", "--inspect", "${file}"],
10+
"runtimeArgs": ["run", "--inspect-brk", "${file}"],
1111
"attachSimplePort": 9229
1212
},
1313
{
@@ -16,7 +16,7 @@
1616
"request": "launch",
1717
"cwd": "${workspaceFolder}",
1818
"runtimeExecutable": "deno",
19-
"runtimeArgs": ["test", "--inspect", "${file}"],
19+
"runtimeArgs": ["test", "--inspect-brk", "${file}"],
2020
"attachSimplePort": 9229
2121
}
2222
]

README.md

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

3-
[![version](https://img.shields.io/badge/release-0.9.5-success)](https://deno.land/x/test_suite@0.9.5)
4-
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/test_suite@0.9.5/mod.ts)
3+
[![version](https://img.shields.io/badge/release-0.10.0-success)](https://deno.land/x/test_suite@0.10.0)
4+
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/test_suite@0.10.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/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)
88

9-
An extension of Deno's built-in test runner to add setup/teardown hooks and the
10-
ability to organize tests. Includes describe/it functions for creating nested
11-
tests in a format similar to Jasmine, Jest, and Mocha.
9+
An extension of Deno's built-in test runner to add setup/teardown hooks and make
10+
it easier to organize tests in a format similar to Jasmine, Jest, and Mocha.
1211

1312
## Features
1413

1514
- Ability to group tests together into test suites
16-
- Setup/teardown hooks for test suites(beforeAll, afterAll, beforeEach,
15+
- Setup/teardown hooks for test suites (beforeAll, afterAll, beforeEach,
1716
afterEach)
1817
- Tests within a test suite inherit configuration options
1918
- describe/it functions similar to Jasmine, Jest, and Mocha
19+
- shorthand for focusing and ignoring tests
2020

2121
## Installation
2222

@@ -26,20 +26,13 @@ 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.5/mod.ts";
29+
import { describe, it } from "https://deno.land/x/test_suite@0.10.0/mod.ts";
3030
// Import from GitHub
31-
import { TestSuite, test } "https://raw.githubusercontent.com/udibo/test_suite/0.9.5/mod.ts";
31+
import { describe, it } "https://raw.githubusercontent.com/udibo/test_suite/0.10.0/mod.ts";
3232
```
3333
3434
## Usage
3535
36-
Below are some examples of how to use TestSuite and test in tests.
37-
38-
See [deno docs](https://doc.deno.land/https/deno.land/x/test_suite@0.9.5/mod.ts)
39-
for more information.
40-
41-
### TestSuite
42-
4336
When you have a set of tests that are related, you can group them together by
4437
creating a test suite. A test suite can contain other test suites and tests. All
4538
tests within a suite will inherit their options from the suite unless they
@@ -53,99 +46,29 @@ values available to the tests that are defined in the beforeAll function.
5346
The beforeEach and afterEach hook options are similar to beforeAll and afterAll
5447
except they are called before and after each individual test.
5548
56-
The example test below can be found in the example directory.
57-
58-
```ts
59-
import { test, TestSuite } from "https://deno.land/x/test_suite@0.9.5/mod.ts";
60-
import { assertEquals } from "https://deno.land/std@0.120.0/testing/asserts.ts";
61-
import {
62-
getUser,
63-
resetUsers,
64-
User,
65-
} from "https://deno.land/x/test_suite@0.9.5/example/user.ts";
66-
67-
interface UserSuiteContext {
68-
user: User;
69-
}
70-
const userSuite: TestSuite<UserSuiteContext> = new TestSuite({
71-
name: "user",
72-
beforeEach(context: UserSuiteContext) {
73-
context.user = new User("Kyle June");
74-
},
75-
afterEach() {
76-
resetUsers();
77-
},
78-
});
79-
80-
test(userSuite, "create", () => {
81-
const user = new User("John Doe");
82-
assertEquals(user.name, "John Doe");
83-
});
84-
85-
const getUserSuite: TestSuite<UserSuiteContext> = new TestSuite({
86-
name: "getUser",
87-
suite: userSuite,
88-
});
49+
Below are some examples of how to use `describe` and `it` in tests.
8950
90-
test(getUserSuite, "user does not exist", () => {
91-
assertEquals(getUser("John Doe"), undefined);
92-
});
93-
94-
test(getUserSuite, "user exists", (context: UserSuiteContext) => {
95-
assertEquals(getUser("Kyle June"), context.user);
96-
});
97-
98-
test(userSuite, "resetUsers", (context: UserSuiteContext) => {
99-
assertEquals(getUser("Kyle June"), context.user);
100-
resetUsers();
101-
assertEquals(getUser("Kyle June"), undefined);
102-
});
103-
```
104-
105-
If you run the above tests using `deno test`, you will get the following output.
106-
Each test name is prefixed with the suite name.
107-
108-
```sh
109-
$ deno test
110-
running 4 tests
111-
test user create ... ok (4ms)
112-
test user getUser user does not exist ... ok (1ms)
113-
test user getUser user exists ... ok (2ms)
114-
test user resetUsers ... ok (2ms)
115-
116-
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (11ms)
117-
```
118-
119-
### describe/it
120-
121-
When you have a set of tests that are related, you can group them together by
122-
creating a test suite with describe. A test suite can contain other test suites
123-
and tests. All tests within a suite will inherit their options from the suite
124-
unless they specifically set them.
125-
126-
The beforeAll and afterAll hook options can be used to do something before and
127-
after all the tests in the suite run. If you would like to set values for all
128-
tests within the suite, you can create a context interface that defines all the
129-
values available to the tests that are defined in the beforeAll function.
51+
See
52+
[deno docs](https://doc.deno.land/https/deno.land/x/test_suite@0.10.0/mod.ts)
53+
for more information.
13054
131-
The beforeEach and afterEach hook options are similar to beforeAll and afterAll
132-
except they are called before and after each individual test.
55+
### Nested test grouping
13356
134-
The example test below can be found in the example directory.
57+
The example below can be found [here](examples/user_nested_test.ts).
13558
13659
```ts
13760
import {
13861
afterEach,
13962
beforeEach,
14063
describe,
14164
it,
142-
} from "https://deno.land/x/test_suite@0.9.5/mod.ts";
143-
import { assertEquals } from "https://deno.land/std@0.120.0/testing/asserts.ts";
65+
} from "https://deno.land/x/test_suite@0.10.0/mod.ts";
66+
import { assertEquals } from "https://deno.land/std@0.126.0/testing/asserts.ts";
14467
import {
14568
getUser,
14669
resetUsers,
14770
User,
148-
} from "https://deno.land/x/test_suite@0.9.5/examples/user.ts";
71+
} from "https://deno.land/x/test_suite@0.10.0/examples/user.ts";
14972

15073
describe("user describe", () => {
15174
let user: User;
@@ -182,19 +105,119 @@ describe("user describe", () => {
182105
```
183106

184107
If you run the above tests using `deno test`, you will get the following output.
185-
Each test name is prefixed with the suite name.
186108

187109
```sh
188110
$ deno test
189-
running 4 tests
190-
test user describe create ... ok (4ms)
191-
test user describe getUser user does not exist ... ok (1ms)
192-
test user describe getUser user exists ... ok (2ms)
193-
test user describe resetUsers ... ok (2ms)
111+
running 1 test from file:///examples/user_nested_test.ts
112+
test user describe ...
113+
test create ... ok (4ms)
114+
test getUser ...
115+
test user does not exist ... ok (4ms)
116+
test user exists ... ok (3ms)
117+
ok (11ms)
118+
test resetUsers ... ok (3ms)
119+
ok (24ms)
120+
121+
test result: ok. 1 passed (5 steps); 0 failed; 0 ignored; 0 measured; 0 filtered out (43ms)
122+
```
123+
124+
### Flat test grouping
125+
126+
The example below can be found [here](examples/user_flat_test.ts).
127+
128+
```ts
129+
import { test, TestSuite } from "https://deno.land/x/test_suite@0.10.0/mod.ts";
130+
import { assertEquals } from "https://deno.land/std@0.126.0/testing/asserts.ts";
131+
import {
132+
getUser,
133+
resetUsers,
134+
User,
135+
} from "https://deno.land/x/test_suite@0.10.0/example/user.ts";
136+
137+
interface UserContext {
138+
user: User;
139+
}
140+
141+
const userSuite = describe({
142+
name: "user",
143+
beforeEach(context: UserContext) {
144+
context.user = new User("Kyle June");
145+
},
146+
afterEach() {
147+
resetUsers();
148+
},
149+
});
150+
151+
it(userSuite, "create", () => {
152+
const user = new User("John Doe");
153+
assertEquals(user.name, "John Doe");
154+
});
155+
156+
interface GetUserContext extends UserContext {
157+
value?: number;
158+
}
194159

195-
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (11ms)
160+
const getUserSuite = describe<GetUserContext>({
161+
name: "getUser",
162+
suite: userSuite,
163+
});
164+
165+
it(getUserSuite, "user does not exist", () => {
166+
assertEquals(getUser("John Doe"), undefined);
167+
});
168+
169+
it(getUserSuite, "user exists", (context: UserContext) => {
170+
assertEquals(getUser("Kyle June"), context.user);
171+
});
172+
173+
it(userSuite, "resetUsers", (context: UserContext) => {
174+
assertEquals(getUser("Kyle June"), context.user);
175+
resetUsers();
176+
assertEquals(getUser("Kyle June"), undefined);
177+
});
196178
```
197179

180+
If you run the above tests using `deno test`, you will get the following output.
181+
182+
```sh
183+
$ deno test
184+
running 1 test from file:///examples/user_flat_test.ts
185+
test user ...
186+
test create ... ok (3ms)
187+
test getUser ...
188+
test user does not exist ... ok (2ms)
189+
test user exists ... ok (4ms)
190+
ok (11ms)
191+
test resetUsers ... ok (3ms)
192+
ok (22ms)
193+
194+
test result: ok. 1 passed (5 steps); 0 failed; 0 ignored; 0 measured; 0 filtered out (44ms)
195+
```
196+
197+
### Shorthand for focusing and ignoring tests
198+
199+
To avoid having to change your test function arguments to be able to focus or
200+
ignore tests temporarily, a shorthand has been implemented. This makes it as
201+
easy as typing `.only` and `.ignore` to focus and ignore tests.
202+
203+
- To focus a test case, replace `it` with `it.only`.
204+
- To ignore a test case, replace `it` with `it.ignore`.
205+
- To focus a test suite, replace `describe` with `describe.only`.
206+
- To ignore a test suite, replace `describe` with `describe.ignore`.
207+
208+
### Migrating from earlier versions
209+
210+
The `TestSuite` class has been turned into an internal only class. To create a
211+
test suite, use the `describe` function instead of creating a new instance of
212+
`TestSuite` directly.
213+
214+
The `test` function has been removed. Replace `test` calls with calls to `it`.
215+
216+
The `each` function that was previously available has been temporarily removed
217+
to allow me to switch over to the test step api quicker. I plan on implementing
218+
this later. If you make use of the `each` function for generating test cases, do
219+
not upgrade beyond version 0.9.5.
220+
198221
## License
199222

200223
[MIT](LICENSE)

deps.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
export { delay } from "https://deno.land/std@0.120.0/async/delay.ts";
1+
export { delay } from "https://deno.land/std@0.126.0/async/delay.ts";
22
export {
33
assert,
44
assertEquals,
55
AssertionError,
66
assertObjectMatch,
77
assertRejects,
8+
assertStrictEquals,
89
assertThrows,
9-
} from "https://deno.land/std@0.120.0/testing/asserts.ts";
10+
} from "https://deno.land/std@0.126.0/testing/asserts.ts";
1011

1112
export { Vector } from "https://deno.land/x/collections@0.11.2/vector.ts";

0 commit comments

Comments
 (0)