Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sinon-spy-utils

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sinon-spy-utils

Utils functions for Sinon Spies and Stubs

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
35
increased by250%
Maintainers
1
Weekly downloads
 
Created
Source

sinon-spy-utils NPM Version Build Status Coverage Status dependencies Status devDependencies Status

This package is a collection of util functions to make your life easier when using Sinon.js. At the moment, it contains 3 functions: Mock (creating fake objects), SpyAndDo (scoped spy) and StubAndDo (scoped stub).

Installation

Node Installation

npm install sinon-spy-utils

You can then SpyUtils = require('sinon-spy-utils') or import { Mock } from 'sinon-spy-utils'.

Browser Installation

Use the minified UMD build in the dist folder: here. It exports a global window.SinonSpyUtils when imported as a <script> tag.

Usage

Every code snippet will be presented in 3 different styles: Node.js require, Node.js import and Browser Javascript (with required HTML <script>s).

Mock

This function creates a mock object containing functions corresponding to the provided names.

  • require:
  var Mock = require('sinon-spy-utils').Mock;
  ...
  var fakeUser = Mock('getName', 'getAvatar', 'getAge');
  • import:
  import { Mock } from 'sinon-spy-utils';
  ...
  const fakeUser = Mock('getName', 'getAvatar', 'getAge');
  • browser:
  <script src="sinon.min.js"></script>
  <script src="sinon-spy-utils.min.js"></script>
  ...
  var fakeUser = SinonSpyUtils.Mock('getName', 'getAvatar', 'getAge');

SpyAndDo

This function will spy the listed functions of the provided object and call the provided function. The spied functions will be restored whatever happens.

  • require:
  var SpyAndDo = require('sinon-spy-utils').SpyAndDo;
  ...
  SpyAndDo(console, 'error', 'log', function (spies) {
    functionThatCallsConsoleLog(); // your function
    expect(spies.log).to.have.been.called; // your test (here using sinon + chai style)
  });
  • import:
  import { SpyAndDo } from 'sinon-spy-utils';
  ...
  SpyAndDo(console, 'error', 'log', (spies) => {
    functionThatCallsConsoleLog(); // your function
    expect(spies.log).to.have.been.called; // your test (here using sinon + chai style)
  });
  • browser:
  <script src="sinon.min.js"></script>
  <script src="sinon-spy-utils.min.js"></script>
  ...
  SinonSpyUtils.SpyAndDo(console, 'error', 'log', function (spies) {
    functionThatCallsConsoleLog(); // your function
    expect(spies.log).to.have.been.called; // your test (here using sinon + chai style)
  });

StubAndDo

This function will stub the listed functions of the provided object and call the provided function. The stubbed functions will be restored whatever happens.

  • require:
  var StubAndDo = require('sinon-spy-utils').StubAndDo;
  ...
  StubAndDo(console, 'error', 'log', function (spies) {
    functionThatCallsConsoleLog(); // your function
    expect(spies.log).to.have.been.called; // your test (here using sinon + chai style)
  });
  • import:
  import { StubAndDo } from 'sinon-spy-utils';
  ...
  StubAndDo(console, 'error', 'log', (spies) => {
    functionThatCallsConsoleLog(); // your function
    expect(spies.log).to.have.been.called; // your test (here using sinon + chai style)
  });
  • browser:
  <script src="sinon.min.js"></script>
  <script src="sinon-spy-utils.min.js"></script>
  ...
  SinonSpyUtils.StubAndDo(console, 'error', 'log', function (spies) {
    functionThatCallsConsoleLog(); // your function
    expect(spies.log).to.have.been.called; // your test (here using sinon + chai style)
  });

License

MIT, Copyright (c) 2017 Louis Brunner

Keywords

FAQs

Package last updated on 22 Mar 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