Modulo di affermazione di Node.js

❮ Moduli integrati


Esempio

Se un'espressione restituisce 0 o false, viene generato un errore e il programma viene terminato:

var assert = require('assert');
assert(5 > 7);

Definizione e utilizzo

Il modulo assert fornisce un modo per testare le espressioni. Se l'espressione restituisce 0 o false, viene causato un errore di asserzione e il programma viene terminato.

Questo modulo è stato creato per essere utilizzato internamente da Node.js.


Sintassi

La sintassi per includere il modulo assert nella tua applicazione:

var assert = require('assert');

Metodi di affermazione

Method Description
assert() Checks if a value is true. Same as assert.ok()
deepEqual() Checks if two values are equal
deepStrictEqual() Checks if two values are equal, using the strict equal operator (===)
doesNotThrow()  
equal() Checks if two values are equal, using the equal operator (==)
fail() Throws an Assertion Error
ifError() Throws a specified error if the specified error evaluates to true
notDeepEqual() Checks if two values are not equal
notDeepStrictEqual() Checks if two values are not equal, using the strict not equal operator (!==)
notEqual() Checks if two values are not equal, using the not equal operator (!=)
notStrictEqual() Checks if two values are not equal, using the strict not equal operator (!==)
ok() Checks if a value is true
strictEqual() Checks if two values are equal, using the strict equal operator (===)
throws()  

❮ Moduli integrati