@unthrown/vitest 0.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/LICENSE +21 -0
- package/README.md +47 -0
- package/dist/index.cjs +96 -0
- package/dist/index.d.cts +33 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +33 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +93 -0
- package/dist/index.mjs.map +1 -0
- package/docs/index.md +149 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Benoit Travers
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# @unthrown/vitest
|
|
2
|
+
|
|
3
|
+
> [Vitest](https://vitest.dev) matchers for [unthrown](https://github.com/btravstack/unthrown)'s
|
|
4
|
+
> `Result` and `AsyncResult`.
|
|
5
|
+
|
|
6
|
+
📖 **[Documentation](https://btravstack.github.io/unthrown/guide/testing)** ·
|
|
7
|
+
[API Reference](https://btravstack.github.io/unthrown/api/vitest/)
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add -D @unthrown/vitest
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Register the matchers once (e.g. in a Vitest setup file):
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import "@unthrown/vitest";
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
expect(ok(1)).toBeOk();
|
|
21
|
+
expect(ok(1)).toBeOkWith(1);
|
|
22
|
+
expect(err(new NotFound())).toBeErrTagged("NotFound");
|
|
23
|
+
expect(aDefect).toBeDefect();
|
|
24
|
+
|
|
25
|
+
// toBeErrTagged also takes an optional payload: a plain object matches exactly,
|
|
26
|
+
// an asymmetric matcher matches partially.
|
|
27
|
+
expect(err(new NotFound({ id: 1 }))).toBeErrTagged("NotFound", { id: 1 });
|
|
28
|
+
expect(err(new NotFound({ id: 1, msg: "x" }))).toBeErrTagged(
|
|
29
|
+
"NotFound",
|
|
30
|
+
expect.objectContaining({ id: 1 }),
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
// AsyncResult — `await` is required
|
|
34
|
+
await expect(fromPromise(load(), qualify)).toBeOk();
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Matchers: `toBeOk`, `toBeOkWith`, `toBeErr`, `toBeErrTagged(tag, expected?)`, `toBeDefect`.
|
|
38
|
+
|
|
39
|
+
> [!WARNING]
|
|
40
|
+
> For an `AsyncResult` the matcher is asynchronous — you **must** `await` the
|
|
41
|
+
> assertion, or it passes silently.
|
|
42
|
+
|
|
43
|
+
`vitest` is a peer dependency.
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
|
|
47
|
+
[MIT](../../LICENSE) © Benoit TRAVERS
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let unthrown = require("unthrown");
|
|
3
|
+
let vitest = require("vitest");
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
function isThenable(x) {
|
|
6
|
+
return (typeof x === "object" || typeof x === "function") && x !== null && typeof x.then === "function";
|
|
7
|
+
}
|
|
8
|
+
function isResult(x) {
|
|
9
|
+
return typeof x?.isOk === "function";
|
|
10
|
+
}
|
|
11
|
+
function render(result, stringify) {
|
|
12
|
+
if ((0, unthrown.isOk)(result)) return `Ok(${stringify(result.value)})`;
|
|
13
|
+
if ((0, unthrown.isErr)(result)) return `Err(${stringify(result.error)})`;
|
|
14
|
+
if ((0, unthrown.isDefect)(result)) return `Defect(${stringify(result.cause)})`;
|
|
15
|
+
return stringify(result);
|
|
16
|
+
}
|
|
17
|
+
function payloadOf(error) {
|
|
18
|
+
const out = {};
|
|
19
|
+
for (const key of Object.keys(error)) if (key !== "_tag" && key !== "name") out[key] = error[key];
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
function settle(received, stringify, check) {
|
|
23
|
+
const run = (value) => isResult(value) ? check(value) : {
|
|
24
|
+
pass: false,
|
|
25
|
+
message: () => `expected an unthrown Result, but received ${stringify(value)}`
|
|
26
|
+
};
|
|
27
|
+
return isThenable(received) ? Promise.resolve(received).then(run) : run(received);
|
|
28
|
+
}
|
|
29
|
+
function toBeOk(received) {
|
|
30
|
+
const { stringify } = this.utils;
|
|
31
|
+
return settle(received, stringify, (result) => {
|
|
32
|
+
const pass = (0, unthrown.isOk)(result);
|
|
33
|
+
return {
|
|
34
|
+
pass,
|
|
35
|
+
message: () => pass ? `expected result not to be Ok, but it was ${render(result, stringify)}` : `expected result to be Ok, but got ${render(result, stringify)}`
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function toBeOkWith(received, expected) {
|
|
40
|
+
const { stringify } = this.utils;
|
|
41
|
+
const { equals } = this;
|
|
42
|
+
return settle(received, stringify, (result) => {
|
|
43
|
+
const pass = (0, unthrown.isOk)(result) && equals(result.value, expected);
|
|
44
|
+
return {
|
|
45
|
+
pass,
|
|
46
|
+
message: () => pass ? `expected result not to be Ok(${stringify(expected)})` : `expected result to be Ok(${stringify(expected)}), but got ${render(result, stringify)}`
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function toBeErr(received) {
|
|
51
|
+
const { stringify } = this.utils;
|
|
52
|
+
return settle(received, stringify, (result) => {
|
|
53
|
+
const pass = (0, unthrown.isErr)(result);
|
|
54
|
+
return {
|
|
55
|
+
pass,
|
|
56
|
+
message: () => pass ? `expected result not to be Err, but it was ${render(result, stringify)}` : `expected result to be Err, but got ${render(result, stringify)}`
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
function toBeErrTagged(received, tag, expected) {
|
|
61
|
+
const { stringify } = this.utils;
|
|
62
|
+
const { equals } = this;
|
|
63
|
+
const hasExpected = expected !== void 0;
|
|
64
|
+
const label = hasExpected ? `Err tagged ${stringify(tag)} matching ${stringify(expected)}` : `Err tagged ${stringify(tag)}`;
|
|
65
|
+
return settle(received, stringify, (result) => {
|
|
66
|
+
const error = (0, unthrown.isErr)(result) ? result.error : void 0;
|
|
67
|
+
const pass = error?._tag === tag && (!hasExpected || equals(payloadOf(error), expected));
|
|
68
|
+
return {
|
|
69
|
+
pass,
|
|
70
|
+
message: () => pass ? `expected result not to be ${label}` : `expected result to be ${label}, but got ${render(result, stringify)}`
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
function toBeDefect(received) {
|
|
75
|
+
const { stringify } = this.utils;
|
|
76
|
+
return settle(received, stringify, (result) => {
|
|
77
|
+
const pass = (0, unthrown.isDefect)(result);
|
|
78
|
+
return {
|
|
79
|
+
pass,
|
|
80
|
+
message: () => pass ? `expected result not to be a Defect, but it was ${render(result, stringify)}` : `expected result to be a Defect, but got ${render(result, stringify)}`
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
vitest.expect.extend({
|
|
85
|
+
toBeOk,
|
|
86
|
+
toBeOkWith,
|
|
87
|
+
toBeErr,
|
|
88
|
+
toBeErrTagged,
|
|
89
|
+
toBeDefect
|
|
90
|
+
});
|
|
91
|
+
//#endregion
|
|
92
|
+
exports.toBeDefect = toBeDefect;
|
|
93
|
+
exports.toBeErr = toBeErr;
|
|
94
|
+
exports.toBeErrTagged = toBeErrTagged;
|
|
95
|
+
exports.toBeOk = toBeOk;
|
|
96
|
+
exports.toBeOkWith = toBeOkWith;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { MatcherResult, MatcherState } from "vitest";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
declare function toBeOk(this: MatcherState, received: unknown): MatcherResult;
|
|
5
|
+
declare function toBeOkWith(this: MatcherState, received: unknown, expected: unknown): MatcherResult;
|
|
6
|
+
declare function toBeErr(this: MatcherState, received: unknown): MatcherResult;
|
|
7
|
+
declare function toBeErrTagged(this: MatcherState, received: unknown, tag: string, expected?: unknown): MatcherResult;
|
|
8
|
+
declare function toBeDefect(this: MatcherState, received: unknown): MatcherResult;
|
|
9
|
+
/**
|
|
10
|
+
* The matchers `@unthrown/vitest` contributes to Vitest's `expect`. For an
|
|
11
|
+
* `AsyncResult`, `await` the assertion; `toBeOkWith` compares deeply.
|
|
12
|
+
*
|
|
13
|
+
* @typeParam R - the assertion's chaining return type.
|
|
14
|
+
*/
|
|
15
|
+
type UnthrownMatchers<R = unknown> = {
|
|
16
|
+
toBeOk: () => R;
|
|
17
|
+
toBeOkWith: (value: unknown) => R;
|
|
18
|
+
toBeErr: () => R;
|
|
19
|
+
/**
|
|
20
|
+
* Assert an `Err` whose error has `_tag === tag`. Optionally pass `expected`
|
|
21
|
+
* to also match the error's payload (its own props minus `_tag`/`name`): a
|
|
22
|
+
* plain object matches exactly, an asymmetric matcher (e.g.
|
|
23
|
+
* `expect.objectContaining(...)`) matches partially.
|
|
24
|
+
*/
|
|
25
|
+
toBeErrTagged: (tag: string, expected?: unknown) => R;
|
|
26
|
+
toBeDefect: () => R;
|
|
27
|
+
};
|
|
28
|
+
declare module "vitest" {
|
|
29
|
+
interface Matchers<T = any> extends UnthrownMatchers<T> {}
|
|
30
|
+
} //# sourceMappingURL=index.d.ts.map
|
|
31
|
+
//#endregion
|
|
32
|
+
export { UnthrownMatchers, toBeDefect, toBeErr, toBeErrTagged, toBeOk, toBeOkWith };
|
|
33
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";;;iBAsES,MAAA,CAAO,IAAA,EAAM,YAAA,EAAc,QAAA,YAAoB,aAAa;AAAA,iBAc5D,UAAA,CAAW,IAAA,EAAM,YAAA,EAAc,QAAA,WAAmB,QAAA,YAAoB,aAAa;AAAA,iBAenF,OAAA,CAAQ,IAAA,EAAM,YAAA,EAAc,QAAA,YAAoB,aAAa;AAAA,iBAc7D,aAAA,CACP,IAAA,EAAM,YAAA,EACN,QAAA,WACA,GAAA,UACA,QAAA,aACC,aAAa;AAAA,iBAqBP,UAAA,CAAW,IAAA,EAAM,YAAA,EAAc,QAAA,YAAoB,aAAa;;;;;;AArEJ;KA6FzD,gBAAA;EACV,MAAA,QAAc,CAAA;EACd,UAAA,GAAa,KAAA,cAAmB,CAAA;EAChC,OAAA,QAAe,CAAA;EAlFS;;;;;;EAyFxB,aAAA,GAAgB,GAAA,UAAa,QAAA,eAAuB,CAAA;EACpD,UAAA,QAAkB,CAAA;AAAA;AAAA;EAAA,UAKR,QAAA,kBAA0B,gBAAgB,CAAC,CAAA;AAAA"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { MatcherResult, MatcherState } from "vitest";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
declare function toBeOk(this: MatcherState, received: unknown): MatcherResult;
|
|
5
|
+
declare function toBeOkWith(this: MatcherState, received: unknown, expected: unknown): MatcherResult;
|
|
6
|
+
declare function toBeErr(this: MatcherState, received: unknown): MatcherResult;
|
|
7
|
+
declare function toBeErrTagged(this: MatcherState, received: unknown, tag: string, expected?: unknown): MatcherResult;
|
|
8
|
+
declare function toBeDefect(this: MatcherState, received: unknown): MatcherResult;
|
|
9
|
+
/**
|
|
10
|
+
* The matchers `@unthrown/vitest` contributes to Vitest's `expect`. For an
|
|
11
|
+
* `AsyncResult`, `await` the assertion; `toBeOkWith` compares deeply.
|
|
12
|
+
*
|
|
13
|
+
* @typeParam R - the assertion's chaining return type.
|
|
14
|
+
*/
|
|
15
|
+
type UnthrownMatchers<R = unknown> = {
|
|
16
|
+
toBeOk: () => R;
|
|
17
|
+
toBeOkWith: (value: unknown) => R;
|
|
18
|
+
toBeErr: () => R;
|
|
19
|
+
/**
|
|
20
|
+
* Assert an `Err` whose error has `_tag === tag`. Optionally pass `expected`
|
|
21
|
+
* to also match the error's payload (its own props minus `_tag`/`name`): a
|
|
22
|
+
* plain object matches exactly, an asymmetric matcher (e.g.
|
|
23
|
+
* `expect.objectContaining(...)`) matches partially.
|
|
24
|
+
*/
|
|
25
|
+
toBeErrTagged: (tag: string, expected?: unknown) => R;
|
|
26
|
+
toBeDefect: () => R;
|
|
27
|
+
};
|
|
28
|
+
declare module "vitest" {
|
|
29
|
+
interface Matchers<T = any> extends UnthrownMatchers<T> {}
|
|
30
|
+
} //# sourceMappingURL=index.d.ts.map
|
|
31
|
+
//#endregion
|
|
32
|
+
export { UnthrownMatchers, toBeDefect, toBeErr, toBeErrTagged, toBeOk, toBeOkWith };
|
|
33
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;iBAsES,MAAA,CAAO,IAAA,EAAM,YAAA,EAAc,QAAA,YAAoB,aAAa;AAAA,iBAc5D,UAAA,CAAW,IAAA,EAAM,YAAA,EAAc,QAAA,WAAmB,QAAA,YAAoB,aAAa;AAAA,iBAenF,OAAA,CAAQ,IAAA,EAAM,YAAA,EAAc,QAAA,YAAoB,aAAa;AAAA,iBAc7D,aAAA,CACP,IAAA,EAAM,YAAA,EACN,QAAA,WACA,GAAA,UACA,QAAA,aACC,aAAa;AAAA,iBAqBP,UAAA,CAAW,IAAA,EAAM,YAAA,EAAc,QAAA,YAAoB,aAAa;;;;;;AArEJ;KA6FzD,gBAAA;EACV,MAAA,QAAc,CAAA;EACd,UAAA,GAAa,KAAA,cAAmB,CAAA;EAChC,OAAA,QAAe,CAAA;EAlFS;;;;;;EAyFxB,aAAA,GAAgB,GAAA,UAAa,QAAA,eAAuB,CAAA;EACpD,UAAA,QAAkB,CAAA;AAAA;AAAA;EAAA,UAKR,QAAA,kBAA0B,gBAAgB,CAAC,CAAA;AAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { isDefect, isErr, isOk } from "unthrown";
|
|
2
|
+
import { expect } from "vitest";
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
function isThenable(x) {
|
|
5
|
+
return (typeof x === "object" || typeof x === "function") && x !== null && typeof x.then === "function";
|
|
6
|
+
}
|
|
7
|
+
function isResult(x) {
|
|
8
|
+
return typeof x?.isOk === "function";
|
|
9
|
+
}
|
|
10
|
+
function render(result, stringify) {
|
|
11
|
+
if (isOk(result)) return `Ok(${stringify(result.value)})`;
|
|
12
|
+
if (isErr(result)) return `Err(${stringify(result.error)})`;
|
|
13
|
+
if (isDefect(result)) return `Defect(${stringify(result.cause)})`;
|
|
14
|
+
return stringify(result);
|
|
15
|
+
}
|
|
16
|
+
function payloadOf(error) {
|
|
17
|
+
const out = {};
|
|
18
|
+
for (const key of Object.keys(error)) if (key !== "_tag" && key !== "name") out[key] = error[key];
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
function settle(received, stringify, check) {
|
|
22
|
+
const run = (value) => isResult(value) ? check(value) : {
|
|
23
|
+
pass: false,
|
|
24
|
+
message: () => `expected an unthrown Result, but received ${stringify(value)}`
|
|
25
|
+
};
|
|
26
|
+
return isThenable(received) ? Promise.resolve(received).then(run) : run(received);
|
|
27
|
+
}
|
|
28
|
+
function toBeOk(received) {
|
|
29
|
+
const { stringify } = this.utils;
|
|
30
|
+
return settle(received, stringify, (result) => {
|
|
31
|
+
const pass = isOk(result);
|
|
32
|
+
return {
|
|
33
|
+
pass,
|
|
34
|
+
message: () => pass ? `expected result not to be Ok, but it was ${render(result, stringify)}` : `expected result to be Ok, but got ${render(result, stringify)}`
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function toBeOkWith(received, expected) {
|
|
39
|
+
const { stringify } = this.utils;
|
|
40
|
+
const { equals } = this;
|
|
41
|
+
return settle(received, stringify, (result) => {
|
|
42
|
+
const pass = isOk(result) && equals(result.value, expected);
|
|
43
|
+
return {
|
|
44
|
+
pass,
|
|
45
|
+
message: () => pass ? `expected result not to be Ok(${stringify(expected)})` : `expected result to be Ok(${stringify(expected)}), but got ${render(result, stringify)}`
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function toBeErr(received) {
|
|
50
|
+
const { stringify } = this.utils;
|
|
51
|
+
return settle(received, stringify, (result) => {
|
|
52
|
+
const pass = isErr(result);
|
|
53
|
+
return {
|
|
54
|
+
pass,
|
|
55
|
+
message: () => pass ? `expected result not to be Err, but it was ${render(result, stringify)}` : `expected result to be Err, but got ${render(result, stringify)}`
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
function toBeErrTagged(received, tag, expected) {
|
|
60
|
+
const { stringify } = this.utils;
|
|
61
|
+
const { equals } = this;
|
|
62
|
+
const hasExpected = expected !== void 0;
|
|
63
|
+
const label = hasExpected ? `Err tagged ${stringify(tag)} matching ${stringify(expected)}` : `Err tagged ${stringify(tag)}`;
|
|
64
|
+
return settle(received, stringify, (result) => {
|
|
65
|
+
const error = isErr(result) ? result.error : void 0;
|
|
66
|
+
const pass = error?._tag === tag && (!hasExpected || equals(payloadOf(error), expected));
|
|
67
|
+
return {
|
|
68
|
+
pass,
|
|
69
|
+
message: () => pass ? `expected result not to be ${label}` : `expected result to be ${label}, but got ${render(result, stringify)}`
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function toBeDefect(received) {
|
|
74
|
+
const { stringify } = this.utils;
|
|
75
|
+
return settle(received, stringify, (result) => {
|
|
76
|
+
const pass = isDefect(result);
|
|
77
|
+
return {
|
|
78
|
+
pass,
|
|
79
|
+
message: () => pass ? `expected result not to be a Defect, but it was ${render(result, stringify)}` : `expected result to be a Defect, but got ${render(result, stringify)}`
|
|
80
|
+
};
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
expect.extend({
|
|
84
|
+
toBeOk,
|
|
85
|
+
toBeOkWith,
|
|
86
|
+
toBeErr,
|
|
87
|
+
toBeErrTagged,
|
|
88
|
+
toBeDefect
|
|
89
|
+
});
|
|
90
|
+
//#endregion
|
|
91
|
+
export { toBeDefect, toBeErr, toBeErrTagged, toBeOk, toBeOkWith };
|
|
92
|
+
|
|
93
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +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(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":"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"}
|
package/docs/index.md
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
**@unthrown/vitest**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
# @unthrown/vitest
|
|
6
|
+
|
|
7
|
+
## Type Aliases
|
|
8
|
+
|
|
9
|
+
### UnthrownMatchers
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
type UnthrownMatchers<R> = object;
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Defined in: [index.ts:164](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/vitest/src/index.ts#L164)
|
|
16
|
+
|
|
17
|
+
The matchers `@unthrown/vitest` contributes to Vitest's `expect`. For an
|
|
18
|
+
`AsyncResult`, `await` the assertion; `toBeOkWith` compares deeply.
|
|
19
|
+
|
|
20
|
+
#### Type Parameters
|
|
21
|
+
|
|
22
|
+
| Type Parameter | Default type | Description |
|
|
23
|
+
| ------ | ------ | ------ |
|
|
24
|
+
| `R` | `unknown` | the assertion's chaining return type. |
|
|
25
|
+
|
|
26
|
+
#### Properties
|
|
27
|
+
|
|
28
|
+
| Property | Type | Description | Defined in |
|
|
29
|
+
| ------ | ------ | ------ | ------ |
|
|
30
|
+
| <a id="tobedefect"></a> `toBeDefect` | () => `R` | - | [index.ts:175](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/vitest/src/index.ts#L175) |
|
|
31
|
+
| <a id="tobeerr"></a> `toBeErr` | () => `R` | - | [index.ts:167](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/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/4553631adb8280ef7c869dfae5bdb13be762095c/packages/vitest/src/index.ts#L174) |
|
|
33
|
+
| <a id="tobeok"></a> `toBeOk` | () => `R` | - | [index.ts:165](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/vitest/src/index.ts#L165) |
|
|
34
|
+
| <a id="tobeokwith"></a> `toBeOkWith` | (`value`) => `R` | - | [index.ts:166](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/vitest/src/index.ts#L166) |
|
|
35
|
+
|
|
36
|
+
## Functions
|
|
37
|
+
|
|
38
|
+
### toBeDefect()
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
function toBeDefect(this, received): ExpectationResult;
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Defined in: [index.ts:140](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/vitest/src/index.ts#L140)
|
|
45
|
+
|
|
46
|
+
#### Parameters
|
|
47
|
+
|
|
48
|
+
| Parameter | Type |
|
|
49
|
+
| ------ | ------ |
|
|
50
|
+
| `this` | `MatcherState` |
|
|
51
|
+
| `received` | `unknown` |
|
|
52
|
+
|
|
53
|
+
#### Returns
|
|
54
|
+
|
|
55
|
+
`ExpectationResult`
|
|
56
|
+
|
|
57
|
+
***
|
|
58
|
+
|
|
59
|
+
### toBeErr()
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
function toBeErr(this, received): ExpectationResult;
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Defined in: [index.ts:100](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/vitest/src/index.ts#L100)
|
|
66
|
+
|
|
67
|
+
#### Parameters
|
|
68
|
+
|
|
69
|
+
| Parameter | Type |
|
|
70
|
+
| ------ | ------ |
|
|
71
|
+
| `this` | `MatcherState` |
|
|
72
|
+
| `received` | `unknown` |
|
|
73
|
+
|
|
74
|
+
#### Returns
|
|
75
|
+
|
|
76
|
+
`ExpectationResult`
|
|
77
|
+
|
|
78
|
+
***
|
|
79
|
+
|
|
80
|
+
### toBeErrTagged()
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
function toBeErrTagged(
|
|
84
|
+
this,
|
|
85
|
+
received,
|
|
86
|
+
tag,
|
|
87
|
+
expected?): ExpectationResult;
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Defined in: [index.ts:114](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/vitest/src/index.ts#L114)
|
|
91
|
+
|
|
92
|
+
#### Parameters
|
|
93
|
+
|
|
94
|
+
| Parameter | Type |
|
|
95
|
+
| ------ | ------ |
|
|
96
|
+
| `this` | `MatcherState` |
|
|
97
|
+
| `received` | `unknown` |
|
|
98
|
+
| `tag` | `string` |
|
|
99
|
+
| `expected?` | `unknown` |
|
|
100
|
+
|
|
101
|
+
#### Returns
|
|
102
|
+
|
|
103
|
+
`ExpectationResult`
|
|
104
|
+
|
|
105
|
+
***
|
|
106
|
+
|
|
107
|
+
### toBeOk()
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
function toBeOk(this, received): ExpectationResult;
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Defined in: [index.ts:71](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/vitest/src/index.ts#L71)
|
|
114
|
+
|
|
115
|
+
#### Parameters
|
|
116
|
+
|
|
117
|
+
| Parameter | Type |
|
|
118
|
+
| ------ | ------ |
|
|
119
|
+
| `this` | `MatcherState` |
|
|
120
|
+
| `received` | `unknown` |
|
|
121
|
+
|
|
122
|
+
#### Returns
|
|
123
|
+
|
|
124
|
+
`ExpectationResult`
|
|
125
|
+
|
|
126
|
+
***
|
|
127
|
+
|
|
128
|
+
### toBeOkWith()
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
function toBeOkWith(
|
|
132
|
+
this,
|
|
133
|
+
received,
|
|
134
|
+
expected): ExpectationResult;
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Defined in: [index.ts:85](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/vitest/src/index.ts#L85)
|
|
138
|
+
|
|
139
|
+
#### Parameters
|
|
140
|
+
|
|
141
|
+
| Parameter | Type |
|
|
142
|
+
| ------ | ------ |
|
|
143
|
+
| `this` | `MatcherState` |
|
|
144
|
+
| `received` | `unknown` |
|
|
145
|
+
| `expected` | `unknown` |
|
|
146
|
+
|
|
147
|
+
#### Returns
|
|
148
|
+
|
|
149
|
+
`ExpectationResult`
|
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@unthrown/vitest",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Vitest matchers for unthrown",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"errors-as-values",
|
|
7
|
+
"matchers",
|
|
8
|
+
"result",
|
|
9
|
+
"testing",
|
|
10
|
+
"typescript",
|
|
11
|
+
"unthrown",
|
|
12
|
+
"vitest"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/btravstack/unthrown#readme",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/btravstack/unthrown/issues"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": "Benoit TRAVERS <benoit.travers.fr@gmail.com>",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/btravstack/unthrown.git",
|
|
23
|
+
"directory": "packages/vitest"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"docs"
|
|
28
|
+
],
|
|
29
|
+
"type": "module",
|
|
30
|
+
"main": "./dist/index.cjs",
|
|
31
|
+
"module": "./dist/index.mjs",
|
|
32
|
+
"types": "./dist/index.d.mts",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"import": {
|
|
36
|
+
"types": "./dist/index.d.mts",
|
|
37
|
+
"default": "./dist/index.mjs"
|
|
38
|
+
},
|
|
39
|
+
"require": {
|
|
40
|
+
"types": "./dist/index.d.cts",
|
|
41
|
+
"default": "./dist/index.cjs"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"./package.json": "./package.json"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"unthrown": "0.1.0"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/node": "24.13.2",
|
|
51
|
+
"@vitest/coverage-v8": "4.1.8",
|
|
52
|
+
"tsdown": "0.22.2",
|
|
53
|
+
"typedoc": "0.28.19",
|
|
54
|
+
"typedoc-plugin-markdown": "4.12.0",
|
|
55
|
+
"typescript": "6.0.3",
|
|
56
|
+
"vitest": "4.1.8",
|
|
57
|
+
"@unthrown/typedoc": "0.1.0",
|
|
58
|
+
"@unthrown/tsconfig": "0.1.0"
|
|
59
|
+
},
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"vitest": "^4"
|
|
62
|
+
},
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=22.19"
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "tsdown src/index.ts --format cjs,esm --dts --clean",
|
|
68
|
+
"build:docs": "typedoc",
|
|
69
|
+
"dev": "tsdown src/index.ts --format cjs,esm --dts --watch",
|
|
70
|
+
"test": "vitest run",
|
|
71
|
+
"typecheck": "tsc --noEmit"
|
|
72
|
+
}
|
|
73
|
+
}
|