@kensio/smartass 0.0.1 → 0.0.2
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 +77 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,78 @@
|
|
|
1
|
-
# smartass
|
|
1
|
+
# @kensio/smartass
|
|
2
2
|
|
|
3
|
-
TypeScript-first test assertion functions with precise type narrowing
|
|
3
|
+
TypeScript-first test assertion functions with precise type narrowing via
|
|
4
|
+
assertion signatures.
|
|
5
|
+
|
|
6
|
+
See
|
|
7
|
+
[TypeScript assertion functions / assertion signatures](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions)
|
|
8
|
+
|
|
9
|
+
The fluent `expect().toBe()` interface in Jest and Vitest is readable, but is
|
|
10
|
+
unable to provide type information to TypeScript and IntelliSense.
|
|
11
|
+
|
|
12
|
+
This library provides assertion functions with assertion signatures so that you
|
|
13
|
+
can benefit from the extra type information.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @kensio/smartass
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### assertNonNullable
|
|
24
|
+
|
|
25
|
+
Assert that a value is neither null nor undefined, with type narrowing.
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { assertNonNullable } from '@kensio/smartass';
|
|
29
|
+
|
|
30
|
+
test('user has a name', () => {
|
|
31
|
+
const user = getUser();
|
|
32
|
+
assertNonNullable(user);
|
|
33
|
+
// TypeScript now knows user is neither null nor undefined, so no need
|
|
34
|
+
// for ! or ? operators.
|
|
35
|
+
expect(user.name).toBe('Alice');
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### assertNotEmpty
|
|
40
|
+
|
|
41
|
+
Assert that an array is non-empty, with type narrowing to a tuple type.
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { assertNotEmpty } from '@kensio/smartass';
|
|
45
|
+
|
|
46
|
+
test('first item is correct', () => {
|
|
47
|
+
const items: string[] = getItems();
|
|
48
|
+
assertNotEmpty(items);
|
|
49
|
+
// TypeScript now knows items is an array of strings with at
|
|
50
|
+
// least one element, so no need for ? operator.
|
|
51
|
+
expect(items[0]).toBe('first');
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### assertOneOf
|
|
56
|
+
|
|
57
|
+
Assert that a value is one of a set of allowed values, with type narrowing.
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { assertOneOf } from '@kensio/smartass';
|
|
61
|
+
|
|
62
|
+
test('status is valid', () => {
|
|
63
|
+
const status = getStatus();
|
|
64
|
+
assertOneOf(status, ['pending', 'active', 'completed'] as const);
|
|
65
|
+
// TypeScript now knows status is 'pending' | 'active' | 'completed'
|
|
66
|
+
expect(status).toBe('active');
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Custom Error Messages
|
|
71
|
+
|
|
72
|
+
All assertion functions accept an optional custom error message:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
assertNonNullable(user, 'User must be provided');
|
|
76
|
+
assertNotEmpty(items, 'Items array cannot be empty');
|
|
77
|
+
assertOneOf(status, ['open', 'closed'], 'Status must be either open or closed');
|
|
78
|
+
```
|
package/package.json
CHANGED