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
51
votes
8 answers

How to stub a Typescript-Interface / Type-definition?

I work with Typescript on an AngularJS 1.X project. I use different Javascript libraries for different purposes. To unit-test my source I would like to stub some dependencies using the Typings (= interfaces). I don't want to use the ANY-type and…
user1879408
  • 1,948
  • 3
  • 17
  • 27
49
votes
1 answer

Reset "called" Count on Sinon Spy

How do I reset the "called" count on a Sinon spy before each test? Here's what I'm doing now: beforeEach(function() { this.spied = sinon.spy(Obj.prototype, 'spiedMethod'); }); afterEach(function() { Obj.prototype.spiedMethod.restore(); …
cantera
  • 24,479
  • 25
  • 95
  • 138
48
votes
6 answers

How do you mock MySQL (without an ORM) in Node.js?

I'm using Node.js with felixge's node-mysql client. I am not using an ORM. I'm testing with Vows and want to be able to mock my database, possibly using Sinon. Since I don't really have a DAL per se (aside from node-mysql), I'm not really sure how…
Josh Smith
  • 14,674
  • 18
  • 72
  • 118
48
votes
3 answers

How to unit test console output with mocha on nodejs?

Take into account the following example Javascript code below: function privateFunction (time) { if (time < 12) { console.log('Good morning'); } if (time >= 12 && time <19) { console.log('Good afternoon'); } else { console.log('Good night!');…
Kemel Zaidan
  • 503
  • 1
  • 5
  • 9
45
votes
1 answer

How to mock middleware in Express to skip authentication for unit test?

I have the following in Express //index.js var service = require('./subscription.service'); var auth = require('../auth/auth.service'); var router = express.Router(); router.post('/sync', auth.isAuthenticated, service.synchronise); …
jeh
  • 2,373
  • 4
  • 23
  • 38
45
votes
1 answer

Use SinonJS to stub and spy on the same function?

In the following example, I want to stub the get function to prevent the actual HTTP request from occurring. I want to spy on the get method to check what arguments it was called with. var request = require('request'), sinon =…
Armand
  • 23,463
  • 20
  • 90
  • 119
39
votes
2 answers

Difference between fake, spy, stub and mock of sinon library ( sinon fake vs spy vs stub vs mock )

I tried to understand difference between sinon library's fake, spy, stub and mock but not able to understand it clearly. Can anybody help me to understand about it?
Ziaullhaq Savanur
  • 1,848
  • 2
  • 17
  • 20
39
votes
4 answers

How do I use Sinon with Typescript?

If I use sinon with typescript then how do I cast the sinon mock to an instance of my object? For instance a SinonMock would be returned but my controller under test may require a specific service passed in to its constructor. var myServiceMock:…
Brandon
  • 984
  • 1
  • 11
  • 26
38
votes
2 answers

Possible to stub method twice within a single test to return different results?

I would like to re-stub someHandler.getStatus, but I'm getting TypeError: Attempted to wrap getStatus which is already wrapped .. it('is a test', function() { sandbox.stub(someHandler, 'getStatus', function(callback) { callback(null, { …
bobbyrne01
  • 6,295
  • 19
  • 80
  • 150
38
votes
3 answers

Verifying function call and inspecting arguments using sinon spies

I would like to verify that bar() is called inside foo() from my unit test. I figured that Sinon spies might be suitable, but I don't know how to use them. Is there any way to check that the method is called? Perhaps even extracting the arguments…
filur
  • 2,116
  • 6
  • 24
  • 47
35
votes
1 answer

Understanding Sinon.js's yield(), yields(), and callsArg()

What is the difference between stub.yield([arg1, arg2, ...]) spy.yields([arg1, arg2, ...]) stub.callsArg(index) in the Sinon.js stub library? stub.yield() is the only one that I've been able to grasp: stub = sinon.stub(API, 'call_remote'); …
TimeEmit
  • 4,516
  • 4
  • 18
  • 19
33
votes
2 answers

Mocking JavaScript constructor with Sinon.JS

I'd like to unit test the following ES6 class: // service.js const InternalService = require('internal-service'); class Service { constructor(args) { this.internalService = new InternalService(args); } getData(args) { let events =…
krl
  • 5,087
  • 4
  • 36
  • 53
33
votes
3 answers

Calling original function from Sinon.js Stub

I'm trying to intercept a call with Sinon.js so I can do some logging and then execute the original call. I don't see a way to do this with sinon.spy(), but I think I can do it with sinon.stub(). I provided a custom…
johnkchiu
  • 333
  • 1
  • 3
  • 5
32
votes
6 answers

Testing routers in backbone.js properly?

So I've just started to write tests for my in-progress javascript app, using sinon.js & jasmine.js. Works pretty well overall, but I need to also be able to test my routers. The routers, in their current state, will trigger an number of views and…
Industrial
  • 41,400
  • 69
  • 194
  • 289
32
votes
2 answers

How to stub a private method of a class written in typescript using sinon

I am writing unit tests for a public method which is, in turn, calling a private method of the class written in typescript (Node JS). Sample Code class A { constructor() { } public method1() { if(this.method2()) { //…
SHRUTHI BHARADWAJ
  • 587
  • 2
  • 5
  • 15