Skip to content

Add sinon.restore(obj) to restore all stubbed methods on the object #1553

@tsclaus

Description

@tsclaus

Without using sandboxes, there is no quick reversal to sinon.stub(obj). sinon.restore(obj) and/or sinon.stub.restore(obj) could be added to serve as the inverse to sinon.stub(obj). With the walk utility function, this should be pretty easy; with approval, I can add the feature.

An example of its usefulness (using Karma):

const sinon = require('sinon');

const testedLibrary = require('testedLibrary'),
      dependency    = require('dependency');

describe('testedLibrary', function() {
  beforeEach(function() {
    sinon.stub(dependency);
  });

  it('should test something', function() {
    // changing dependency.method behavior here automatically changes it in `testedLibrary.js` as well
    // since the same object is used in both files
  });

  afterEach(function() {
    // without this you either have to clear the cache or iterate over dependency yourself
    sinon.restore(dependency);
  });
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions