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

mismatched

v3.3.5

Published

A composable matching/assertion/validation framework in Typescript that displays mismatches as diff trees

Downloads

1,878

Readme

mismatched

mismatched is a Typescript-based assertion and matcher framework, with a sophisticated compositional approach. Available at https://github.com/rickmugridge/mismatched.

mismatched can be used:

For latest changes, see What is New.

Also see:

Example Assertions:

Here's a few simple examples of assertions (all these examples are in the examples directory here:

describe("Object-matching Examples", () => {
    const actual = {name: "hamcrest", address: {number: 3, street: "Oak St", other: [1, 2]}};

    it('Full object match', () => {
        assertThat(actual)
            .is({name: "hamcrest", address: {number: 3, street: "Oak St", other: [1, 2]}});
    });

    describe("Partial object match", () => {
        it('Do not care about one field', () => {
            assertThat(actual)
                .is({
                    name: "hamcrest",
                    address: {number: 3, street: "Oak St", other: match.any()}
                });
        });

        it('Matching optionally on several fields', () => {
            assertThat(actual)
                .is({
                    name: match.anyOf(["hamcrest", "tsDiffMatcher"]),
                    address: {
                        number: match.number.greater(0),
                        street: "Oak St",
                        other: match.ofType.array()
                    }
                });
        });
    });
});

We can see above that:

  • We can specify an object as the match, and a suitable matcher will be constructed automatically
  • We can use matchers at arbitrary points in the matching object (or array or at the top level)
    • Eg, because we don't care about some part of it (eg, a field which is randomly generated)
    • Eg, because we don't need to be too specific (some number, some array)

when matching fails, and the changes are minor, it provides feedback as a diff tree (looks to be related to a Haskell tree-diff). Eg:

failed

When the address.number was 3 but was expected to be 4.

Matchers

There are many built-in matchers. See Matchers. This includes a section on writing custom matchers.

Displaying the results of mismatches

We aim to provide useful output when a match fails. PrettyPrinter does this.

The results are provided as a JS object, which shows what matched and what didn't in a diff tree. It does not display it in JSON format. Instead, it is displayed as plain JS, so that it's easy to copy parts of it if a test is not right.

It aims to lay out the JS object/value to make it convenient to read. It tries to strike a balance between all being on one line and being spread out over many lines. Either extreme can make it difficult to read.

It allows for custom renderers.

validateThat()

validateThat() is intended for validating data received.

Here's a simple example of validations (see the micro tests for individual matchers for other examples):

    it("validateThat():", () => {
            const validationResult = validateThat({f: 2, g: true})
                .satisfies({f: match.ofType.number(), g: match.ofType.boolean()});
            assertThat(validationResult.passed()).is(true);
    });

See validateThat() for further details.

Automated tests for mismatched itself

There are many unit (micro) tests. To run:

  npm test

There are also property tests. To run:

   npm run prop-test

See Testing Mismatched with Property Tests for further details.