Book Image

Learning Node.js Development

By : Andrew Mead
Book Image

Learning Node.js Development

By: Andrew Mead

Overview of this book

Learning Node.js Development is a practical, project-based book that provides you with all you need to get started as a Node.js developer. Node is a ubiquitous technology on the modern web, and an essential part of any web developers' toolkit. If you are looking to create real-world Node applications, or you want to switch careers or launch a side project to generate some extra income, then you're in the right place. This book has been written around a single goal—turning you into a professional Node developer capable of developing, testing, and deploying real-world production applications. Learning Node.js Development is built from the ground up around the latest version of Node.js (version 9.x.x). You'll be learning all the cutting-edge features available only in the latest software versions. This book cuts through the mass of information available around Node and delivers the essential skills that you need to become a Node developer. It takes you through creating complete apps and understanding how to build, deploy, and test your own Node apps. It maps out everything in a comprehensive, easy-to-follow package designed to get you up and running quickly.
Table of Contents (13 chapters)

Using assertion libraries in testing Node modules

In the previous sections, we made two test cases to verify that utils.add and our utils.square method work as expected. We did that using an if condition, that is, if the value was not 44 that means something went wrong and we threw an error. In this section, we'll learn how to use an assertion library, which will take care of all of the if condition in utils.test.js code for us:

if (res !== 44)
throw new Error(`Expected 44, but got ${res}.`)

Because when we add more and more tests, the code will end up looking pretty similar and there's no reason to keep rewriting it. Assertion libraries let us make assertions about values, whether it's about their type, the value itself, whether an array contains an element, all sorts of things like that. They really are fantastic.

The one we'll be using is called expect...