@clipboard-health/testing-core 0.1.1 → 0.3.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 CHANGED
@@ -1,11 +1,12 @@
1
1
  # @clipboard-health/testing-core <!-- omit from toc -->
2
2
 
3
- Core utilities for testing Clipboard Health's software.
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,136 @@ 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/expectToBeLeft.ts
61
+
62
+ import { ok } from "node:assert/strict";
63
+
64
+ import { expectToBeLeft } from "@clipboard-health/testing-core";
65
+ import { type Either, left, right } from "@clipboard-health/util-typescript";
66
+
67
+ function divide(numerator: number, denominator: number): Either<string, number> {
68
+ if (denominator === 0) {
69
+ return left("Cannot divide by zero");
70
+ }
71
+
72
+ return right(numerator / denominator);
73
+ }
74
+
75
+ const value = divide(10, 0);
76
+ expectToBeLeft(value);
77
+
78
+ // Narrowed to Left
79
+ ok(value.left === "Cannot divide by zero");
80
+
81
+ ```
82
+
83
+ <!-- prettier-ignore -->
84
+ ```ts
85
+ // ./examples/expectToBeRight.ts
86
+
87
+ import { ok } from "node:assert/strict";
88
+
89
+ import { expectToBeRight } from "@clipboard-health/testing-core";
90
+ import { type Either, left, right } from "@clipboard-health/util-typescript";
91
+
92
+ function divide(numerator: number, denominator: number): Either<string, number> {
93
+ if (denominator === 0) {
94
+ return left("Cannot divide by zero");
95
+ }
96
+
97
+ return right(numerator / denominator);
98
+ }
99
+
100
+ const value = divide(10, 2);
101
+ expectToBeRight(value);
102
+
103
+ // Narrowed to Right
104
+ ok(value.right === 5);
105
+
106
+ ```
107
+
108
+ <!-- prettier-ignore -->
109
+ ```ts
110
+ // ./examples/expectToBeSafeParseError.ts
111
+
112
+ import { ok } from "node:assert/strict";
113
+
114
+ import { expectToBeDefined, expectToBeSafeParseError } from "@clipboard-health/testing-core";
115
+ import { z } from "zod";
116
+
117
+ const schema = z.object({ name: z.string() });
118
+
119
+ const value = schema.safeParse({ name: 1 });
120
+ expectToBeSafeParseError(value);
121
+
122
+ // Narrowed to `SafeParseError`
123
+ const firstIssue = value.error.issues[0];
124
+ expectToBeDefined(firstIssue);
125
+
126
+ // Narrowed to `ZodIssue`
127
+ ok(firstIssue.message === "Expected string, received number");
128
+
129
+ ```
130
+
131
+ <!-- prettier-ignore -->
132
+ ```ts
133
+ // ./examples/expectToBeSafeParseSuccess.ts
134
+
135
+ import { ok } from "node:assert/strict";
136
+
137
+ import { expectToBeSafeParseSuccess } from "@clipboard-health/testing-core";
138
+ import { z } from "zod";
139
+
140
+ const schema = z.object({ name: z.string() });
141
+
142
+ const value = schema.safeParse({ name: "hi" });
143
+ expectToBeSafeParseSuccess(value);
144
+
145
+ // Narrowed to `SafeParseSuccess`
146
+ ok(value.data.name === "hi");
147
+
148
+ ```
149
+
19
150
  ## Local development commands
20
151
 
21
152
  See [`package.json`](./package.json) `scripts` for a list of commands.
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@clipboard-health/testing-core",
3
- "description": "Core utilities for testing Clipboard Health's software.",
4
- "version": "0.1.1",
3
+ "description": "TypeScript-friendly testing utilities.",
4
+ "version": "0.3.0",
5
5
  "dependencies": {
6
+ "@clipboard-health/util-typescript": "0.1.0",
6
7
  "tslib": "2.8.0",
7
8
  "zod": "3.23.8"
8
9
  },
@@ -12,6 +13,9 @@
12
13
  "publishConfig": {
13
14
  "access": "public"
14
15
  },
16
+ "scripts": {
17
+ "embed": "embedme README.md"
18
+ },
15
19
  "type": "commonjs",
16
20
  "typings": "./src/index.d.ts",
17
21
  "types": "./src/index.d.ts"
package/src/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  export * from "./lib/expectToBeDefined";
2
+ export * from "./lib/expectToBeLeft";
3
+ export * from "./lib/expectToBeRight";
2
4
  export * from "./lib/expectToBeSafeParseError";
3
5
  export * from "./lib/expectToBeSafeParseSuccess";
package/src/index.js CHANGED
@@ -2,6 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./lib/expectToBeDefined"), exports);
5
+ tslib_1.__exportStar(require("./lib/expectToBeLeft"), exports);
6
+ tslib_1.__exportStar(require("./lib/expectToBeRight"), exports);
5
7
  tslib_1.__exportStar(require("./lib/expectToBeSafeParseError"), exports);
6
8
  tslib_1.__exportStar(require("./lib/expectToBeSafeParseSuccess"), exports);
7
9
  //# sourceMappingURL=index.js.map
package/src/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/testing-core/src/index.ts"],"names":[],"mappings":";;;AAAA,kEAAwC;AACxC,yEAA+C;AAC/C,2EAAiD"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/testing-core/src/index.ts"],"names":[],"mappings":";;;AAAA,kEAAwC;AACxC,+DAAqC;AACrC,gEAAsC;AACtC,yEAA+C;AAC/C,2EAAiD"}
@@ -0,0 +1,2 @@
1
+ import { type Either, type Left } from "@clipboard-health/util-typescript";
2
+ export declare function expectToBeLeft<E, A>(value: Either<E, A> | undefined): asserts value is Left<E>;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.expectToBeLeft = expectToBeLeft;
4
+ const node_assert_1 = require("node:assert");
5
+ const util_typescript_1 = require("@clipboard-health/util-typescript");
6
+ const expectToBeDefined_1 = require("./expectToBeDefined");
7
+ function expectToBeLeft(value) {
8
+ (0, expectToBeDefined_1.expectToBeDefined)(value);
9
+ (0, node_assert_1.ok)((0, util_typescript_1.isLeft)(value));
10
+ }
11
+ //# sourceMappingURL=expectToBeLeft.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"expectToBeLeft.js","sourceRoot":"","sources":["../../../../../packages/testing-core/src/lib/expectToBeLeft.ts"],"names":[],"mappings":";;AAMA,wCAGC;AATD,6CAAiC;AAEjC,uEAAmF;AAEnF,2DAAwD;AAExD,SAAgB,cAAc,CAAO,KAA+B;IAClE,IAAA,qCAAiB,EAAC,KAAK,CAAC,CAAC;IACzB,IAAA,gBAAE,EAAC,IAAA,wBAAM,EAAC,KAAK,CAAC,CAAC,CAAC;AACpB,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { type None, type Option } from "@clipboard-health/util-typescript";
2
+ export declare function expectToBeNone<A>(value: Option<A> | undefined): asserts value is None;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.expectToBeNone = expectToBeNone;
4
+ const node_assert_1 = require("node:assert");
5
+ const util_typescript_1 = require("@clipboard-health/util-typescript");
6
+ const expectToBeDefined_1 = require("./expectToBeDefined");
7
+ function expectToBeNone(value) {
8
+ (0, expectToBeDefined_1.expectToBeDefined)(value);
9
+ (0, node_assert_1.ok)((0, util_typescript_1.isNone)(value));
10
+ }
11
+ //# sourceMappingURL=expectToBeNone.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"expectToBeNone.js","sourceRoot":"","sources":["../../../../../packages/testing-core/src/lib/expectToBeNone.ts"],"names":[],"mappings":";;AAMA,wCAGC;AATD,6CAAiC;AAEjC,uEAAmF;AAEnF,2DAAwD;AAExD,SAAgB,cAAc,CAAI,KAA4B;IAC5D,IAAA,qCAAiB,EAAC,KAAK,CAAC,CAAC;IACzB,IAAA,gBAAE,EAAC,IAAA,wBAAM,EAAC,KAAK,CAAC,CAAC,CAAC;AACpB,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { type Either, type Right } from "@clipboard-health/util-typescript";
2
+ export declare function expectToBeRight<A, E>(value: Either<E, A> | undefined): asserts value is Right<A>;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.expectToBeRight = expectToBeRight;
4
+ const node_assert_1 = require("node:assert");
5
+ const util_typescript_1 = require("@clipboard-health/util-typescript");
6
+ const expectToBeDefined_1 = require("./expectToBeDefined");
7
+ function expectToBeRight(value) {
8
+ (0, expectToBeDefined_1.expectToBeDefined)(value);
9
+ (0, node_assert_1.ok)((0, util_typescript_1.isRight)(value));
10
+ }
11
+ //# sourceMappingURL=expectToBeRight.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"expectToBeRight.js","sourceRoot":"","sources":["../../../../../packages/testing-core/src/lib/expectToBeRight.ts"],"names":[],"mappings":";;AAMA,0CAGC;AATD,6CAAiC;AAEjC,uEAAqF;AAErF,2DAAwD;AAExD,SAAgB,eAAe,CAAO,KAA+B;IACnE,IAAA,qCAAiB,EAAC,KAAK,CAAC,CAAC;IACzB,IAAA,gBAAE,EAAC,IAAA,yBAAO,EAAC,KAAK,CAAC,CAAC,CAAC;AACrB,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { type Option, type Some } from "@clipboard-health/util-typescript";
2
+ export declare function expectToBeSome<A>(value: Option<A> | undefined): asserts value is Some<A>;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.expectToBeSome = expectToBeSome;
4
+ const node_assert_1 = require("node:assert");
5
+ const util_typescript_1 = require("@clipboard-health/util-typescript");
6
+ const expectToBeDefined_1 = require("./expectToBeDefined");
7
+ function expectToBeSome(value) {
8
+ (0, expectToBeDefined_1.expectToBeDefined)(value);
9
+ (0, node_assert_1.ok)((0, util_typescript_1.isSome)(value));
10
+ }
11
+ //# sourceMappingURL=expectToBeSome.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"expectToBeSome.js","sourceRoot":"","sources":["../../../../../packages/testing-core/src/lib/expectToBeSome.ts"],"names":[],"mappings":";;AAMA,wCAGC;AATD,6CAAiC;AAEjC,uEAAmF;AAEnF,2DAAwD;AAExD,SAAgB,cAAc,CAAI,KAA4B;IAC5D,IAAA,qCAAiB,EAAC,KAAK,CAAC,CAAC;IACzB,IAAA,gBAAE,EAAC,IAAA,wBAAM,EAAC,KAAK,CAAC,CAAC,CAAC;AACpB,CAAC"}