@8hobbies/test-utils

Utilities that wrap some assertions in vitest!expect with type assertions.

One nuance with some of the vitest!expect functions are the lack of type assertions.

Example

const iAmDefined: number | undefined = Math.random() < 1 ? 10 : undefined;
expect(iAmDefined).toBeDefined();
expect(iAmDefined + 1).toBe(11);
// ^ 'iAmDefined' is possibly 'undefined'.

This is annoying as either a verboseif-clause is needed to inform the TypeScript compiler that iAmDefined is not undefined, or an unsafe type assertion! is needed. Similar problems exist for null.

This module solves this family of problems with expectToBeDefined.Simply replace expect(iAmDefined).toBeDefined() with expectToBeDefined(iAmDefined), and the above error is gone.

Index

Functions