@travetto/test 3.0.2 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -67
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
<!-- This file was generated by @travetto/doc and should not be modified directly -->
|
|
2
|
-
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/test/DOC.
|
|
2
|
+
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/test/DOC.tsx and execute "npx trv doc" to rebuild -->
|
|
3
3
|
# Testing
|
|
4
|
+
|
|
4
5
|
## Declarative test framework
|
|
5
6
|
|
|
6
7
|
**Install: @travetto/test**
|
|
@@ -13,8 +14,6 @@ yarn add @travetto/test
|
|
|
13
14
|
```
|
|
14
15
|
|
|
15
16
|
This module provides unit testing functionality that integrates with the framework. It is a declarative framework, using decorators to define tests and suites. The test produces results in the following formats:
|
|
16
|
-
|
|
17
|
-
|
|
18
17
|
* [TAP 13](https://testanything.org/tap-version-13-specification.html), default and human-readable
|
|
19
18
|
* [JSON](https://www.json.org), best for integrating with at a code level
|
|
20
19
|
* [xUnit](https://en.wikipedia.org/wiki/XUnit), standard format for CI/CD systems e.g. Jenkins, Bamboo, etc.
|
|
@@ -22,8 +21,7 @@ This module provides unit testing functionality that integrates with the framewo
|
|
|
22
21
|
**Note**: All tests should be under the `test/.*` folders. The pattern for tests is defined as a regex and not standard globbing.
|
|
23
22
|
|
|
24
23
|
## Definition
|
|
25
|
-
|
|
26
|
-
A test suite is a collection of individual tests. All test suites are classes with the [@Suite](https://github.com/travetto/travetto/tree/main/module/test/src/decorator/suite.ts#L14) decorator. Tests are defined as methods on the suite class, using the [@Test](https://github.com/travetto/travetto/tree/main/module/test/src/decorator/test.ts#L20) decorator. All tests intrinsically support `async`/`await`.
|
|
24
|
+
A test suite is a collection of individual tests. All test suites are classes with the [@Suite](https://github.com/travetto/travetto/tree/main/module/test/src/decorator/suite.ts#L14) decorator. Tests are defined as methods on the suite class, using the [@Test](https://github.com/travetto/travetto/tree/main/module/test/src/decorator/test.ts#L20) decorator. All tests intrinsically support `async`/`await`.
|
|
27
25
|
|
|
28
26
|
A simple example would be:
|
|
29
27
|
|
|
@@ -112,8 +110,6 @@ AssertionError(
|
|
|
112
110
|
```
|
|
113
111
|
|
|
114
112
|
The equivalences for all of the [assert](https://nodejs.org/api/assert.html) operations are:
|
|
115
|
-
|
|
116
|
-
|
|
117
113
|
* `assert(a == b)` as `assert.equal(a, b)`
|
|
118
114
|
* `assert(a !== b)` as `assert.notEqual(a, b)`
|
|
119
115
|
* `assert(a === b)` as `assert.strictEqual(a, b)`
|
|
@@ -125,65 +121,62 @@ The equivalences for all of the [assert](https://nodejs.org/api/assert.html) ope
|
|
|
125
121
|
* `assert(a instanceof b)` as `assert.instanceOf(a, b)`
|
|
126
122
|
* `assert(a.includes(b))` as `assert.ok(a.includes(b))`
|
|
127
123
|
* `assert(/a/.test(b))` as `assert.ok(/a/.test(b))`
|
|
128
|
-
|
|
129
124
|
In addition to the standard operations, there is support for throwing/rejecting errors (or the inverse). This is useful for testing error states or ensuring errors do not occur.
|
|
130
125
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
Additionally, the `throws`/`rejects` assertions take in a secondary parameter to allow for specification of the type of error expected. This can be:
|
|
186
|
-
|
|
126
|
+
### Throws
|
|
127
|
+
`throws`/`doesNotThrow` is for catching synchronous rejections
|
|
128
|
+
|
|
129
|
+
**Code: Throws vs Does Not Throw**
|
|
130
|
+
```typescript
|
|
131
|
+
import assert from 'assert';
|
|
132
|
+
|
|
133
|
+
import { Suite, Test } from '@travetto/test';
|
|
134
|
+
|
|
135
|
+
@Suite()
|
|
136
|
+
class SimpleTest {
|
|
137
|
+
|
|
138
|
+
@Test()
|
|
139
|
+
async testThrows() {
|
|
140
|
+
assert.throws(() => {
|
|
141
|
+
throw new Error();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
assert.doesNotThrow(() => {
|
|
145
|
+
|
|
146
|
+
let a = 5;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Rejects
|
|
153
|
+
`rejects`/`doesNotReject` is for catching asynchronous rejections
|
|
154
|
+
|
|
155
|
+
**Code: Rejects vs Does Not Reject**
|
|
156
|
+
```typescript
|
|
157
|
+
import assert from 'assert';
|
|
158
|
+
|
|
159
|
+
import { Suite, Test } from '@travetto/test';
|
|
160
|
+
|
|
161
|
+
@Suite()
|
|
162
|
+
class SimpleTest {
|
|
163
|
+
|
|
164
|
+
@Test()
|
|
165
|
+
async testRejects() {
|
|
166
|
+
await assert.rejects(async () => {
|
|
167
|
+
throw new Error();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
await assert.doesNotReject(async () => {
|
|
171
|
+
|
|
172
|
+
let a = 5;
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Error Matching
|
|
179
|
+
Additionally, the `throws`/`rejects` assertions take in a secondary parameter to allow for specification of the type of error expected. This can be:
|
|
187
180
|
* A regular expression or string to match against the error's message
|
|
188
181
|
* A class to ensure the returned error is an instance of the class passed in
|
|
189
182
|
* A function to allow for whatever custom verification of the error is needed
|
|
@@ -221,7 +214,6 @@ class SimpleTest {
|
|
|
221
214
|
```
|
|
222
215
|
|
|
223
216
|
## Running Tests
|
|
224
|
-
|
|
225
217
|
To run the tests you can either call the [Command Line Interface](https://github.com/travetto/travetto/tree/main/module/cli#readme "CLI infrastructure for Travetto framework") by invoking
|
|
226
218
|
|
|
227
219
|
**Terminal: Test Help Output**
|
|
@@ -240,10 +232,9 @@ Options:
|
|
|
240
232
|
The regexes are the patterns of tests you want to run, and all tests must be found under the `test/` folder.
|
|
241
233
|
|
|
242
234
|
### Travetto Plugin
|
|
243
|
-
|
|
244
235
|
The [VSCode plugin](https://marketplace.visualstudio.com/items?itemName=arcsine.travetto-plugin) also supports test running, which will provide even more functionality for real-time testing and debugging.
|
|
245
236
|
|
|
246
237
|
## Additional Considerations
|
|
247
|
-
During the test execution, a few things additionally happen that should be helpful. The primary addition, is that all console output is captured, and will be exposed in the test output. This allows for investigation at a later point in time by analyzing the output.
|
|
238
|
+
During the test execution, a few things additionally happen that should be helpful. The primary addition, is that all console output is captured, and will be exposed in the test output. This allows for investigation at a later point in time by analyzing the output.
|
|
248
239
|
|
|
249
240
|
Like output, all promises are also intercepted. This allows the code to ensure that all promises have been resolved before completing the test. Any uncompleted promises will automatically trigger an error state and fail the test.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/test",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.3",
|
|
4
4
|
"description": "Declarative test framework",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"unit-testing",
|
|
@@ -27,15 +27,15 @@
|
|
|
27
27
|
"directory": "module/test"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@travetto/base": "^3.0.
|
|
31
|
-
"@travetto/registry": "^3.0.
|
|
32
|
-
"@travetto/terminal": "^3.0.
|
|
33
|
-
"@travetto/worker": "^3.0.
|
|
34
|
-
"@travetto/yaml": "^3.0.
|
|
30
|
+
"@travetto/base": "^3.0.3",
|
|
31
|
+
"@travetto/registry": "^3.0.3",
|
|
32
|
+
"@travetto/terminal": "^3.0.3",
|
|
33
|
+
"@travetto/worker": "^3.0.3",
|
|
34
|
+
"@travetto/yaml": "^3.0.3"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@travetto/cli": "^3.0.
|
|
38
|
-
"@travetto/transformer": "^3.0.
|
|
37
|
+
"@travetto/cli": "^3.0.3",
|
|
38
|
+
"@travetto/transformer": "^3.0.3"
|
|
39
39
|
},
|
|
40
40
|
"peerDependenciesMeta": {
|
|
41
41
|
"@travetto/transformer": {
|