SinonJS Fundamentals

SinonJS is one of the most popular JavaScript libraries for test doubles. This course walks through the various features of SinonJS so that you know not only how to use each feature, but more importantly, why you would use each one.
Course info
Rating
(10)
Level
Intermediate
Updated
Jul 2, 2018
Duration
2h 22m
Table of contents
Course Overview
The State of Testing at Globomantics
Spying on Functions
Swapping a Stub for a Function
Verifying Behavior with Mocks
Controlling How Parameters Are Matched
Faking out XHR and Timer Calls
Easier Set up and Clean up with Sandboxes
What’s Next?
Description
Course info
Rating
(10)
Level
Intermediate
Updated
Jul 2, 2018
Duration
2h 22m
Description

Unit testing can be hard, particularly when you're testing against live systems. Data from databases can change, APIs can be down, and accessing file systems can be slow. Using test doubles in your unit tests can help alleviate a lot of the struggles and issues you might experience. In this course, SinonJS Fundamentals, you'll learn how the SinonJS library can help make unit testing easier and more repeatable. First, you'll explore each of the test doubles that Sinon offers. Not only will you learn the syntax, but with the hands-on demos, you'll see how and when to use each test double. Next, you'll delve into other utilities that Sinon offers that simplify checking that the right data was returned. Then, you'll discover how Sinon can help you even with the hard problems of testing, such as faking out dates or XHR data in the browser. Finally, you'll learn how the Sinon ecosystem works to make it easier for you to stay on top of all of your test doubles. By the end of the course, you'll be able to incorporate Sinon into your projects to ensure quick, repeatable test results.

About the author
About the author

Nate's first program was written in QBasic on an 8086 clone his dad built. Since then he's written applications in C++, .NET, and Node.js.

More from the author
Functional Programming: The Big Picture
Beginner
1h 12m
Nov 21, 2018
Postman Fundamentals
Beginner
2h 40m
Dec 12, 2017
More courses by Nate Taylor
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
Hi everyone. My name is Nate Taylor, and welcome to my course, SinonJS Fundamentals. I am a lead software developer at Aviture in Omaha, Nebraska. Writing tests can be hard, but by figuring out how to fake out specific functions, you can make your testing a little bit easier. This course will walk you through the basics of Sinon. JS, a popular test double library in JavaScript. It will help you get up and running so that you can start using it in your projects. Some of the major topics that we'll cover include spies, stubs, mocks, and other fake functions that you can use with Sinon. By the end of the course, you'll be able to install Sinon and use it to fake out functions to help your unit tests be isolated. You'll also have a good understanding of the Sinon. JS idioms, and even get exposed to some of the available libraries that you can use. Before beginning the course, you should be familiar with unit testing, as well as JavaScript. I hope you'll join me on this journey to learn test doubles with the SinonJS Fundamentals course, at Pluralsight.

Spying on Functions
You've got the prereqs installed, you've had a chance to get a quick glance at the code, and you've even run the tests and you've seen how they're failing when you're not connected to a database. At this point, I'm sure you're ready to get to work and begin figuring out how Sinon can help both you and Globomantics out. The first part of Sinon to examine is a spy.

Swapping a Stub for a Function
You've already started cleaning up some of Globomantics' broken tests using spies. Unfortunately, you were only able to clean up certain tests. For example, tests that wanted to verify a specific function was called. In order to get more tests fixed, you need to control what data is returned from the database. That's just part of what you'll learn as you start diving into stubs in this module.

Verifying Behavior with Mocks
Now that you've learned about spies and stubs, you've covered two- thirds of the test doubles provided by Sinon. JS. Sometimes you have a case where you want a test double that's partly a spy and partly a stub. In those cases, as you'll learn in this module, a mock is likely the right test double for you.

Controlling How Parameters Are Matched
You've now learned the three types of test doubles in Sinon. JS, spies, stubs, and mocks, and with those three doubles, you'll be able to fake out any object in the Globomantics code base, but Sinon is more than just three test doubles. It also provides you with some helpful utilities. The first one to look at will assist you as you tell Sinon exactly how to check the result object of a spy, or when to execute a specific stub. In Sinon lingo, these are called matchers, and you'll learn more about them in this module.

Faking out XHR and Timer Calls
At this point, you've learned about spies, stubs, and mocks. Additionally, you've learned about various ways you can match arguments that are passed into or returned from Sinon's test doubles. In doing all of that, you've been able to make Globomantics' code base better, but there are two problems in testing that tend to be difficult and painful to address. The first is testing front-end calls that are made to a back end. The second problem is interacting with time. If your code relies on new date for example, how can you write a test to verify that the correct date is used? You'll learn about both of these utilities in this module as you continue to help Globomantics' software get better.

Easier Set up and Clean up with Sandboxes
At this point in the course, you've been a huge help to Globomantics. You've introduced spies, stubs, and mocks into their code base. Thanks in no small part to your efforts, they're well on the path to having a robust test suite. Before you wrap up though, there's one more Sinon feature to learn about, and that is how to set up and tear down your test suite with less effort.

What’s Next?
At this point, you've learned the major features of Sinon. JS. You've also helped clean up Globomantics' code base. You are ready to go out into the real world and start using Sinon on all of your projects. Before heading out though, take a quick minute to review the various Sinon features that you've learned. After that, you'll learn about some of the next steps that you can take to get started with Sinon. JS.