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

@mattapperson/slapshot

v2.1.0

Published

Mock method calls with snapshots, run your intigation tests online or offline!

Downloads

71

Readme

Slapshot

NPM version dependencies Status

A simple, easy utility to write a single set of tests that can be run:

  • "online" (full integration-test)

OR

  • "offline" (auto-mocked unit tests with result snapshotted from when last run "online")

Install

yarn install --dev @mattapperson/slapshot

Why would someone want this?

Developers love to debate unit tests versus integration tests. However, the truth is they both are extremely useful, and both have serious downsides.

Integration tests give you a ton of confidence about your code... but are often slow, and in many cases require an internet connection.

Unit tests Run quickly, giving rapid feedback on the code your developing, thus allowing you to move faster. Still, many times you begin to assemble the many pieces of your codebase that are all well unit tested, but together they fail or are buggy. Not to mention often, maintaining Mocks for integration points is like maintaining comments in code too often; things don't match up or are insufficient.

You could write 2 test suites... but that's 2x the tests to maintain.

But with Slapshot:

  • one test/mock file, and one step... No need to write a test that generates the mocks first, allowing it to feel like Jest snapshots.
  • Tests to be run as unit tests... but if the ENV var SLAPSHOT_ONLINE=true is set the tests are run as integration tests. All with a single test being written!
  • No setup to ensure is working locally or a CI... environment starting or not can be programmatically controlled in the test itself using callWhenOnline
  • To update the snapshotted results, simply run your tests with the ENV var SLAPSHOT_ONLINE=true AND the CLI arg of --updateSnapshot

Requirements

  • Node 8 or higher
  • Jest 20.0 or higher

Example usage / API

/**
 * Method 1: callWhenOnline
 *
 * This block only runs if recording or running as intigration, and starts mongodb to be used in the tests
 **/
await callWhenOnline(async () => {
    await new Promise((resolve) => {
        const pipe = spawn("mongod", ["--dbpath=<LOCATION>", "--port", "1223"]);
        pipe.stdout.on("data", function (data) {
            if (data.indexOf("started") !== -1) {
                resolve();
            }
        });
    });
});

/**
 * Method 2: memorize
 *
 * if CLI flag `--updateSnapshot` or the env var `SLAPSHOT_ONLINE=true` is set to record, this returns the live server results
 * if not, it returns a snapshotted version. When live, the above callWhenOnline would have
 * started the server it would connect to making the process seamless
 **/
test("get user by ID", async () => {
    const userEmail = "[email protected]";
    const results = await memorize(
        `get user by email: ${userEmail}`,
        async () => getUser({ email: userEmail }),
        {
            // validateSnapshot can be a function, a bool, or an object
            // AS A FUNCTION:
            // It receves the live and snapshot values as params and throwing an error fails the compare

            // AS AN OBJECT:
            // You can use dot annotation to deeply compare or ignore the diff
            validateSnapshot: {
                // The ID might not always be the same in our tests,
                // so don't fail the snapshot validation because of it
                // when running in "online" mode
                ignore: ["id"],

                // Optionally, "whitelist" keys
                // only: ['name.first']
            },
        }
    );
    expect(results.fullName).toBe("Matt Apperson");
});

async function getUser({ email }) {
    const client = await mongodb.connect({ port: 1223 });
    return client.query({ email });
}