Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

@unthrown/vitest

Vitest matchers for unthrown's Result and AsyncResult.

📖 Documentation · API Reference

pnpm add -D @unthrown/vitest

Register the matchers once (e.g. in a Vitest setup file):

import "@unthrown/vitest";
expect(Ok(1)).toBeOk();
expect(Ok(1)).toBeOkWith(1);
expect(Err(new NotFound())).toBeErrTagged("NotFound");
expect(aDefect).toBeDefect();

// toBeErrTagged also takes an optional payload: a plain object matches exactly,
// an asymmetric matcher matches partially.
expect(Err(new NotFound({ id: 1 }))).toBeErrTagged("NotFound", { id: 1 });
expect(Err(new NotFound({ id: 1, msg: "x" }))).toBeErrTagged(
  "NotFound",
  expect.objectContaining({ id: 1 }),
);

// AsyncResult — `await` is required
await expect(fromPromise(load(), qualify)).toBeOk();

Matchers: toBeOk, toBeOkWith, toBeErr, toBeErrWith, toBeErrTagged(tag, expected?), toBeDefect.

Warning

For an AsyncResult the matcher is asynchronous — you must await the assertion, or it passes silently.

vitest is a peer dependency.

License

MIT © Benoit TRAVERS