Questions tagged [sinon]

Sinon is a mocking framework for JavaScript, which can create spies, stubs and mocks.

Sinon is to create test spies, stubs and mocks that can be used with any JavaScript unit testing framework. It is also shipped as part of the Buster.JS unit test framework.

2840 questions
169
votes
9 answers

Cleaning up sinon stubs easily

Is there a way to easily reset all sinon spys mocks and stubs that will work cleanly with mocha's beforeEach blocks. I see sandboxing is an option but I do not see how you can use a sandbox for this beforeEach -> sinon.stub some, 'method' …
austinbv
  • 9,297
  • 6
  • 50
  • 82
141
votes
18 answers

How to mock localStorage in JavaScript unit tests?

Are there any libraries out there to mock localStorage? I've been using Sinon.JS for most of my other javascript mocking and have found it is really great. My initial testing shows that localStorage refuses to be assignable in firefox (sadface) so…
anthony sottile
  • 61,815
  • 15
  • 148
  • 207
140
votes
11 answers

Sinon error Attempted to wrap function which is already wrapped

Though there is a same question here but I could not find answer to my problem so here goes my question: I am testing my node js app using mocha and chai. I am using sinion to wrap my function. describe('App Functions', function(){ let mockObj…
rovy
  • 2,481
  • 5
  • 18
  • 26
120
votes
4 answers

Stubbing a class method with Sinon.js

I am trying to stub a method using sinon.js but I get the following error: Uncaught TypeError: Attempted to wrap undefined property sample_pressure as function I also went to this question (Stubbing and/or mocking a class in sinon.js?) and copied…
Paul
  • 2,409
  • 2
  • 26
  • 29
103
votes
7 answers

How to stub process.env in node.js?

I want to stub process.env.FOO with bar. var sinon = require('sinon'); var stub = sinon.stub(process.env, 'FOO', 'bar'); I'm confused. I read document, but still I don't understand yet.sinonjs docs sinonjs is one example, not sinonjs is okay.
Matt - sanemat
  • 5,418
  • 8
  • 37
  • 41
96
votes
6 answers

How to mock/replace getter function of object with Jest?

In Sinon I can do the following: var myObj = { prop: 'foo' }; sinon.stub(myObj, 'prop').get(function getterFn() { return 'bar'; }); myObj.prop; // 'bar' But how can I do the same with Jest? I can't just overwrite the function with…
I_like_foxes
  • 1,179
  • 1
  • 7
  • 12
87
votes
4 answers

Can sinon stub withArgs match some but not all arguments

I have a function I am stubbing that gets called with multiple arguments. I want to check just the first argument. The rest are callback function, so I want to leave them alone. Thus, I might have the following 2 calls, using ajax as an…
deitch
  • 14,019
  • 14
  • 68
  • 96
73
votes
1 answer

Proxyquire, rewire, SandboxedModule, and Sinon: pros & cons

When mocking Node dependencies, I've happened upon the following libraries: Proxyquire Rewire SandboxedModule Sinon They all seem to do more-or-less the same thing: allow you to mock require() calls (with the exception of Sinon which mocks pretty…
robrich
  • 13,017
  • 7
  • 36
  • 63
68
votes
5 answers

How does one stub promise with sinon?

I have a data service with following function function getInsureds(searchCriteria) { var deferred = $q.defer(); insuredsSearch.get(searchCriteria, function (insureds) { deferred.resolve(insureds); }, …
epitka
  • 17,275
  • 20
  • 88
  • 141
65
votes
2 answers

Sinon JS "Attempted to wrap ajax which is already wrapped"

I got the above error message when I ran my test. Below is my code (I'm using Backbone JS and Jasmine for testing). Does anyone know why this happens? $(function() { describe("Category", function() { beforeEach(function() { category =…
trivektor
  • 5,608
  • 9
  • 37
  • 53
61
votes
3 answers

How do I stub new Date() using sinon?

I want to verify that various date fields were updated properly but I don't want to mess around with predicting when new Date() was called. How do I stub out the Date constructor? import sinon = require('sinon'); import should =…
MrHen
  • 2,420
  • 2
  • 25
  • 39
59
votes
9 answers

Javascript: Mocking Constructor using Sinon

I am pulling my hair out trying to figure out how to mock a constructor using sinon. I have a function that will create multiple widgets by calling a constructor that accepts a few arguments. I want to verify that the constructor is called the…
sevenstripe
  • 635
  • 1
  • 6
  • 12
58
votes
5 answers

How to Unit Test React-Redux Connected Components?

I am using Mocha, Chai, Karma, Sinon, Webpack for Unit tests. I followed this link to configure my testing environment for React-Redux Code. How to implement testing + code coverage on React with Karma, Babel, and Webpack I can successfully test my…
Ayushya
  • 1,862
  • 1
  • 10
  • 15
55
votes
4 answers

How to properly unit test jQuery's .ajax() promises using Jasmine and/or Sinon?

I've got a fairly straightforward function which returns a jQuery .ajax() promise as such: CLAW.controls.validateLocation = function(val, $inputEl) { return $.ajax({ url: locationServiceUrl + 'ValidateLocation/', data: { …
J. Ky Marsh
  • 2,465
  • 3
  • 26
  • 32
54
votes
7 answers

How to mock e.preventDefault in react component's child

Hy, I don't know how to mock an inline function in React component's child My stack: sinon, chai, enzyme; Component usage: someFn()} /> Component's render: render() { return (
  • S Panfilov
    • 16,641
    • 17
    • 74
    • 96
  • 1
    2 3
    99 100