Skip to content

Commit cb3a786

Browse files
committed
doc: update documentation for TestKernel
1 parent bb90595 commit cb3a786

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/backend/tools/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,66 @@ a test iterator through all the services.
88

99
The Test Kernel is ideal for running unit and integration tests against individual services, ensuring they behave correctly.
1010

11+
### Usage
12+
13+
```
14+
node src/backend/tools/test`
15+
```
16+
17+
### Testing Services
18+
19+
Implement the method `_test` on any service. When `_test` is called the "construct"
20+
phase has already completed (meaning `_construct` on your service has been called
21+
by now if you've implemented it), but the "init" phase will never happen (so _init
22+
is never called).
23+
24+
> **TODO:** I want to add support for mocking `_init` for deeper testing.
25+
26+
For example, it should look similar to this snippet:
27+
28+
```javascript
29+
class ExampleService extends BaseService {
30+
// ...
31+
async _test ({ assert }) {
32+
assert.equal('actual', 'expected', 'rule should have a description');
33+
}
34+
}
35+
```
36+
37+
Notice the parameter `assert` - this holds the TestKernel's testing API. The
38+
reason this is a named parameter is to leave room for future support of
39+
multiple testing APIs if this is ever desired or we decide to migrate
40+
incrementally.
41+
42+
The last parameter to `assert.equal` is a message describing the test rule.
43+
The message should always be a short statement with minimal punctuation.
44+
45+
#### TestKernel's testing API
46+
47+
The `assert` value here is a function that also has other methods defined
48+
in its properties. When called directly, `assert` will run the callback you
49+
provide as an assertion. When no `name` parameter is specified, the callback
50+
itself will be printed as the name of the assertion - this is useful for
51+
very short expressions which are self-descriptive.
52+
53+
```javascript
54+
class ExampleService extends BaseService {
55+
// ...
56+
async _test ({ assert }) {
57+
assert(() => 1 === 2, 'one should equal two');
58+
assert.equal(1, 2, 'one should equal two')
59+
assert(() => 3 === 4); // prints out as: `() => 3 === 4`
60+
}
61+
}
62+
```
63+
64+
| Method | Parameters | Types | Description |
65+
|----------------|---------------------------------|---------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|
66+
| `assert` | `callback`, `name?` | `callback: () => boolean`, `name?: string` | Runs the callback as an assertion. If `name` is omitted, the callback's source text is used as the printed name of the assertion. |
67+
| `assert.equal` | `actual`, `expected`, `message` | `actual: any`, `expected: any`, `message: string` | Asserts that `actual === expected`. The final parameter is a short descriptive message for the test rule. |
68+
69+
70+
1171
### Test Kernel Notes
1272

1373
1. **Logging**:

0 commit comments

Comments
 (0)