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

soother

v0.0.7

Published

Standalone test dummies, stubs, mocks, fakes and spies for JavaScript. Works with any unit testing framework.

Downloads

11

Readme

soother

logo

soother is a cross-browser standalone library that provide test dummies, stubs, mocks, fakes and spies for JavaScript. Works with any unit testing framework.

npm version

Notes

It's a quite different but very simple.

The library makes use of an idiomatically different style than other libraries that using stubs.

Feel free to open merge requests with upgrades and report issues.

GitHub Repository

Getting Started

npm install soother --save-dev

Dummy

A minimal function is which does nothing and returns nothing

from 'K&R' (c)

dummy

        import Soother from 'soother';

        const instance = new Object();
        instance.test = Soother.dummy();
        instance.test('test');
        const [firstCall] = instance.test.calls;
        const [argOfCall] = firstCall.args;
        expect(argOfCall).to.equal('test');

Stub

stub

        const stubbedInstance = Soother.stub(instance);

        stubbedInstance.test();
        stubbedInstance.otherMethod();
        stubbedInstance.anyOther();

        stubbedInstance.test('test');
        stubbedInstance.otherMethod();
        stubbedInstance.anyOther();

        expect(stubbedInstance.test.calls.length).to.equal(2);
        expect(stubbedInstance.otherMethod.calls.length).to.equal(2);
        expect(stubbedInstance.anyOther.calls.length).to.equal(2);
        
        const [, methodCall] = stubbedInstance.test.calls;
        const [callArg] = methodCall.args;
        expect(callArg).to.equal('test');

Mock

mock

        let mockInstance = Soother.mock(instance, {
            test: () => 'mock'
        });
        mockInstance.testProperty = 1;
        expect(mockInstance.test()).to.equal('mock');
        expect(mockInstance.testProperty.calls.length).to.equal(1);

        mockInstance = Soother.mock(instance, {
            testProperty: 'mock'
        });
        expect(mockInstance.testProperty).to.equal('mock');
        mockInstance.test();
        expect(mockInstance.test.calls.length).to.equal(1);

Spy

        const spyInstance = Soother.spy(instance);

        spyInstance.test('spy');
        const prop = spyInstance.testProperty;
        expect(prop).to.equal('test');
        spyInstance.testProperty = 'spy';
        spyInstance.test = 'spy';

        const { sets, gets } = spyInstance.spied;
        const [firstSet] = sets;
        const [firstGet] = gets;
        expect(sets.length).to.equal(2);
        expect(gets.length).to.equal(2);
        expect(firstGet.type).to.equal('method');
        expect(firstGet.args.length).to.equal(3);
        expect(firstSet.testProperty).to.equal('spy');

Fake AJAX

Example with XMLHttpRequest

        import Soother from 'soother';

        const url = 'http://www.test.org/test.txt';

        const fake = Soother.fakeXMLHttpRequest();
        fake.register('GET', url, 'test response');


        const oReq = new XMLHttpRequest();
        oReq.onreadystatechange = () => {
            expect(oReq.responseText).to.equal('test response');
        };
        
        oReq.open('GET', url);
        oReq.setRequestHeader('Content-Type', 'text/html; charset=utf-8');
        oReq.send();

        expect(oReq.getResponseHeader('Content-Type')).to.equal('text/html; charset=utf-8');
        expect(oReq.getAllResponseHeaders()).to.equal(null);

        const [firstCall] = fake.calls();
        expect(firstCall.method).to.equal('GET');
        expect(firstCall.url).to.equal('http://www.test.org/test.txt');
        expect(firstCall.data).to.equal(undefined);

        const methods = fake.methods();
        expect(methods.GET[url]).to.equal('test response');

Example with axios

    import Axios from 'axios';
    import { SESSION_API_URL } from '../../constants';
    
    /**
     * Call specific endpoint via HTTP and read items
     *
     * @returns {AxiosPromise} Promise to return all items
     */
    export function getItems() {
        return Axios.get(`${SESSION_API_URL}/items`);
    }
    
    import Soother from 'soother';
    import { getItems } from '../itemService';
    
    describe('Test Item API Service', () => {
        let fakeAjax = null;
    
        beforeEach(done => {
            fakeAjax = Soother.fakeXMLHttpRequest();
            done();
        });
    
        it('should provide method for getting items', done => {
            getItems().then(() => {
                const [call] = fakeAjax.calls();
    
                expect(call.method).to.be.equal('GET');
                expect(call.url).to.be.equal('/api/v1/items');
    
                done();
            }).catch(done);
        });

You also can setup fake backend with end-point mocks


import Soother from 'soother';

// TODO remove after integration
export default function fakeBackend() {
    const fakeAjax = Soother.fakeXMLHttpRequest();

    const url = '/api/v1/item';

    const list = [{ name: '9' }, { name: '10' }];
    const item = { name: '11' };

    fakeAjax.register('GET', `${url}s`, list);
    fakeAjax.register('POST', url, item);
    fakeAjax.register('PUT', url, item);
    fakeAjax.register('DELETE', `${url}/11`, item);
}

Stub and Mock CommonJS modules

        const ms = Soother.moduleSoother();
        ms.mockModule('.\.css$', { test: 'test' });
        ms.stubModule('.\.svg$');

        const svg1 = require('./icon_1.svg');
        const svg2 = require('./icon_2.svg');

        const css1 = require('./styles_1.css');
        const css2 = require('./styles_2.css');

        expect(svg1).to.equal({});
        expect(svg2).to.equal({});

        expect(css1.test).to.equal('test');
        expect(css2.test).to.equal('test');

Alternatives

Tests

cd soother

npm i

npm test

Then check ./coverage/report.html

License

Copyright (c) 2017 artur.basak aka archik Licensed under the Apache-2.0 license.