@kensio/smartass
v0.1.4
Published
TypeScript-first test assertion functions with precise type narrowing
Downloads
264
Maintainers
Readme
@kensio/smartass
TypeScript-first test assertion functions with precise type narrowing via assertion signatures.
See TypeScript assertion functions / assertion signatures
The fluent expect().toBe() interface in Jest and Vitest is readable, but is
unable to provide type information to TypeScript and IntelliSense.
This library provides assertion functions with assertion signatures so that you can benefit from the extra type information.
import { assertNonNullable } from '@kensio/smartass';
const user: { name: string } | undefined = getUser();
assertNonNullable(user);
// TypeScript now knows user is neither null nor undefined,
// so no need for ? or ! operators.
const userName = user.name;import { assertOneOf } from '@kensio/smartass';
const status = getStatus();
assertOneOf(status, ['pending', 'active', 'completed']);
// TypeScript now knows status is of type 'pending' | 'active' | 'completed'Installation
npm install @kensio/smartassAssertion functions
- assertArrayLength
- assertArrayMinLength
- assertArrayNotEmpty
- assertFalse
- assertIdentical
- assertInstanceOf
- assertNonNullable
- assertNumberBetween
- assertNumberToNearest
- assertOneOf
- assertThrowsError
- assertThrowsErrorAsync
- assertTrue
- assertTypeBigInt
- assertTypeBoolean
- assertTypeFunction
- assertTypeNumber
- assertTypeNumeric
- assertTypeObject
- assertTypeString
