Assert

Platform Support

The following shows Assert module APIs available for each platform.

LinuxRT-ThreadZephyr
assert.assertOOO
assert.doesNotThrowOOO
assert.equalOOO
assert.failOOO
assert.notEqualOOO
assert.notStrictEqualOOO
assert.strictEqualOOO
assert.throwsOOO

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.

  • actual {any} This property contains the actual value.
  • expected {any} This property contains the expected value.
  • message {any} The error message, default value is the error itself.
  • name {string} The name is AssertionError string.
  • operator {string} This property contains the operator used for comparing actual with expected.

assert(value[, message])

  • value {any} Value to test.
  • message {any} Message displayed in the thrown error.

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

Example

  1. var assert = require('assert');
  2. assert.assert(1);
  3. // OK
  4. assert.assert(true);
  5. // OK
  6. assert.assert(false);
  7. // throws "AssertionError: false == true"
  8. assert.assert(0);
  9. // throws "AssertionError: 0 == true"
  10. assert.assert(false, "it's false");
  11. // throws "AssertionError: it's false"

doesNotThrow(block[, message])

  • block {Function}
  • message {any} Message to be displayed.

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

Example

  1. var assert = require('assert');
  2. assert.doesNotThrow(
  3. function() {
  4. assert.assert(1);
  5. }
  6. );
  7. // OK
  8. assert.doesNotThrow(
  9. function() {
  10. assert.assert(0);
  11. }
  12. )
  13. // throws "AssertionError: Got unwanted exception."

equal(actual, expected[, message])

  • actual {any} The actual value.
  • expected {any} The expected value.
  • message {any} Message to be displayed.

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

Example

  1. var assert = require('assert');
  2. assert.equal(1, 1);
  3. assert.equal(1, '1');

fail(actual, expected, message, operator)

  • actual {any} The actual value.
  • expected {any} The expected value.
  • message {any} Message to be displayed.
  • operator {string} The operator.

Throws an AssertionError exception with the given message.

Example

  1. var assert = require('assert');
  2. assert.fail(1, 2, undefined, '>');
  3. // AssertionError: 1 > 2

notEqual(actual, expected[, message])

  • actual {any} The actual value.
  • expected {any} The expected value.
  • message {any} Message to be displayed.

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

Example

  1. var assert = require('assert');
  2. assert.notEqual(1, 2);

notStrictEqual(actual, expected[, message])

  • actual {any} The actual value.
  • expected {any} The expected value.
  • message {any} Message to be displayed.

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

Example

  1. var assert = require('assert');
  2. assert.notStrictEqual(1, 2);
  3. // OK
  4. assert.notStrictEqual(1, 1);
  5. // AssertionError: 1 !== 1
  6. assert.notStrictEqual(1, '1');
  7. // OK

strictEqual(actual, expected[, message])

  • actual {any} The actual value.
  • expected {any} The expected value.
  • message {any} Message to be displayed.

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

Example

  1. var assert = require('assert');
  2. assert.strictEqual(1, 1);
  3. // OK
  4. assert.strictEqual(1, 2);
  5. // AssertionError: 1 === 2
  6. assert.strictEqual(1, '1');
  7. // AssertionError: 1 === '1'

throws(block[, expected, message])

  • block {Function} The function that throws an error.
  • expected {Function} The expected error type.
  • message {any} Message to be displayed.

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

Example

  1. var assert = require('assert');
  2. assert.throws(
  3. function() {
  4. assert.equal(1, 2);
  5. },
  6. assert.AssertionError
  7. );
  8. // OK
  9. assert.throws(
  10. function() {
  11. assert.equal(1, 1);
  12. },
  13. assert.AssertionError
  14. );
  15. // Uncaught error: Missing exception
  16. assert.throws(
  17. function() {
  18. assert.equal(1, 2);
  19. },
  20. TypeError
  21. );
  22. // AssertionError