O'Reilly logo

Node.js Web Development - Third Edition by David Herron

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required