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

Get Node.js Web Development - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.