sinon stub not replacing function

I’m trying to use sinon stub to replace a function that might take along time. But when I run the tests, the test code doesn’t seem to be using the sinon stubs.

Here is the code I’m trying to test.

function takeTooLong() {
    return  returnSomething();

function returnSomething() {
    return new Promise((resolve) => {
        setTimeout(() => {
        }, 1500)

module.exports = {

and this is the test code.

const chai = require('chai')
const expect = chai.expect
const db = require('./database')
const sinon = require('sinon')

describe('Mock the DB connection', function () {

it('should use stubs for db connection for takeTooLong', function (done) {

    const stubbed = sinon.stub(db, 'returnSomething').returns(new Promise((res) => res('kk')));
    const result = db.takeTooLong()

    result.then((res) => {

    }).catch((err) => done(err))


I get an assertion error

 AssertionError: expected 'ok' to equal 'kk'
      + expected - actual


What am I doing wrong? Why isn’t the stub being used ? The test framework in Mocha.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Sinon stubs the property of the object, not the function itself.

In your case you are exporting that function within an object.

module.exports = {

So in order to properly call the function from the object, you need to replace your function call with the reference to the export object like :

function takeTooLong() {
    return module.exports.returnSomething();

Of course based on your code, you can always refactor it :

var exports = module.exports = {

    takeTooLong: function() { return exports.returnSomething() }

    returnSomething: function() { /* .. */ }


Solution 2

You might want to have a look at Proxyquire to stub/spy directly exported functions.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply