@chejholloway/result-guard-vitest
v0.1.2
Published
Vitest matchers for asserting on neverthrow Result/ResultAsync values: toBeOk, toBeErr, toMatchOk, toMatchErr.
Maintainers
Readme
result-guard-vitest
Vitest matchers for asserting on neverthrow Result/ResultAsync values.
Install
npm install -D result-guard-vitestAdd it to your Vitest config:
// vitest.config.ts
export default defineConfig({
test: {
setupFiles: ["result-guard-vitest"],
},
});Usage
import { guard } from "result-guard";
const guardedFetch = guard(fetchUser);
test("returns Ok for a valid id", async () => {
const result = await guardedFetch("123");
expect(result).toBeOk();
expect(result).toMatchOk({ id: "123", name: "Alice" });
});
test("returns Err for an empty id", async () => {
const result = await guardedFetch("");
expect(result).toBeErr();
});toMatchOk/toMatchErr support the same partial matchers as toMatchObject — expect.objectContaining(...) works fine.
If you pass an un-awaited ResultAsync directly, the matchers give you a specific error telling you to await it first, instead of a confusing generic assertion failure.
License
MIT
