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

approval-result-factory

v0.3.2

Published

Automatically build service fakes into an approvable result object

Downloads

31

Readme

approval-result-factory

What?

Approval result factory, builds an object that allows for the easier approval of complex interactions of service mocks.

This library is intended to be used with sinon and approvals. It also has support for stubcontractor a touch integration testing library.

Why?

When testing complex mock heavy interactions, there is a lot of boiler plate code used to get a nicely readable approvals result. This facory builds an object to stringify and use.

Note

This library works well with spyFactory.


addDatum

Add datum is the simplest of calls. It adds data to the result unconditionally.

let resultBuilder = resultBuilderFactory();
resultBuilder.addDatum('add string', 'value string');

const result = resultBuilder.getResult();

At this point the result looks like:

{
    "add string": "value string"
}

addData

Add data is the next simplest call. It adds an array of arrays to the object.

let resultBuilder = resultBuilderFactory();
resultBuilder.addData(
    [
        ['add string', 'value string'],
        ['added int', 5],
        ['added null', null],
    ]
);

const result = resultBuilder.getResult(); 

At this point the result looks like:

{
    "add string": "value string",
    "added int": 5,
    "added null": null
}

addCall

Add call looks at a sinon spy method and conditionally adds it if it has been called.

let call1 = sinon.spy();
let call2 = sinon.spy();

call2('I called number 2');

let resultBuilder = resultBuilderFactory();

resultBuilder.addCall('first', call1);
resultBuilder.addCall('second', call2);

const result = resultBuilder.getResult();

Because only call2 was exersized the result looks like:

{
    "second": [
        [
            "I called number 2"
        ]
    ]
}

addCalls

'addcalls' addes spies in an array of arrays.

let call1 = sinon.spy();
let call2 = sinon.spy();
let call3 = sinon.spy();

call1(1);
call3('call3');

let resultBuilder = resultBuilderFactory();

resultBuilder.addCalls(
    [
        ['first', call1],
        ['second', call2],
        ['third', call3]
    ]
);

const result = resultBuilder.getResult();

The result now looks like:

{
    "first": [
        [
            1
        ]
    ],
    "third": [
        [
            "call3"
        ]
    ]
}

addFakeService

Add service fake takes an object with methods that are spies. It then conditionally addes each of those methods if they have been called.

NOTE

The fake service must have a property __name which will contain the name of the module being faked.

const myFakeService = {
    __name: 'calculator',
    add: sinon.spy(),
    subtraction: sinon.spy(),
    multiplication: sinon.spy()
};

myFakeService.add(1, 2);
myFakeService.multiplication(2, 2);

let resultBuilder = resultBuilderFactory();

resultBuilder.addFakeService(myFakeService);
const result = resultBuilder.getResult();

Because subtraction was not called the result looks like this:

{
    "calculator.add": [
        [
            1,
            2
        ]
    ],
    "calculator.multiplication": [
        [
            2,
            2
        ]
    ]
}

addFakeServices

Add fake services takes an array of fake services and conditionally addes each of their spyied on methods.

let fakeCalculator = {
    __name: 'calculator',
    add: sinon.spy(),
    subtraction: sinon.spy(),
    multiplication: sinon.spy()
};

let fakeCar = {
    __name: 'car',
    accelerate: sinon.spy(),
    decelerate: sinon.spy(),
    openWindow: function () {},
};

fakeCalculator.add(1, 2);
fakeCalculator.multiplication(2, 2);

fakeCar.accelerate('faster');
fakeCar.decelerate('slower');
fakeCar.openWindow();

resultBuilder.addFakeServices(
    [
        fakeCalculator,
        fakeCar
    ]
);

Because the subtraction method was not called and the openWindow function is not a spy, the result is the followind:

{
    "calculator.add": [
        [
            1,
            2
        ],
    ],
    "calculator.multiplication": [
        [
            2,
            2
        ]
    ],
    "car.accelerate": [
        [
            "faster"
        ]
    ],
    "car.decelerate": [
        [
            "slower"
        ]
    ]
}

Starting State

You can initialize the object with some starting state.

let resultBuilder = resultBuilderFactory({ author: 'RJK' });

resultBUilder.addDatum('some data', 8);

const result = resultBuilder.getResult();

The result looks like:

{
    "author": "RJK",
    "some data": 8
}