gitmyhub

test-extras

JavaScript ★ 17 updated 26d ago

Assertions and utilities that make node:test better

A lightweight assertion library that adds richer test checks, like error-returning throws and shape-based matchers, on top of Node's basic assert module.

JavaScriptNode.jsTypeScriptsetup: easycomplexity 1/5

test-extras is a small helper library for writing tests in JavaScript or TypeScript. Node's own built in assert module is deliberately basic, and this package fills in the gaps with the kind of assertions people are used to from testing frameworks like AVA and Jest, such as a throws check that actually hands you back the error it caught so you can inspect it further.

Every function in the package is a plain standalone function that raises an AssertionError when a check fails, so it is not tied to any particular test runner. It works equally well inside node:test, Vitest, or even a plain script with no test runner at all, and it does not modify or depend on any of them.

You install it as a development dependency with npm, then import only the specific assertions or matchers you need. The library covers a wide range of situations: catching thrown errors and rejected promises while returning the error itself, checking numeric closeness for floating point comparisons, checking whether a collection includes or excludes a value, checking a value is one of several allowed options, comparing arrays regardless of order, checking types with instanceOf, and checking whether something is empty.

It also provides asymmetric matchers, a way of checking that a value matches a shape or type rather than an exact value. These matchers, such as any type checks, objectContaining for partial object matching, arrayContaining for partial array matching, and stringMatching for pattern based string checks, are used inside a matches function and can be nested inside each other to build flexible checks on objects and arrays.

Overall this is a lightweight testing utility aimed at developers who want richer, more expressive assertions than Node's built in assert module offers, without committing to a full alternate testing framework.

Where it fits