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

@mwilliamson/precisely

v1.3.0

Published

Matcher library for JavaScript

Downloads

169

Readme

Precisely: better assertions for JavaScript/TypeScript tests

Precisely allows you to write precise assertions so you only test the behaviour you're really interested in. This makes it clearer to the reader what the expected behaviour is, and makes tests less brittle. This also allows better error messages to be generated when assertions fail. Inspired by Hamcrest.

For instance, suppose we want to make sure that a unique function removes duplicates from a list. We might write a test like so:

import { assertThat, containsExactly } from "@mwilliamson/precisely";

test("test unique() removes duplicates", () => {
    const result = unique(["a", "a", "b", "a", "b"]);

    assertThat(result, containsExactly("a", "b"));
});

The assertion will pass so long as result contains "a" and "b" in any order, but no other items. Unlike, say, assert.deepStrictEqual(result, ["a", "b"]), our assertion ignores the ordering of elements. This is useful when:

  • the ordering of the result is non-determistic, such as the results of SQL SELECT queries without an ORDER BY clause.

  • the ordering isn't specified in the contract of unique. If we assert a particular ordering, then we'd be testing the implementation rather than the contract.

  • the ordering is specified in the contract of unique, but the ordering is tested in a separate test case.

When the assertion fails, rather than just stating the two values weren't equal, the error message will describe the failure in more detail. For instance, if result has the value ["a", "a", "b"], we'd get the failure message:

Expected: iterable containing in any order:
  * 'a'
  * 'b'
but: had extra elements:
  * 'a'

Installation

npm install @mwilliamson/precisely

API

Use assertThat(value, matcher) to assert that a value satisfies a matcher.

Many matchers are composed of other matchers. If they are given a value instead of a matcher, then that value is wrapped in equalTo(). For instance, hasProperties({name: "bob"}) is equivalent to hasProperties({name: equalTo("bob")}).

  • equalTo(value): matches a value if it is equal to value using ===.

  • deepEqualTo(value): matches a value if it is deeply strictly equal to value.

  • hasProperties(properties): matches a value if it has the specified properties. For instance:

    assertThat(result, hasProperties({
        name: "The Princess Bride",
        authors: containsExactly(
            hasProperties({name: "William Goldman"}),
        ),
    }));
  • containsExactly(...elements): matches an iterable if it has the same elements in any order. For instance:

    assertThat(result, containsExactly("a", "b"));
    // Matches ["a", "b"] and ["b", "a"],
    // but not ["a", "a", "b"] nor ["a"] nor ["a", "b", "c"]
  • isSequence(...elements): matches an iterable if it has the same elements in the same order. For instance:

    assertThat(result, isSequence("a", "b"));
    // Matches ["a", "b"]
    // but not ["b", "a"] nor ["a", "b", "c"] nor ["c", "a", "b"]
  • includes(*args): matches an iterable if it includes all of the elements. For instance:

    
    assertThat(result, includes("a", "b"));
    // Matches ["a", "b"], ["b", "a"] and ["a", "c", "b"]
    // but not ["a", "c"] nor ["a"]
    assertThat(result, includes("a", "a"));
    // Matches ["a", "a"] and ["a", "a", "a"]
    // but not ["a"]
  • anything: matches all values.

  • startsWith(prefix): matches a string if it starts with prefix.