Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
sinon-spy-utils
Advanced tools
Readme
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).
npm install sinon-spy-utils
You can then SpyUtils = require('sinon-spy-utils')
or import { Mock } from 'sinon-spy-utils'
.
Use the minified UMD build in the dist
folder: here.
It exports a global window.SinonSpyUtils
when imported as a <script>
tag.
Every code snippet will be presented in 3 different styles: Node.js require
, Node.js import
and Browser Javascript (with required HTML <script>
s).
This function creates a mock object containing functions corresponding to the provided names.
var Mock = require('sinon-spy-utils').Mock;
...
var fakeUser = Mock('getName', 'getAvatar', 'getAge');
import { Mock } from 'sinon-spy-utils';
...
const fakeUser = Mock('getName', 'getAvatar', 'getAge');
<script src="sinon.min.js"></script>
<script src="sinon-spy-utils.min.js"></script>
...
var fakeUser = SinonSpyUtils.Mock('getName', 'getAvatar', 'getAge');
This function will spy the listed functions of the provided object and call the provided function. The spied functions will be restored whatever happens.
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 { 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)
});
<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)
});
This function will stub the listed functions of the provided object and call the provided function. The stubbed functions will be restored whatever happens.
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 { 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)
});
<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)
});
MIT, Copyright (c) 2017-2020 Louis Brunner
FAQs
Utility functions for Sinon Spies and Stubs
The npm package sinon-spy-utils receives a total of 23 weekly downloads. As such, sinon-spy-utils popularity was classified as not popular.
We found that sinon-spy-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.