ShadowNode

Use Node.js in your end devices(QQ: 796448809)

View the Project on GitHub yodaos-project/ShadowNode

Platform Support

The following shows Assert module APIs available for each platform.

  Linux
(Ubuntu)
Raspbian
(Raspberry Pi)
NuttX
(STM32F4-Discovery)
TizenRT
(Artik053)
assert.ok O O O O
assert.doesNotThrow O O O O
assert.equal O O O O
assert.fail O O O O
assert.notEqual O O O O
assert.notStrictEqual O O O O
assert.strictEqual O O O O
assert.throws O O O O

Assert

Assert module is designed for writing tests for applications.

You can access the functions of the module by adding require('assert') to your file.

Class: AssertionError

Assert module will produce AssertionError in case of an assertion failure. AssertionError inherits standard Error thus it has properties provided by Error object including additional properties.

assert(value[, message])

An alias of assert.ok().

assert.ok(value[, message])

Checks if the value is truthy. If it is not, throws an AssertionError, with the given optional message.

Example

var assert = require('assert');

assert.ok(1);
// OK

assert.ok(true);
// OK

assert.ok(false);
// throws "AssertionError: false == true"

assert.ok(0);
// throws "AssertionError: 0 == true"

assert.ok(false, "it's false");
// throws "AssertionError: it's false"

assert.doesNotThrow(block[, message])

Tests if the given block does not throw any exception. Otherwise throws an exception with the given optional message.

Example

var assert = require('assert');

assert.doesNotThrow(
  function() {
    assert.ok(1);
  }
);
// OK

assert.doesNotThrow(
  function() {
    assert.ok(0);
  }
)
// throws "AssertionError: Got unwanted exception."

assert.equal(actual, expected[, message])

Tests if actual == expected is evaluated to true. Otherwise throws an exception with the given optional message.

Example

var assert = require('assert');

assert.equal(1, 1);
assert.equal(1, '1');

assert.fail(actual, expected, message, operator)

Throws an AssertionError exception with the given message.

Example

var assert = require('assert');

assert.fail(1, 2, undefined, '>');
// AssertionError: 1 > 2

assert.notEqual(actual, expected[, message])

Tests if actual != expected is evaluated to true. Otherwise throws an exception with the given optional message.

Example

var assert = require('assert');

assert.notEqual(1, 2);

assert.notStrictEqual(actual, expected[, message])

Tests if actual !== expected is evaluated to true. Otherwise throws an exception with the given optional message.

Example

var assert = require('assert');

assert.notStrictEqual(1, 2);
// OK

assert.notStrictEqual(1, 1);
// AssertionError: 1 !== 1

assert.notStrictEqual(1, '1');
// OK

assert.strictEqual(actual, expected[, message])

Tests if actual === expected is evaluated to true. Otherwise throws an exception with the given optional message.

Example

var assert = require('assert');

assert.strictEqual(1, 1);
// OK

assert.strictEqual(1, 2);
// AssertionError: 1 === 2

assert.strictEqual(1, '1');
// AssertionError: 1 === '1'

assert.deepStrictEqual(actual, expected, message)

Example

var assert = require('assert');

var obj1 = { a: 1, b: 2 };
var obj2 = { a: 1, b: 2 };
var obj3 = { a: 1, b: 3 };

assert.deepStrictEqual(obj1, obj2);
// OK

assert.deepStrictEqual(obj1, obj3);
// AssertionError

assert.deepStrictEqual(NaN, NaN);
// OK

assert.throws(block[, expected, message])

Tests if the given block throws an expected error. Otherwise throws an exception with the given optional message.

Example

var assert = require('assert');

assert.throws(
  function() {
    assert.equal(1, 2);
  },
  assert.AssertionError
);
// OK

assert.throws(() => {
  throw new Error('foobar');
}, /foobar/);
// OK

assert.throws(() => {
  var e = new Error('foobar');
  e.code = 'ENO';
  throw e;
}, {
  message: 'foobar',
  code: 'ENO',
});
// OK

assert.throws(() => {
  throw new Error('foobar');
}, err => err.message === 'foobar');
// OK

assert.throws(
  function() {
    assert.equal(1, 1);
  },
  assert.AssertionError
);
// Uncaught error: Missing exception

assert.throws(
  function() {
    assert.equal(1, 2);
  },
  TypeError
);
// AssertionError