sinon-auto-restore

2.0.1 • Public • Published

sinon-auto-restore

npm codecov.io JavaScript Style Guide npm David Greenkeeper badge semantic-release

A library that provides a streamlined interface for creating lots of sinon stubs and spies on objects that can be automatically restored with a simple call to restore that can be placed in your global teardown function. Beyond that, it also allows you to replace fields in the same way. Created to be used in a mocha setup but can be configured to work with other test frameworks.

API

onObject(objectName)

Provides an interface to stub and spy on methods of an object and to replace fields.

  • .stub()
    Replaces all own methods of an object by stubs.
  • .stub(n)
    Replaces all own and inherited methods of an object by stubs going up to n levels up the prototype chain.
  • .stub('method1' <,'method2' <...>>)
    Replaces only the given methods by stubs.
  • .stub('method', replacementFunction)
    Replaces a given method by a stub with the provided functionality.
  • .spy()
    Spies on all own methods of an object.
  • .spy(n)
    Spies on all own and inherited methods of an object going up to n levels up the prototype chain.
  • .spy('method1' <,'method2' <...>>)
    Spies on the given methods of an object.
  • .replace('field', replacementValue)
    Replaces a given field by another value.

All of the above methods can be chained. In case different method calls would modify the same field or method, only the last method call is used, i.e.,

onObject(myObject).spy().stub('stubbedMethod')

spies on all own methods of myObject except .stubbedMethod, which is replaced by a stub. This also works across different calls to onObject, i.e.

onObject(myObject).spy()
onObject(myObject).stub('stubbedMethod')

would have exactly the same effect.

restore()

Restores all stubs, spies and replacements.

Dependents (0)

Package Sidebar

Install

npm i sinon-auto-restore

Weekly Downloads

6

Version

2.0.1

License

MIT

Last publish

Collaborators

  • lukastaegert