DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Using Sinon Stubs

Learn how to test your JavaScript code by spies, called stubs, using the Sinon.js library. It's quick and easy, and will make your life easier!

Nir Alfasi user avatar by
Nir Alfasi
·
Feb. 05, 19 · Tutorial
Like (3)
Save
Tweet
Share
13.35K Views

Join the DZone community and get the full member experience.

Join For Free

A few days ago, I wanted to test a JS function. Part of the test was to verify that another function gets called, and gets called correctly (with the correct parameters). I ended up using Sinon for the task, and now, looking back at it, it’s pretty easy and straightforward. The thing is, when I was trying to write my tests I didn’t find good examples and there was a little struggle until I figured it out, which is the motivation for this post (why not share and make someone’s else life easier, right?). 

So let’s go down to business!

I wanted to leverage Sinon to stub sendBigDealEventin order to test that it was called properly:

const events = require('events');

function pleaseTestMe(a, b, c) {  
  ...  
  events.sendBigDealEvent(a, c, x); // test that it was called!  
  ...  
  return something;
}

Which I was able to achieve by:

describe('Testing pleaseTestMe()', () => {  
  let a = 1, b = 2, ...
  ...   
  it('Test that sendBigDealEvent() was called correctly', () => {    
    const sendBigDealEvent = sinon.stub(events, 'sendBigDealEvent');    
    pleaseTestMe(a, b, c);    
    sinon.assert.calledOnce(sendBigDealEvent);    
    sinon.assert.calledWith(sendBigDealEvent, sinon.match(a, c, sinon.match.any));    
    sendBigDealEvent.restore();  
  });
  ...
});

As we can see, Sinon makes it easy to stub a module’s function using the syntax:

const sendBigDealEvent = sinon.stub(events, 'sendBigDealEvent');

Pay attention to the fact that the name of the function is passed as a string as the second argument to stub.

Then, Sinon allows us to check how many times the function was called with cute utility methods such as: calledOnce, calledTwice, calledTriceand callCount.

Further, we can check that sendBigDealEventwas called with specific arguments by using a combination of sinon.assert.calledWithand Sinon matchers:

sinon.assert.calledWith(sendBigDealEvent, sinon.match(a, c, sinon.match.any));

You can read more about assertions and matchers here.

And las.t but not less: when you’re done with the stub you should release it:

sendBigDealEvent.restore();

The reason for that is that if you’ll try to use Sinon to stub that same method in another test and if you forget to restore()you’ll run into an exception saying that you’re trying to wrap an object which is already wrapped. Something like:

Attempted to wrap someObj which is already wrapped

Nice and easy, right?

Stub (distributed computing)

Published at DZone with permission of Nir Alfasi. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Create a Real-Time Scalable Streaming App Using Apache NiFi, Apache Pulsar, and Apache Flink SQL
  • Better Performance and Security by Monitoring Logs, Metrics, and More
  • Kubernetes vs Docker: Differences Explained
  • How To Check Docker Images for Vulnerabilities

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: