Socket
Book a DemoInstallSign in
Socket

spy-stub

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

spy-stub

Spy and stub utils for testing

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

Spy-stub

Simple spy and stub functions for testing purposes.

You need to mock or spy a function and sinon.js feels like an overkill? Here are 2 stupidly simple helpers.

  • No dependencies
  • Node.js >= 4

Install

$ npm install spy-stub

Use

spy(object, method)

const { spy } = require('spy-stub');

const object = {
    snore(volume) {
        return `Snore with volume ${volume}`;
    };
};

it('should spy on a "snore" function', () => {
    const snoreSpy = spy(object, 'snore');

    const result = object.snore(5);

    assert.equal(snoreSpy.called, 1, 'Snore called once');
    const firstCallArgs = snoreSpy.args[0]; // "args" is list of arguments for each call
    const firstArgument = firstCallArgs[0];
    assert.equal(firstArgument, 5, 'Snore called with volume 5');
    assert.equal(result, 'Snore with volume 5', 'Result is correct');
});

stub()

const { stub } = require('spy-stub');

function functionToTest(number, snore) {
    if (number === 5) {
        return snore(number);
    }
};

it('should stub "snore" function', () => {
    const snoreStub = stub();

    functionToTest(5, snoreStub);

    assert.equal(snoreStub.called, 1, 'Snore called once');
    const firstCallArgs = snoreSpy.args[0];
    const firstArgument = firstCallArgs[0];
    assert.equal(firstArgument, 5, 'Snore called with volume 5');
});

stub(mockFunction)

const { stub } = require('spy-stub');

function functionToTest(number, snore) {
    return snore(number);
};

it('should stub "snore" function', () => {
    const snoreStub = stub(() => 'mock data');

    const result = functionToTest(5, snoreStub);

    assert.equal(snoreStub.called, 1, 'Snore called once');
    const firstCallArgs = snoreSpy.args[0];
    const firstArgument = firstCallArgs[0];
    assert.equal(firstArgument, 5, 'Snore called with volume 5');
    assert.equal(result, 'mock date', 'Result is mocked'); // <-- result is mocked
});

stub(object, method, mockFunction)

const { stub } = require('spy-stub');

it('should stub "snore" function', () => {
    const object = {
        snore(volume) {
            return `Snore with volume ${volume}`;
        };
    };
    const snoreStub = stub(object, 'snore', () => 'mock data');

    const result = object.snore(5);

    assert.equal(snoreStub.called, 1, 'Snore called once');
    const firstCallArgs = snoreSpy.args[0];
    const firstArgument = firstCallArgs[0];
    assert.equal(firstArgument, 5, 'Snore called with volume 5');
    assert.equal(result, 'mock date', 'Result is mocked'); // <-- result is mocked
});

Keywords

spy

FAQs

Package last updated on 18 May 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.