GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Check if a File Exists in Node.js
  • Working with UUID in Node
  • Using bcrypt-js to Hash Passwords in JavaScript
  • Modify Authorized redirect_uris For Google OAuth
  • Sleep in NodeJS
  • Fundamentals
  • Node
  • Vue
  • Webpack
  • Axios
  • Mongoose
  • Express
  • Lodash
  • npm

This view

  • Original page
  • New address

GeezerVison is showing masteringjs.io — Working with the Node.js assert Library

Mastering JS Logo
Mastering JS
Tutorials Newsletter eBooks Jobs
☰
Tutorials Newsletter eBooks Jobs
Tutorials / Node /

Working with the Node.js assert Library

Sep 7, 2022

Node.js' assert.equal(a, b) function will throw an error if a != b. assert.equal(a, b) is equivalent assert(a, b). Asserts are most commonly used for testing, but can also be used as a more concise alternative to an if statement in your code.

const assert = require('assert');

const a = 1;
const b = 2;
assert.equal(a, b); // Throws
assert(a, b); // Throws
assert.equal(a, a); // Succeeds
assert(a, a); // Succeeds

strictEqual()

While equal(a,b) throws an error if a != b, strictEqual(a, b) throws an error if a !== b. Here's more on the difference between !== and != in JavaScript

const assert = require('assert');

const a = 1;
const b = '1';

assert.equal(a, b); // Succeeds
assert.strictEqual(a, b); // Fails

deepEqual() && deepStrictEqual()

These functions do a deep comparison of objects to make sure they have the same keys and values. Only use these functions with POJOs, this will not work with MongoDB ObjectIds.

const assert = require('assert');

const obj = { a: 1, b: 2, c: 3};
const pojo = { a: '1', b: '2', c: '3'};
const entry = { a: 1, b: 2, c: 3 };

assert.deepEqual(obj, pojo); // passes
assert.deepStrictEqual(obj, entry); // passes
assert.deepStrictEqual(obj, pojo); // fails

throws()

Use this function when you want to assert that the function you are testing should throw an error with a specific message. The first parameter is a function and the second parameter is a regular expression that you want the error message to match.

const assert = require('assert');

function run() {
  assert.throws(() => { test(); }, /TypeError: Wrong Value/);
}

function test() {
  throw new TypeError('Wrong Value');
}

run();

rejects()

This functions similarly to throws(), but is used with promises. Use this with async functions.

const assert = require('assert');

async function run() {
 await assert.rejects(async () => { await test(); }, /TypeError: Wrong Value/);
}

async function test() {
  throw new TypeError('Wrong Value')
}

run();

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Node Tutorials

  • Getting Started with Oso Authorization in Node.js
  • Check if a File Exists in Node.js
  • How To Fix "__dirname is not defined" Error in Node.js
  • Working with UUID in Node
  • Using bcrypt-js to Hash Passwords in JavaScript
  • Modify Authorized redirect_uris For Google OAuth
  • Sleep in NodeJS
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.