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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

How are you handling the data revolution? We want your take on what's real, what's hype, and what's next in the world of data engineering.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • WireMock: The Ridiculously Easy Way (For Spring Microservices)
  • How To Create a Stub in 5 Minutes
  • Cypress.io — The Rising Future of Web Automation Testing
  • Consumer-Driven Contract Testing With Spring Cloud Contract

Trending

  • Blockchain in Healthcare: Enhancing Data Security and Interoperability
  • Top Tools for Front-End Developers
  • WebAssembly (Wasm) and AI at the Edge: The New Frontier for Real-Time Applications
  • Ethical AI for Product Owners and Product Managers

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!

By 
Nir Alfasi user avatar
Nir Alfasi
·
Feb. 05, 19 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
14.2K 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.

Related

  • WireMock: The Ridiculously Easy Way (For Spring Microservices)
  • How To Create a Stub in 5 Minutes
  • Cypress.io — The Rising Future of Web Automation Testing
  • Consumer-Driven Contract Testing With Spring Cloud Contract

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: