New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

sinon-test

Package Overview
Dependencies
Maintainers
4
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sinon-test

> Automatic sandbox setup and teardown for SinonJS

  • 2.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
21K
increased by3.07%
Maintainers
4
Weekly downloads
 
Created
Source

Sinon Test

Automatic sandbox setup and teardown for SinonJS

Why?

Instead of writing tedious setup and teardown code for each individual test case you can let Sinon do all the cleanup for you.

So instead of doing this (using Mocha syntax):

var spy1;
var spy2;

afterEach(()=>{
    spy1.restore();
    spy2.restore();
});

it('should do something', ()=>{
    spy1 = sinon.spy(myFunc);
    spy2 = sinon.spy(myOtherFunc);
    myFunc(1);
    myFunc(2);
    assert(spy1.calledWith(1));
    assert(spy1.calledWith(2));
});

You could write just this

it('should do something', sinonTest(function(){
    var spy1 = this.spy(myFunc);
    var spy2 = this.spy(myOtherFunc);
    myFunc(1);
    myFunc(2);
    assert(spy1.calledWith(1));
    assert(spy1.calledWith(2));
})); //auto-cleanup

Sinon will take care of removing all the spies and stubs from the wrapped functions for you. It does this by using sinon.sandbox internally.

Do notice that we use a function and not a arrow function (ES2015) when wrapping the test with sinon.test as it needs to be able to access the this pointer used inside of the function, which using an arrow function would prevent.

See the Usage section for more details.

Installation

via npm (node package manager)

$ npm install sinon-test

or just add it as a <script src="dist/sinon-test.js"></script> tag to the html where you write your tests. A pre-built browser version can be found in the NPM package under dist/sinon-test.js.

Usage

Once initialized, the package creates a context for your test based on a sinon sandbox. You can use this in a wrapped test function to create sinon spies, stubs, etc. After your test completes, the sandbox restores anything modified to its original value.

If your test function takes any arguments, pass then to the test wrapper after the test function. If the last argument is a function, it is assumed to be a callback for an asynchronous test. The test function may also return a promise.

See the sinon documentation for more documentation on sandboxes.

sinon-test instances need to be configured with a sinon instance (version 2+) before they can be used.

var sinon = require('sinon');
var sinonTestFactory = require('sinon-test');
var sinonTest = sinonTestFactory(sinon);
var assert = require('assert');

describe('my function', function() {
    var myFunc = require('./my-func');

    it('should do something', sinonTest(function(){
        var spy = this.spy(myFunc);
        myFunc(1);
        assert(spy.calledWith(1));
    })); //auto-cleanup

});

API

cons sinonTest = require('sinon-test')(sinon);

In order to configure the sandbox that is created, a configuration hash can be passed as a 2nd argument to sinonTest:

const sinonTest = require('sinon-test')(sinon, {useFakeTimers: false});

Backwards compatibility

Sinon 1.x used to ship with this functionality built-in, exposed as sinon.test(). You can keep all your existing test code by configuring an instance of sinon-test, as done above, and then assigning it to sinon like this in your tests:

sinon.test = sinonTest;

FAQs

Package last updated on 08 Aug 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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc