npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

4.5

v0.5.0

Published

Monadic test assertions

Downloads

276

Readme

4.5

Functional, curried, and monadic Assertions

The functional assertions library you've been missing! Your tests should follow the same practices you put into your codebase. If you prefer functional style APIs and practices in JavaScript or TypeScript your tests likely don't look too much like your applications. This library aims to solve this with functionally-oriented assertions that are lazy and monadic.

Table of Contents

Let me have it!

yarn add 4.5
# or
npm install --save 4.5

API

  • All functions of arity 2 or more are curried.
  • All types are defined below in the Types section.

Assertions

Create assertions that can be verified. Assertions are lazy by default, which means, they don't do anything unless specifically asked to verify themselves.

equals :: a → a → Assertion a

Creates an assertion that, when verified, will check value equality of items, including deep equality of objects or arrays.

is :: a → a → Assertion a

Creates an assertion that, when verified, will check reference equality of items.

pass :: a → Assertion a (Applicative)

Creates an assertion that, when verified, will always succeed with the given value.

This is 4.5's implementation of Applicative often called of or just in many other libraries.

fail :: * → Assertion *

Creates an assertion that, when verified, will always fail with a string representation of the given value.

throws :: (* → *) → Assertion Error

Creates an assertion that verifies a given function throws an error.

Combinators

Combinators are what allow you to compose your assertions and express many aspects of your tests in a declarative and expressive way.

map :: (a → b) → Assertion a → Assertion b (Functor)

Given a function from one value a to another value b and an assertion of a returns a new assertion of b.

ap :: Assertion (a → b) → Assertion a → Assertion b (Apply)

Given an assertion of a function of type a to type b and an assertion of type a returns an assertion of type b

chain :: (a → Assertion b) → Assertion a → Assertion b (Monad)

Given a function from one value a to Assertion b and an assertion of type a, returns an assertion of type b

bimap :: ([char] → [char]) → (a → b) → Assertion a → Assertion b (Bifunctor)

The first parameter is a function mapping one string to another upon unsuccessful verification of the provided assertion.

The second parameter is a function mapping a value a to a value of b upon successful verification of the provided assertion.

The third parameter is an assertion of type a.

Returns an assertion of type b. If the assertion given as the third parameter is unsuccessfully verified it will also fail to be verified. If the assertion given as the third parameter is verified it will also successfully verify.

concat:: Assertion a → Assertion a → Assertion a

Concatenate 2 assertions together. The second assertion passed to concat will not be verified if the first assertion failed.

Interpreters

Interpreters are functions that verify assertions.

assert :: Assertion a → unit

Given an assertion that can be successfully verified as correct, it will return void/undefined. Given an assertion that can not be verified as correct, it will throw an Error. This option is perfect for test frameworks that use thrown errors to signal test status, such as Mocha.

Types

export interface Assertion<T> {
  verify(verification: Verification<T>): void;
}

export interface Verification<T> {
  success(actual: T): any;
  failure(message: string): any;
}