Talespin_Kit
Talespin_Kit

Reputation: 21877

How to use nodejs Assert

How to use the Assert in nodejs http://nodejs.org/api/assert.html

Tried

Assert.assert(1===1);

It throws following error

ReferenceError: Assert is not defined

How to use "assert". I am new to javascript and nodejs. The nodejs documentation explictly mentions some of the sections as a class Example:- http://nodejs.org/api/all.html#all_class_buffer_1. But Assert is not mentioned as a class. Then what is "Assert"?

Upvotes: 12

Views: 18467

Answers (3)

Rahul_Dabhi
Rahul_Dabhi

Reputation: 730

This module is used for writing unit tests for your applications, you can access it with require('assert').

var assert = require('assert');

assert.throws(
  function() {
    throw new Error("Wrong value");
  },
  function(err) {
    if ( (err instanceof Error) && /value/.test(err) ) {
      return true;
    }
  },
  "unexpected error"
);

Upvotes: 14

user4890006
user4890006

Reputation:

assert.throws(
  function() {
    throw new Error("Wrong value");
  },
  function(err) {
    if ( (err instanceof Error) && /value/.test(err) ) {
      return true;
    }
  },
  "unexpected error"
);

Upvotes: 1

rishiag
rishiag

Reputation: 2274

Although assert is already included in nodejs installation. You still need to require it.

var assert = require('assert')

And then use as follows:

assert.equal(1,1);

Upvotes: 7

Related Questions