What is the function of library Sinon?

What is the function of library Sinon?

SinonJS is a JavaScript library that provides standalone test spies, stubs, and mocks. It’s very flexible and easy to use since you can combine it with any testing framework.

How do I create a stub on Sinon?

Create Shared Stubs in beforeEach describe(‘Something’, function() { var save; beforeEach(function() { save = sinon. stub(Database, ‘save’); }); afterEach(function() { save. restore(); }); it(‘should do something’, function() { //you can use the stub in tests by accessing the variable save.

What is stub Sinon?

Test stubs are functions (spies) with pre-programmed behavior. They support the full test spy API in addition to methods which can be used to alter the stub’s behavior. As spies, stubs can be either anonymous, or wrap existing functions.

How do you clear a Sinon stub?

If you just want to reset a specific stub you can use stub. reset() . Another is stub. restore() , which is used to restore the original functionality to the stubbed function.

When would you want to use a stub function in JavaScript?

While spies wrap existing functionality, stubs allow us to replace the functionality completely. The original function won’t run anymore, but rather our new stub that simply returns a result. This is especially handy if we want to test async stuff that makes use of things like jQuery’s AJAX methods.

What are the advantages of Sinon JS framework?

sinon. js provides “[s]tandalone test spies, stubs and mocks for JavaScript.” It has no dependencies and will work with any unit testing framework. In short, they’ve done a lot of the work for you in writing mock servers, timers, events, and providing a framework to override the base behavior of external libraries.

What is Sinon sandbox?

JS. Sandboxes simplify working with fakes that need to be restored and/or verified. If you’re using fake timers, fake XHR, or you are stubbing/spying on globally accessible properties you should use a sandbox to ease cleanup.

How does jest mocking work?

Mocking is a technique to isolate test subjects by replacing dependencies with objects that you can control and inspect. A dependency can be anything your subject depends on, but it is typically a module that the subject imports.

What does Sinon restore do?

Replaces the spy with the original method. Only available if the spy replaced an existing method. The highlighted sentence is the key. The restore functions in sinon only work on spies and stubs that replace an existing method, and our spy was created as an anonymous (standalone) spy.

How to use Sinon to stub a function?

In such cases, you can use Sinon to stub a function. Let’s see it in action. Start by installing a sinon into the project. Once installed you need to require it in the app.js file and write a stub for the lib.js module’s method. Here is how it looks :

Should I use Sinon with Node JS?

For Node environments, we usually recommend solutions targetting link seams or explicit dependency injection. Though in some more basic cases, you can get away with only using Sinon by modifying the module exports of the dependency.

How to create a fake output using Sinon?

Let’s see it in action. Start by installing a sinon into the project. Once installed you need to require it in the app.js file and write a stub for the lib.js module’s method. Here is how it looks : Save the above changes and execute the app.js file. You will get the pre defined fake output in return.

What can you check with Sinon spies?

These are not the only things you can check with spies though — Sinon provides many other assertions you can use to check a variety of different things. The same assertions can also be used with stubs. If you spy on a function, the function’s behavior is not affected. If you want to change how a function behaves, you need a stub.

author

Back to Top