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

Just for understanding purpose call: FuncInfoCollector = is a Function that records arguments, return value, the value of this(context) and exception thrown (if any) for all of its calls. (this FuncInfoCollector is dummy name given by me, it is not present in SINON lib) Fake = FuncInfoCollector + can only create a fake function. To … Read more

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

Sinon has a nice API for handling multiple calls (stub.onCall(n);) to the same stubbed method. Example from stub api doc: “test should stub method differently on consecutive calls”: function () { var callback = sinon.stub(); callback.onCall(0).returns(1); callback.onCall(1).returns(2); callback.returns(3); callback(); // Returns 1 callback(); // Returns 2 callback(); // All following calls return 3 } You … Read more

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

You can use sinon to stub isAuthenticated method, but you should do that before a reference to auth.isAuthenticated is set as a middleware, so before you require the index.js and app is created. Most likely you would want this in a beforeEach hook: var app; var auth; beforeEach(function() { auth = require(‘../wherever/auth/auth.service’); sinon.stub(auth, ‘isAuthenticated’) .callsFake(function(req, … Read more

How to unit test console output with mocha on nodejs?

I prefer mocha-sinon over “plain” sinon because it integrates nicely with Mocha. Example: var expect = require(‘chai’).expect; require(‘mocha-sinon’); // Function to test, can also be in another file and as long as it’s // being called through some public interface it should be testable. // If it’s not in any way exposed/exported, testing will be … Read more

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

With sinon, you can put a mock or stub around an entire module. For example, suppose the mysql module has a function query: var mock; mock = sinon.mock(require(‘mysql’)) mock.expects(‘query’).with(queryString, queryParams).yields(null, rows); queryString, queryParams are the input you expect. rows is the output you expect. When your class under test now require mysql and calls the … Read more

Reset “called” Count on Sinon Spy

This question was asked a while back but may still be interesting, especially for people who are new to sinon. this.spied.reset() is not needed as Obj.prototype.spiedMethod.restore(); removes the spy. Update 2018-03-22: As pointed out in some of the comments below my answer, stub.reset will do two things: Remove the stub behaviour Remove the stub history … Read more

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

I have been writing Typescript tests using qUnit and Sinon, and I have experienced exactly the same pain you are describing. Let’s assume you have a dependency on an interface like: interface IDependency { a(): void; b(): boolean; } I have managed to avoid the need of additional tools/libraries by using a couple of approaches … Read more