@unthrown/vitest 0.3.0 → 1.1.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 +5 -5
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +11 -11
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -17,15 +17,15 @@ import "@unthrown/vitest";
|
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
```ts
|
|
20
|
-
expect(
|
|
21
|
-
expect(
|
|
22
|
-
expect(
|
|
20
|
+
expect(Ok(1)).toBeOk();
|
|
21
|
+
expect(Ok(1)).toBeOkWith(1);
|
|
22
|
+
expect(Err(new NotFound())).toBeErrTagged("NotFound");
|
|
23
23
|
expect(aDefect).toBeDefect();
|
|
24
24
|
|
|
25
25
|
// toBeErrTagged also takes an optional payload: a plain object matches exactly,
|
|
26
26
|
// an asymmetric matcher matches partially.
|
|
27
|
-
expect(
|
|
28
|
-
expect(
|
|
27
|
+
expect(Err(new NotFound({ id: 1 }))).toBeErrTagged("NotFound", { id: 1 });
|
|
28
|
+
expect(Err(new NotFound({ id: 1, msg: "x" }))).toBeErrTagged(
|
|
29
29
|
"NotFound",
|
|
30
30
|
expect.objectContaining({ id: 1 }),
|
|
31
31
|
);
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["// @unthrown/vitest — custom Vitest matchers for `Result` / `AsyncResult`.\n//\n// Import this module once (e.g. in a test setup file) to register the matchers\n// and pull in the type augmentation:\n//\n// import \"@unthrown/vitest\";\n// expect(
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["// @unthrown/vitest — custom Vitest matchers for `Result` / `AsyncResult`.\n//\n// Import this module once (e.g. in a test setup file) to register the matchers\n// and pull in the type augmentation:\n//\n// import \"@unthrown/vitest\";\n// expect(Ok(1)).toBeOk();\n// await expect(fromSafePromise(p)).toBeOk(); // AsyncResult — `await` REQUIRED\n//\n// IMPORTANT: for an AsyncResult the matcher is asynchronous, so you MUST `await`\n// the assertion. A forgotten `await` makes the assertion pass silently.\n\nimport { isDefect, isErr, isOk, type Result } from \"unthrown\";\nimport { expect } from \"vitest\";\nimport type { MatcherResult, MatcherState } from \"vitest\";\n\ntype SomeResult = Result<unknown, unknown>;\ntype Stringify = (value: unknown) => string;\ntype Outcome = { pass: boolean; message: () => string };\n\nfunction isThenable(x: unknown): x is PromiseLike<unknown> {\n return (\n (typeof x === \"object\" || typeof x === \"function\") &&\n x !== null &&\n typeof (x as { then?: unknown }).then === \"function\"\n );\n}\n\nfunction isResult(x: unknown): x is SomeResult {\n return typeof (x as { isOk?: unknown } | null | undefined)?.isOk === \"function\";\n}\n\nfunction render(result: SomeResult, stringify: Stringify): string {\n if (isOk(result)) return `Ok(${stringify(result.value)})`;\n if (isErr(result)) return `Err(${stringify(result.error)})`;\n if (isDefect(result)) return `Defect(${stringify(result.cause)})`;\n return stringify(result);\n}\n\n// The \"payload\" of a TaggedError: its own enumerable properties minus the\n// `_tag` discriminant and the `name` (both set by TaggedError itself, not by\n// you). This is the data you passed to the constructor, so `toBeErrTagged`'s\n// optional second argument matches it: a plain object asserts it exactly, and an\n// asymmetric matcher (e.g. `expect.objectContaining(...)`) asserts it partially.\nfunction payloadOf(error: object): Record<string, unknown> {\n const out: Record<string, unknown> = {};\n for (const key of Object.keys(error)) {\n if (key !== \"_tag\" && key !== \"name\") out[key] = (error as Record<string, unknown>)[key];\n }\n return out;\n}\n\n// Resolve `received` — awaiting it when it is an AsyncResult — then run `check`.\n// This is what lets one matcher serve both `expect(result)` and\n// `await expect(asyncResult)`. A non-Result fails with a clear message.\nfunction settle(\n received: unknown,\n stringify: Stringify,\n check: (result: SomeResult) => Outcome,\n): MatcherResult {\n const run = (value: unknown): Outcome =>\n isResult(value)\n ? check(value)\n : {\n pass: false,\n message: () => `expected an unthrown Result, but received ${stringify(value)}`,\n };\n return isThenable(received) ? Promise.resolve(received).then(run) : run(received);\n}\n\nfunction toBeOk(this: MatcherState, received: unknown): MatcherResult {\n const { stringify } = this.utils;\n return settle(received, stringify, (result) => {\n const pass = isOk(result);\n return {\n pass,\n message: () =>\n pass\n ? `expected result not to be Ok, but it was ${render(result, stringify)}`\n : `expected result to be Ok, but got ${render(result, stringify)}`,\n };\n });\n}\n\nfunction toBeOkWith(this: MatcherState, received: unknown, expected: unknown): MatcherResult {\n const { stringify } = this.utils;\n const { equals } = this;\n return settle(received, stringify, (result) => {\n const pass = isOk(result) && equals(result.value, expected);\n return {\n pass,\n message: () =>\n pass\n ? `expected result not to be Ok(${stringify(expected)})`\n : `expected result to be Ok(${stringify(expected)}), but got ${render(result, stringify)}`,\n };\n });\n}\n\nfunction toBeErr(this: MatcherState, received: unknown): MatcherResult {\n const { stringify } = this.utils;\n return settle(received, stringify, (result) => {\n const pass = isErr(result);\n return {\n pass,\n message: () =>\n pass\n ? `expected result not to be Err, but it was ${render(result, stringify)}`\n : `expected result to be Err, but got ${render(result, stringify)}`,\n };\n });\n}\n\nfunction toBeErrTagged(\n this: MatcherState,\n received: unknown,\n tag: string,\n expected?: unknown,\n): MatcherResult {\n const { stringify } = this.utils;\n const { equals } = this;\n const hasExpected = expected !== undefined;\n const label = hasExpected\n ? `Err tagged ${stringify(tag)} matching ${stringify(expected)}`\n : `Err tagged ${stringify(tag)}`;\n return settle(received, stringify, (result) => {\n const error = isErr(result) ? result.error : undefined;\n const tagPass = (error as { _tag?: unknown } | undefined)?._tag === tag;\n const pass = tagPass && (!hasExpected || equals(payloadOf(error as object), expected));\n return {\n pass,\n message: () =>\n pass\n ? `expected result not to be ${label}`\n : `expected result to be ${label}, but got ${render(result, stringify)}`,\n };\n });\n}\n\nfunction toBeDefect(this: MatcherState, received: unknown): MatcherResult {\n const { stringify } = this.utils;\n return settle(received, stringify, (result) => {\n const pass = isDefect(result);\n return {\n pass,\n message: () =>\n pass\n ? `expected result not to be a Defect, but it was ${render(result, stringify)}`\n : `expected result to be a Defect, but got ${render(result, stringify)}`,\n };\n });\n}\n\nexpect.extend({ toBeOk, toBeOkWith, toBeErr, toBeErrTagged, toBeDefect });\n\nexport { toBeDefect, toBeErr, toBeErrTagged, toBeOk, toBeOkWith };\n\n/**\n * The matchers `@unthrown/vitest` contributes to Vitest's `expect`. For an\n * `AsyncResult`, `await` the assertion; `toBeOkWith` compares deeply.\n *\n * @typeParam R - the assertion's chaining return type.\n */\nexport type UnthrownMatchers<R = unknown> = {\n toBeOk: () => R;\n toBeOkWith: (value: unknown) => R;\n toBeErr: () => R;\n /**\n * Assert an `Err` whose error has `_tag === tag`. Optionally pass `expected`\n * to also match the error's payload (its own props minus `_tag`/`name`): a\n * plain object matches exactly, an asymmetric matcher (e.g.\n * `expect.objectContaining(...)`) matches partially.\n */\n toBeErrTagged: (tag: string, expected?: unknown) => R;\n toBeDefect: () => R;\n};\n\ndeclare module \"vitest\" {\n // oxlint-disable-next-line typescript/consistent-type-definitions, typescript/no-explicit-any -- a module augmentation must mirror Vitest's `interface Matchers<T = any>` exactly\n interface Matchers<T = any> extends UnthrownMatchers<T> {}\n}\n"],"mappings":";;;AAoBA,SAAS,WAAW,GAAuC;CACzD,QACG,OAAO,MAAM,YAAY,OAAO,MAAM,eACvC,MAAM,QACN,OAAQ,EAAyB,SAAS;AAE9C;AAEA,SAAS,SAAS,GAA6B;CAC7C,OAAO,OAAQ,GAA6C,SAAS;AACvE;AAEA,SAAS,OAAO,QAAoB,WAA8B;CAChE,IAAI,KAAK,MAAM,GAAG,OAAO,MAAM,UAAU,OAAO,KAAK,EAAE;CACvD,IAAI,MAAM,MAAM,GAAG,OAAO,OAAO,UAAU,OAAO,KAAK,EAAE;CACzD,IAAI,SAAS,MAAM,GAAG,OAAO,UAAU,UAAU,OAAO,KAAK,EAAE;CAC/D,OAAO,UAAU,MAAM;AACzB;AAOA,SAAS,UAAU,OAAwC;CACzD,MAAM,MAA+B,CAAC;CACtC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AAM,EAAE,WAAW;CACnB,MAAM,cAAc,aAAa,KAAA;CACjC,MAAM,QAAQ,cACV,cAAc,UAAU,GAAG,EAAE,YAAY,UAAU,QAAQ,MAC3D,cAAc,UAAU,GAAG;CAC/B,OAAO,OAAO,UAAU,YAAY,WAAW;EAC7C,MAAM,QAAQ,MAAM,MAAM,IAAI,OAAO,QAAQ,KAAA;EAE7C,MAAM,OADW,OAA0C,SAAS,QAC3C,CAAC,eAAe,OAAO,UAAU,KAAe,GAAG,QAAQ;EACpF,OAAO;GACL;GACA,eACE,OACI,6BAA6B,UAC7B,yBAAyB,MAAM,YAAY,OAAO,QAAQ,SAAS;EAC3E;CACF,CAAC;AACH;AAEA,SAAS,WAA+B,UAAkC;CACxE,MAAM,EAAE,cAAc,KAAK;CAC3B,OAAO,OAAO,UAAU,YAAY,WAAW;EAC7C,MAAM,OAAO,SAAS,MAAM;EAC5B,OAAO;GACL;GACA,eACE,OACI,kDAAkD,OAAO,QAAQ,SAAS,MAC1E,2CAA2C,OAAO,QAAQ,SAAS;EAC3E;CACF,CAAC;AACH;AAEA,OAAO,OAAO;CAAE;CAAQ;CAAY;CAAS;CAAe;AAAW,CAAC"}
|
package/docs/index.md
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
type UnthrownMatchers<R> = object;
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
Defined in: [index.ts:164](https://github.com/btravstack/unthrown/blob/
|
|
15
|
+
Defined in: [index.ts:164](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/vitest/src/index.ts#L164)
|
|
16
16
|
|
|
17
17
|
The matchers `@unthrown/vitest` contributes to Vitest's `expect`. For an
|
|
18
18
|
`AsyncResult`, `await` the assertion; `toBeOkWith` compares deeply.
|
|
@@ -27,11 +27,11 @@ The matchers `@unthrown/vitest` contributes to Vitest's `expect`. For an
|
|
|
27
27
|
|
|
28
28
|
| Property | Type | Description | Defined in |
|
|
29
29
|
| ------ | ------ | ------ | ------ |
|
|
30
|
-
| <a id="tobedefect"></a> `toBeDefect` | () => `R` | - | [index.ts:175](https://github.com/btravstack/unthrown/blob/
|
|
31
|
-
| <a id="tobeerr"></a> `toBeErr` | () => `R` | - | [index.ts:167](https://github.com/btravstack/unthrown/blob/
|
|
32
|
-
| <a id="tobeerrtagged"></a> `toBeErrTagged` | (`tag`, `expected?`) => `R` | Assert an `Err` whose error has `_tag === tag`. Optionally pass `expected` to also match the error's payload (its own props minus `_tag`/`name`): a plain object matches exactly, an asymmetric matcher (e.g. `expect.objectContaining(...)`) matches partially. | [index.ts:174](https://github.com/btravstack/unthrown/blob/
|
|
33
|
-
| <a id="tobeok"></a> `toBeOk` | () => `R` | - | [index.ts:165](https://github.com/btravstack/unthrown/blob/
|
|
34
|
-
| <a id="tobeokwith"></a> `toBeOkWith` | (`value`) => `R` | - | [index.ts:166](https://github.com/btravstack/unthrown/blob/
|
|
30
|
+
| <a id="tobedefect"></a> `toBeDefect` | () => `R` | - | [index.ts:175](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/vitest/src/index.ts#L175) |
|
|
31
|
+
| <a id="tobeerr"></a> `toBeErr` | () => `R` | - | [index.ts:167](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/vitest/src/index.ts#L167) |
|
|
32
|
+
| <a id="tobeerrtagged"></a> `toBeErrTagged` | (`tag`, `expected?`) => `R` | Assert an `Err` whose error has `_tag === tag`. Optionally pass `expected` to also match the error's payload (its own props minus `_tag`/`name`): a plain object matches exactly, an asymmetric matcher (e.g. `expect.objectContaining(...)`) matches partially. | [index.ts:174](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/vitest/src/index.ts#L174) |
|
|
33
|
+
| <a id="tobeok"></a> `toBeOk` | () => `R` | - | [index.ts:165](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/vitest/src/index.ts#L165) |
|
|
34
|
+
| <a id="tobeokwith"></a> `toBeOkWith` | (`value`) => `R` | - | [index.ts:166](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/vitest/src/index.ts#L166) |
|
|
35
35
|
|
|
36
36
|
## Functions
|
|
37
37
|
|
|
@@ -41,7 +41,7 @@ The matchers `@unthrown/vitest` contributes to Vitest's `expect`. For an
|
|
|
41
41
|
function toBeDefect(this, received): ExpectationResult;
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
-
Defined in: [index.ts:140](https://github.com/btravstack/unthrown/blob/
|
|
44
|
+
Defined in: [index.ts:140](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/vitest/src/index.ts#L140)
|
|
45
45
|
|
|
46
46
|
#### Parameters
|
|
47
47
|
|
|
@@ -62,7 +62,7 @@ Defined in: [index.ts:140](https://github.com/btravstack/unthrown/blob/1e34025e0
|
|
|
62
62
|
function toBeErr(this, received): ExpectationResult;
|
|
63
63
|
```
|
|
64
64
|
|
|
65
|
-
Defined in: [index.ts:100](https://github.com/btravstack/unthrown/blob/
|
|
65
|
+
Defined in: [index.ts:100](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/vitest/src/index.ts#L100)
|
|
66
66
|
|
|
67
67
|
#### Parameters
|
|
68
68
|
|
|
@@ -87,7 +87,7 @@ function toBeErrTagged(
|
|
|
87
87
|
expected?): ExpectationResult;
|
|
88
88
|
```
|
|
89
89
|
|
|
90
|
-
Defined in: [index.ts:114](https://github.com/btravstack/unthrown/blob/
|
|
90
|
+
Defined in: [index.ts:114](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/vitest/src/index.ts#L114)
|
|
91
91
|
|
|
92
92
|
#### Parameters
|
|
93
93
|
|
|
@@ -110,7 +110,7 @@ Defined in: [index.ts:114](https://github.com/btravstack/unthrown/blob/1e34025e0
|
|
|
110
110
|
function toBeOk(this, received): ExpectationResult;
|
|
111
111
|
```
|
|
112
112
|
|
|
113
|
-
Defined in: [index.ts:71](https://github.com/btravstack/unthrown/blob/
|
|
113
|
+
Defined in: [index.ts:71](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/vitest/src/index.ts#L71)
|
|
114
114
|
|
|
115
115
|
#### Parameters
|
|
116
116
|
|
|
@@ -134,7 +134,7 @@ function toBeOkWith(
|
|
|
134
134
|
expected): ExpectationResult;
|
|
135
135
|
```
|
|
136
136
|
|
|
137
|
-
Defined in: [index.ts:85](https://github.com/btravstack/unthrown/blob/
|
|
137
|
+
Defined in: [index.ts:85](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/vitest/src/index.ts#L85)
|
|
138
138
|
|
|
139
139
|
#### Parameters
|
|
140
140
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unthrown/vitest",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Vitest matchers for unthrown",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"errors-as-values",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"./package.json": "./package.json"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"unthrown": "
|
|
47
|
+
"unthrown": "1.1.0"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@types/node": "24.13.2",
|