@clipboard-health/testing-core 0.1.1 → 0.2.0
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 +82 -1
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
# @clipboard-health/testing-core <!-- omit from toc -->
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TypeScript-friendly testing utilities.
|
|
4
4
|
|
|
5
5
|
## Table of contents <!-- omit from toc -->
|
|
6
6
|
|
|
7
7
|
- [Install](#install)
|
|
8
8
|
- [Usage](#usage)
|
|
9
|
+
- [Type narrowing `expect` helpers](#type-narrowing-expect-helpers)
|
|
9
10
|
- [Local development commands](#local-development-commands)
|
|
10
11
|
|
|
11
12
|
## Install
|
|
@@ -16,6 +17,86 @@ npm install @clipboard-health/testing-core
|
|
|
16
17
|
|
|
17
18
|
## Usage
|
|
18
19
|
|
|
20
|
+
### Type narrowing `expect` helpers
|
|
21
|
+
|
|
22
|
+
Jest's [`expect(...).toBeDefined()`](https://jestjs.io/docs/expect#tobedefined) does not narrow types.
|
|
23
|
+
|
|
24
|
+
This gives a type error:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
const value = getValue(); // returns 'string | undefined'
|
|
28
|
+
|
|
29
|
+
expect(value).toBeDefined();
|
|
30
|
+
|
|
31
|
+
const { length } = value;
|
|
32
|
+
// ^? Property 'length' does not exist on type 'string | undefined'.
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
This library's helpers narrow types:
|
|
36
|
+
|
|
37
|
+
<!-- prettier-ignore -->
|
|
38
|
+
```ts
|
|
39
|
+
// ./examples/expectToBeDefined.ts
|
|
40
|
+
|
|
41
|
+
import { ok } from "node:assert/strict";
|
|
42
|
+
|
|
43
|
+
import { expectToBeDefined } from "@clipboard-health/testing-core";
|
|
44
|
+
|
|
45
|
+
function getValue(): string | undefined {
|
|
46
|
+
return "hi";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const value = getValue();
|
|
50
|
+
expectToBeDefined(value);
|
|
51
|
+
|
|
52
|
+
// Narrowed to `string`
|
|
53
|
+
const { length } = value;
|
|
54
|
+
ok(length === 2);
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
<!-- prettier-ignore -->
|
|
59
|
+
```ts
|
|
60
|
+
// ./examples/expectToBeSafeParseError.ts
|
|
61
|
+
|
|
62
|
+
import { ok } from "node:assert/strict";
|
|
63
|
+
|
|
64
|
+
import { expectToBeDefined, expectToBeSafeParseError } from "@clipboard-health/testing-core";
|
|
65
|
+
import { z } from "zod";
|
|
66
|
+
|
|
67
|
+
const schema = z.object({ name: z.string() });
|
|
68
|
+
|
|
69
|
+
const value = schema.safeParse({ name: 1 });
|
|
70
|
+
expectToBeSafeParseError(value);
|
|
71
|
+
|
|
72
|
+
// Narrowed to `SafeParseError`
|
|
73
|
+
const firstIssue = value.error.issues[0];
|
|
74
|
+
expectToBeDefined(firstIssue);
|
|
75
|
+
|
|
76
|
+
// Narrowed to `ZodIssue`
|
|
77
|
+
ok(firstIssue.message === "Expected string, received number");
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
<!-- prettier-ignore -->
|
|
82
|
+
```ts
|
|
83
|
+
// ./examples/expectToBeSafeParseSuccess.ts
|
|
84
|
+
|
|
85
|
+
import { ok } from "node:assert/strict";
|
|
86
|
+
|
|
87
|
+
import { expectToBeSafeParseSuccess } from "@clipboard-health/testing-core";
|
|
88
|
+
import { z } from "zod";
|
|
89
|
+
|
|
90
|
+
const schema = z.object({ name: z.string() });
|
|
91
|
+
|
|
92
|
+
const value = schema.safeParse({ name: "hi" });
|
|
93
|
+
expectToBeSafeParseSuccess(value);
|
|
94
|
+
|
|
95
|
+
// Narrowed to `SafeParseSuccess`
|
|
96
|
+
ok(value.data.name === "hi");
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
|
|
19
100
|
## Local development commands
|
|
20
101
|
|
|
21
102
|
See [`package.json`](./package.json) `scripts` for a list of commands.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clipboard-health/testing-core",
|
|
3
|
-
"description": "
|
|
4
|
-
"version": "0.
|
|
3
|
+
"description": "TypeScript-friendly testing utilities.",
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"tslib": "2.8.0",
|
|
7
7
|
"zod": "3.23.8"
|
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
"publishConfig": {
|
|
13
13
|
"access": "public"
|
|
14
14
|
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"embed": "embedme README.md"
|
|
17
|
+
},
|
|
15
18
|
"type": "commonjs",
|
|
16
19
|
"typings": "./src/index.d.ts",
|
|
17
20
|
"types": "./src/index.d.ts"
|