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

test-established-expectations

v1.0.0

Published

Snapshot testing in chai using json files.

Downloads

48

Readme

test-established-expectations

Snapshot testing in chai using json files without automatic file population. Meaning, the first time you run the tests, they automatically populate the snapshots.

Note that because JSON values are compared, some inputs may be parsed unexpectedly. For example, Map objects will simply get stringified as {}.

Currently requires Mocha.

Usage

Installation

npm i -D test-established-expectations

You most likely want this a dev dependency (since it's meant for testing), hence the -D.

assertExpectedOutput

The most basic usage is with assertExpectedOutput. It accepts a function, calls it, and compares its output to the previously stored output:

import {describe, it} from 'mocha';
import {assertExpectedOutput} from 'test-established-expectations';

function myFunctionToTest() {
    return 'dummy function';
}

describe(myFunctionToTest.name, () => {
    it('should match expected output', async () => {
        await assertExpectedOutput(myFunctionToTest, {
            key: {
                subKey: 'basic expectation',
            },
        });
    });
});

assertExpectation

assertExpectation is a more versatile alternative, as it doesn't rely on a function being input. For assertExpectation to work, you must already have the result or output that you need to compare.

import {describe, it} from 'mocha';
import {assertExpectation} from 'test-established-expectations';

function myFunctionToTest() {
    return 'dummy function';
}

describe(myFunctionToTest.name, () => {
    it('should match expected output with function as key', async () => {
        const result = myFunctionToTest();

        await assertExpectation({
            key: {
                topKey: {function: myFunctionToTest},
                subKey: 'basic expectation with function key',
            },
            result,
        });
    });
});

expectationCases

Use expectationCases to quickly run multiple sets of expectation tests for a single function:

import {expectationCases} from 'test-established-expectations';

function myFunctionToTest(input1: string, input2: number) {
    return `${input1}-${input2}`;
}

describe(myFunctionToTest.name, () => {
    expectationCases(myFunctionToTest, [
        {
            it: 'should combine inputs',
            inputs: [
                'first input',
                2,
            ],
        },
        {
            it: 'should combine different inputs',
            inputs: [
                'one',
                22,
            ],
        },
    ]);

    // optionally pass an options parameter as the second parameter
    expectationCases(
        myFunctionToTest,
        {
            // if the given function to test is anonymous, this testKey must be provided
            testKey: 'topKey',
            cwd: 'my/path',
            expectationFile: 'my-expectation-file.json',
        },
        [
            {
                it: 'should combine different inputs with options',
                inputs: [
                    'more',
                    -1,
                ],
            },
        ],
    );
});

Other top keys

Other top key options that match common testing methods: {describe: 'my test key'}, {context: 'my test key'}:

import {describe, it} from 'mocha';
import {assertExpectation} from 'test-established-expectations';

function myFunctionToTest() {
    return 'dummy function';
}

describe(myFunctionToTest.name, () => {
    it('should match expected output', async () => {
        const result = myFunctionToTest();

        await assertExpectation({
            key: {
                // the topKey here extracts the function name manually
                topKey: {describe: myFunctionToTest.name},
                subKey: 'basic expectation',
            },
            result,
        });
    });
});

You can also just use a string for ultimate flexibility:

import {describe, it} from 'mocha';
import {assertExpectation} from 'test-established-expectations';

function myFunctionToTest() {
    return 'dummy function';
}

describe(myFunctionToTest.name, () => {
    it('should match expected output', async () => {
        const result = myFunctionToTest();

        await assertExpectation({
            key: {
                // the topKey here is a string
                topKey: 'my test',
                subKey: 'basic expectation',
            },
            result,
        });
    });
});

Keys

topKey and subKey are used to store the expected outputs in a nested JSON file object structure. This way, it's easy to mirror a test file's describe, it structure (describe for the topKey, it for the subKey).

Expectation file

A custom expectation file can be chosen with expectationFile:

import {describe, it} from 'mocha';
import {assertExpectedOutput} from 'test-established-expectations';

function myFunctionToTest() {
    return 'dummy function';
}

describe(myFunctionToTest.name, () => {
    it('should match expected output', async () => {
        await assertExpectedOutput(myFunctionToTest, {
            key: {
                subKey: 'basic expectation',
            },
            // use a custom json file to store expectations
            expectationFile: 'my-json-file.json',
        });
    });
});

Prevent overwriting stored values

By default, these assertions overwrite the expectations file with the latest results. This makes it easy to compare failures and to generate initial expectations. If you wish to disable this feature, set noOverwriteWhenDifferent to true:

import {describe, it} from 'mocha';
import {assertExpectedOutput} from 'test-established-expectations';

function myFunctionToTest() {
    return 'dummy function';
}

describe(myFunctionToTest.name, () => {
    it('should match expected output', async () => {
        await assertExpectedOutput(myFunctionToTest, {
            key: {
                subKey: 'basic expectation',
            },
            // prevent overwriting the stored expectations when this expectation fails
            noOverwriteWhenDifferent: true,
        });
    });
});