Skip to content

@unthrown/vitest


@unthrown/vitest ​

Type Aliases ​

HookContext ​

ts
type HookContext = object;

Defined in: index.ts:133

The slice of vitest's TestContext failOnForgottenAwait accepts, for manual wiring.

Properties ​

PropertyTypeDefined in
task?TaskLikeindex.ts:133

TaskLike ​

ts
type TaskLike = object;

Defined in: index.ts:131

The slice of a vitest task failOnForgottenAwait reads: a name and its parent suite.

Properties ​

PropertyTypeDefined in
namestringindex.ts:131
suite?TaskLikeindex.ts:131

UnthrownMatchers ​

ts
type UnthrownMatchers<R> = object;

Defined in: index.ts:462

The matchers @unthrown/vitest contributes to Vitest's expect. For an AsyncResult, await the assertion; toBeOkWith compares deeply.

Remarks ​

Import the package once (e.g. in a test setup file) to register the matchers and pull in this type augmentation.

For an AsyncResult the assertion is asynchronous and must be awaited. A forgotten await does not pass silently: an afterEach hook (registered on import, see failOnForgottenAwait) fails the test with an explicit message naming the matchers still pending when the test ended.

Example ​

ts
import "@unthrown/vitest";
import { Ok, fromSafePromise } from "unthrown";
import { expect, test } from "vitest";

test("sync", () => {
  expect(Ok(1)).toBeOkWith(1);
});

test("async", async () => {
  await expect(fromSafePromise(Promise.resolve(1))).toBeOk();
});

See ​

The Testing guide

Type Parameters ​

Type ParameterDefault typeDescription
Runknownthe assertion's chaining return type.

Properties ​

PropertyTypeDescriptionDefined in
toBeDefect() => Rexpect(result).toBeDefect() asserts the result is a Defect.index.ts:484
toBeDefectWith(expected) => RAssert a Defect whose cause is deeply equal to expected. expected is typed unknown because a defect's cause is unknown by design: nothing reaches that channel through a typed error, so there is no tighter type to give it and no tag-aware variant to add. An asymmetric matcher works as elsewhere: expect(result).toBeDefectWith(expect.any(TypeError)).index.ts:494
toBeErr() => Rexpect(Err("nope")).toBeErr() asserts the result is Err, regardless of the error.index.ts:468
toBeErrTagged(tag, expected?) => RAssert an Err whose error has _tag === tag. Optionally pass expected to also match the error's payload — its own props minus the keys TaggedError reserves (_tag, name, message, stack), so a subclass's override message = "…" does not leak into an exact assertion. A plain object matches exactly, an asymmetric matcher (e.g. expect.objectContaining(...)) matches partially. An explicitly-passed undefined asserts the payload equals undefined (it does not degrade to tag-only). expect(result).toBeErrTagged("NotFound", { id }) asserts the tag and payload.index.ts:481
toBeErrWith(expected) => R-index.ts:482
toBeOk() => Rexpect(Ok(1)).toBeOk() asserts the result is Ok, regardless of value.index.ts:464
toBeOkWith(value) => Rexpect(Ok(1)).toBeOkWith(1) asserts the result is Ok with a deeply-equal value.index.ts:466

Variables ​

toBeDefect ​

ts
const toBeDefect: (this, received, expected?) => ExpectationResult;

Defined in: index.ts:404

Parameters ​

ParameterType
thisMatcherState
receivedunknown
expected?unknown

Returns ​

ExpectationResult


toBeDefectWith ​

ts
const toBeDefectWith: (this, received, expected?) => ExpectationResult;

Defined in: index.ts:411

Parameters ​

ParameterType
thisMatcherState
receivedunknown
expected?unknown

Returns ​

ExpectationResult


toBeErr ​

ts
const toBeErr: (this, received, expected?) => ExpectationResult;

Defined in: index.ts:345

Parameters ​

ParameterType
thisMatcherState
receivedunknown
expected?unknown

Returns ​

ExpectationResult


toBeErrWith ​

ts
const toBeErrWith: (this, received, expected?) => ExpectationResult;

Defined in: index.ts:352

Parameters ​

ParameterType
thisMatcherState
receivedunknown
expected?unknown

Returns ​

ExpectationResult


toBeOk ​

ts
const toBeOk: (this, received, expected?) => ExpectationResult;

Defined in: index.ts:331

Parameters ​

ParameterType
thisMatcherState
receivedunknown
expected?unknown

Returns ​

ExpectationResult


toBeOkWith ​

ts
const toBeOkWith: (this, received, expected?) => ExpectationResult;

Defined in: index.ts:338

Parameters ​

ParameterType
thisMatcherState
receivedunknown
expected?unknown

Returns ​

ExpectationResult

Functions ​

failOnForgottenAwait() ​

ts
function failOnForgottenAwait(context?): void;

Defined in: index.ts:233

The check behind the registered afterEach hook: when async matcher assertions are still pending at the end of a test — a forgotten await — it abandons them (so they cannot late-fire as unhandled rejections) and throws an error naming the un-awaited matchers, failing that test.

Parameters ​

ParameterType
context?HookContext

Returns ​

void

Remarks ​

You never need to call this yourself: importing the package registers it as an afterEach hook alongside the matchers. It is exported so the mechanism itself is testable.


toBeErrTagged() ​

ts
function toBeErrTagged(
   this, 
   received, 
   tag, 
   expected?
): ExpectationResult;

Defined in: index.ts:362

Parameters ​

ParameterType
thisMatcherState
receivedunknown
tagstring
expected?unknown

Returns ​

ExpectationResult

Released under the MIT License.