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