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

quickspec

v2.0.0

Published

A tool for writing specs and quickly writing thorough tests for pure functions

Downloads

15

Readme

Quickspec

Quickspec is a library for simplifying the process of testing pure functions and compositions of pure functions. Rather than writing test after test to simply describe different cases, write a single test describing all cases at once as a scenario.

Quickspec is a library which can be integrated into your existing tests and is testing framework agnostic. It is designed to work in both client- and server-side environments so it goes with you where you want to be. Dump the boilerplate and start writing comprehensive tests quickly.

Installing Quickspec

To install Quickspec, just run the following command:

npm install quickspec --save-dev

That's it! You're ready to start using quickspec in your tests.

Setting up Quickspec

To use Quickspec in node, do the following setup:

const quickspec = require('quickspec`)({ verbose: false });

If you are using quickspec in client-side tests, the setup is almost the same:

const quickspec = quickspecFactory({ verbose: false });

Be sure, if you are using a test runner like Karma, you include the client-safe quickspec file in your list of files:

<project-root>/node_modules/quickspec/dist/quickspec.js

Writing tests with Quickspec

Writing tests with Quickspec requires changing the way you think about tests just a little. Let's dig in:

Let's say we want to test a multiply function which takes two numbers and returns their product. Here's what it could look like:

const multiply = a => b => a * b;

Writing a test function

The first thing we will want to do is create a test function. It will need to take a setup values object and a verify function:

const testMultiply = ({ a, b }, verify) => verify(multiply(a)(b));

Although this function is using object destructuring and arrow functions, you can do this with ES 5.1 code as well:

function testMultiply(setupValues, verify) {
    var a = setupValues.a;
    var b = setupValues.b;

    verify(multiplu(a)(b));
}

Writing a test spec

In this walkthrough I will use Mocha, but this will work with any test framework. Let's take a look at building and testing a spec:

it('should multiply values correctly according to our spec', function () {
    const specSet = [
        {
            name: 'Multiplying a a number by 0',
            setupValues: { a: 0, b: 5 },
            expectedValue: 0
        },
        {
            name: 'Multiplying a number by 1',
            setupValues: { a: 1, b: 7 },
            expectedValue: 7
        },
        {
            name: 'Multiplying a two numbers',
            setupValues: { a: 0.5, b: 9 },
            expectedValue: 4.5
        },
        {
            name: 'Multiplying a positive and negative number',
            setupValues: { a: -2, b: 11 },
            expectedValue: -22
        }
    ];

    const testMultiply = ({ a, b }, verify) => verify(multiply(a)(b));

    quickspec
        .verify(testMultiply)
        .over(specSet);
});

Testing a spec with a theorem

A theorem is a claim that can be proven to be true. Let's have a look at what this means to our test.

Note there is no expected value. The theorem we provide will give us the correct value to compare to.

it('should multiply values correctly according to our spec', function () {
    const specSet = [
        {
            name: 'Multiplying a a number by 0',
            setupValues: { a: 0, b: 5 }
        },
        {
            name: 'Multiplying a number by 1',
            setupValues: { a: 1, b: 7 }
        },
        {
            name: 'Multiplying a two numbers',
            setupValues: { a: 0.5, b: 9 }
        },
        {
            name: 'Multiplying a positive and negative number',
            setupValues: { a: -2, b: 11 }
        }
    ];

    const multiplyTheorem = ({ a, b }, actualResult) => (a * b) === actualResult;
    const testMultiply = ({ a, b }, verify) => verify(multiply(a)(b));

    quickspec
        .verify(testMultiply)
        .withTheorem(multiplyTheorem)
        .over(specSet);
});

Async Testing

Tests can be run asyncronously. The API simply requires an async call first. See examples below:

Async Tests with Expected Value

it('should support async tests', function (done) {

    const verifyAsyncAdd = ({ value1, value2 }, verify) => {
        const verifyOnDone = (error, result) => verify(result);
        asyncAdd(value1, value2, verifyOnDone);
    };

    const specSet = [
        {
            name: 'additive identity',
            setupValues: { value1: 1, value2: 0 },
            expectedResult: 1
        },
        {
            name: 'add two numbers',
            setupValues: { value1: 1, value2: 2 },
            expectedResult: 3
        }
    ];

    quickspec
        .async(done)
        .verify(verifyAsyncAdd)
        .over(specSet);
});

Async Tests with Theorem

it('should support async tests with theorem', function (done) {

    function verifyAsyncAdd ({ value1, value2 }, verify) {
        const verifyOnDone = (error, result) => verify(result);
        asyncAdd(value1, value2, verifyOnDone);
    };

    function addTheorem({ value1, value2 }, actualResult) {
        return (value1 + value2) === actualResult;
    }

    const specSet = [
        {
            name: 'additive identity',
            setupValues: { value1: 1, value2: 0 }
        },
        {
            name: 'add two numbers',
            setupValues: { value1: 1, value2: 2 }
        },
        {
            name: 'add a positive and negative number',
            setupValues: { value1: 3, value2: -7 }
        }
    ];

    quickspec
        .async(done)
        .verify(verifyAsyncAdd)
        .withTheorem(addTheorem)
        .over(specSet);
});

Changelog

2.0.0

  • Added async endpoints
  • Revised theorem behavior

1.0.0

  • Initial release