Get started with should.js CDN

MIT licensed

Should.js: Popular Node.js assertion library. Simple, expressive unit testing with chaining.

Tags:
  • test
  • bdd
  • assert
  • should

Stable version

Copied!

How to start using should.js CDN


// Include Should.js library
const should = require('should');

// Set up some data for testing
const expectedNumber = 42;
const actualNumber = 42;
const expectedString = 'hello world';
const actualString = 'hello world';

// Test number equality
should(actualNumber).be.exactly(expectedNumber).and.not.be.null();

// Test string equality
should(actualString).be.exactly(expectedString).and.not.be.null();

// Test array equality
const expectedArray = [1, 2, 3];
const actualArray = [1, 2, 3];
should(actualArray).deepEqual(expectedArray).and.not.be.null();

// Test object equality
const expectedObject = { name: 'John Doe', age: 30 };
const actualObject = { name: 'John Doe', age: 30 };
should(actualObject).deepEqual(expectedObject).and.not.be.null();

// Test if a value is truthy or falsy
const truthyValue = 10;
const falsyValue = 0;
should(truthyValue).be.truthy();
should(falsyValue).be.falsy();

// Test if a value is an instance of a constructor
const expectedInstance = new Date();
const actualInstance = new Date();
should(actualInstance).instanceOf(Date).and.not.be.null();

// Test if a value is a type (number, string, boolean, etc.)
should(expectedNumber).be.a.Number();
should(expectedString).be.a.String();
should(expectedString).be.a.String().that.is.a.palindrome(); // Custom assertion

// Test if an error is thrown
function divideByZero(num) {
  return num / 0;
}
should(divideByZero).throw(Error, 'Division by zero');
Copied!
Copied!
Copied!

All versions