Book Image

Node Web Development - Second Edition

By : David Herron
Book Image

Node Web Development - Second Edition

By: David Herron

Overview of this book

Table of Contents (17 chapters)
Node Web Development Second Edition
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

Assert – the simplest testing methodology


Node has a useful testing tool built in 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 which must be true or false at a given location.

The assert module has several methods which provide various ways of asserting conditions that must be true if the code is properly functioning.

The following is an example of using assert for testing, which you should put in the file test-deleteFile.js:

var fs = require('fs');
var assert = require('assert');
var df = require('./deleteFile');
df.deleteFile("no-such-file", function(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 were looking for a quick and dirty way...