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
32
votes
3 answers

Stubbing window.location.href with Sinon

I am trying to test some client-side code and for that I need to stub the value of window.location.href property using Mocha/Sinon. What I have tried so far (using this example): describe('Logger', () => { it('should compose a Log', () => { …
Francesco Pezzella
  • 1,755
  • 2
  • 15
  • 18
31
votes
4 answers

stubbing a function using jest

is there a way to stub a function using jest API? I'm used to working with sinon stub, where I can write unit-tests with stubs for any function call coming out of my tested unit- http://sinonjs.org/releases/v1.17.7/stubs/ for…
Mattan Bitner
  • 1,463
  • 3
  • 14
  • 29
31
votes
1 answer

Sinon.JS - How can I get arguments from a stub?

I am trying to use Sinon to test a JS component which looks a bit like this... import Bootbox from "../helpers/bootbox"; import Guard from "../helpers/guard"; import UrlHelper from "../helpers/url-helper"; export default class DeleteButton { …
Keith Jackson
  • 3,078
  • 4
  • 38
  • 66
29
votes
4 answers

Calling different callbacks for stub on firstcall and second call

I'm looking for a way in sinon to call different functions in first and second call to the stub method. Here is an example: var func1 = function(connectionPolicy, requestOptions, callback) { callback({code: 403}); } var func2 =…
Rajesh Nagpal
  • 1,088
  • 1
  • 9
  • 12
29
votes
3 answers

sinon spy on standalone function

When I use Sinon on a function inside an object, it works: function myFunc() { console.log('hello'); } var myObj = { myFunc: myFunc }; var spy = sinon.stub(myFunc); myObj.myFunc(); expect(spy.called).to.be.true(); However, I don't know why…
giangnn
  • 405
  • 1
  • 5
  • 13
29
votes
9 answers

Sinon.Stub in Node with AWS-SDK

I am trying to write some test coverage for an app that uses the aws-sdk NPM module that pushes things up to a SQS queue, but I am unsure how to mock things correctly. Here is my test so far: var request = require('superagent'), expect =…
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189
28
votes
3 answers

Stubbing window functions in Jest

In my code, I trigger a callback upon "OK" click of a window.confirm prompt, and I want to test that the callback is triggered. In sinon, I can stub the window.confirm function via: const confirmStub = sinon.stub(window,…
Yangshun Tay
  • 49,270
  • 33
  • 114
  • 141
28
votes
4 answers

How do I mock a 'timeout' or 'failure' response using Sinon / Qunit?

I've had no problems sorting out mocking the success condition, but cannot seem to fathom how to mock the failure/timeout conditions when using Sinon and Qunit to test and ajax function: My set up is this: $(document).ready( function() { …
Caroline
  • 1,582
  • 3
  • 16
  • 30
27
votes
2 answers

What is the difference between Jest Mock functions and Sinon spies

I am mocking a function with Jest and the documentation says they are really 'spies'. I have also seen the use of spies in SinonJS but I could find no clear difference between the two. If they are serving the same purpose, is there any reason to…
Special Character
  • 2,321
  • 4
  • 25
  • 34
27
votes
2 answers

How to stub static methods with sinon in ES6?

var MyClassStub = sinon.createStubInstance(MyClass); MyClassStub doesn't contain static methods. How to fix that?
Rostislav Shtanko
  • 704
  • 2
  • 9
  • 30
27
votes
6 answers

Stubbing a Mongoose model with Sinon

I want to create a stub for the Mongoose save method in a particular model, so that any instance of my model I create will call the stub instead of the normal Mongoose save method. My understanding is that the only way to do this is to stub the…
amandawulf
  • 673
  • 3
  • 9
  • 18
26
votes
3 answers

Spying on a constructor in javascript with sinon

I am trying to create a spy on a constructor, and see if it gets called -- below are my tests. I'm using sinon-chai so the syntax is valid, but both tests fail. var foo = function(arg) { }; var bar = function(arg) { var baz = new…
billy
  • 531
  • 1
  • 4
  • 8
24
votes
2 answers

Sinon stub function used with destructuring

I wish to stub a function used in the file I'm currently testing. This function is required with a destructuring like this: const { theFunctionIWant } = require('path/to/module') When testing, the stub is never called, and the real function…
Sufiane
  • 1,507
  • 1
  • 14
  • 23
24
votes
3 answers

How to mock a function inside another function (which I am testing) using sinon?

let's say i have a function Func a() { //Do Something let c = b(); return c; } I want to test the function a and mock b() and in the mock want to assign c. Sinon.Stub(Test,"b").returns("DummyValue"); c should be assigned DummyValue. How…
Ravi Shankar
  • 319
  • 1
  • 2
  • 9
24
votes
5 answers

ES2016 Class, Sinon Stub Constructor

I'm trying to stub out a super call with sinon, and es2016 but I'm not having much luck. Any ideas why this isn't working? Running Node 6.2.2, this might be an issue with its implementation of classes/constructors. .babelrc file: { "presets": [ …
klyd
  • 3,939
  • 3
  • 24
  • 34