Book Image

Node.js Web Development - Third Edition

By : David Herron
Book Image

Node.js Web Development - Third Edition

By: David Herron

Overview of this book

Node.js is a server-side JavaScript platform using an event driven, non-blocking I/O model allowing users to build fast and scalable data-intensive applications running in real time. Node.js Web Development shows JavaScript is not just for browser-side applications. It can be used for server-side web application development, real-time applications, microservices, and much more. This book gives you an excellent starting point, bringing you straight to the heart of developing web applications with Node.js. You will progress from a rudimentary knowledge of JavaScript and server-side development to being able to create and maintain your own Node.js application. With this book you'll learn how to use the HTTP Server and Client objects, data storage with both SQL and MongoDB databases, real-time applications with Socket.IO, mobile-first theming with Bootstrap, microservice deployment with Docker, authenticating against third-party services using OAuth, and much more.
Table of Contents (18 chapters)
Node.js Web Development Third Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Assert – the simplest testing methodology


Node.js has a useful testing tool built-in with the assert module. It's not any kind of testing framework but simply a tool that can be used to write test code by making assertions of things that must be true or false at a given location in the code.

The assert module has several methods providing various ways to assert conditions that must be true (or false) if the code is properly functioning.

The following is an example of using assert for testing, providing an implementation of test-deleteFile.js:

const fs = require('fs');
const assert = require('assert');
const df = require('./deleteFile');
df.deleteFile("no-such-file", (err) => {
    assert.throws(
        function() { if (err) throw err; },
        function(error) {
            if ((error instanceof Error)
             && /does not exist/.test(error)) {
               return true;
            } else return false;
        },
        "unexpected error"
    );
});

If you are looking for...