What is core-assert?
The core-assert npm package provides a set of assertion functions for verifying invariants in your code. It is commonly used in testing to ensure that code behaves as expected.
What are core-assert's main functionalities?
assert.ok
The assert.ok function tests if a value is truthy. If the value is not truthy, it throws an AssertionError.
const assert = require('core-assert');
assert.ok(true); // No error
assert.ok(false); // Throws an AssertionError
assert.equal
The assert.equal function tests shallow, coercive equality between two values using the == operator. If the values are not equal, it throws an AssertionError.
const assert = require('core-assert');
assert.equal(1, 1); // No error
assert.equal(1, 2); // Throws an AssertionError
assert.deepEqual
The assert.deepEqual function tests for deep equality between two objects. If the objects are not deeply equal, it throws an AssertionError.
const assert = require('core-assert');
assert.deepEqual({a: 1}, {a: 1}); // No error
assert.deepEqual({a: 1}, {a: 2}); // Throws an AssertionError
assert.throws
The assert.throws function tests if a function throws an error. If the function does not throw an error, it throws an AssertionError.
const assert = require('core-assert');
assert.throws(() => { throw new Error('error'); }, Error); // No error
assert.throws(() => { }, Error); // Throws an AssertionError
Other packages similar to core-assert
chai
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework. It provides a more expressive and readable syntax compared to core-assert.
jest
Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It provides built-in assertion functions and is often used for testing React applications. It offers more features and integrations compared to core-assert.
should
Should is an expressive, readable, framework-agnostic assertion library. It extends Object.prototype to provide a more natural language style for assertions, making tests more readable compared to core-assert.
core-assert
Node.js assert
as a standalone module
Useful to ensure consistency between Node.js versions as the assert
module has changed a lot.
Lets you use the Node.js 4.0 assert.deepStrictEqual()
/assert.notDeepStrictEqual()
methods all the way back to Node.js 0.10.
Issues and improvements should be done in Node.js first.
Install
$ npm install --save core-assert
Usage
var assert = require('core-assert');
assert.strictEqual('unicorn', 'unicorn');
Related
- deep-strict-equal - Test for deep equality - Node.js
assert.deepStrictEqual()
algorithm as a standalone module
License
MIT © Sindre Sorhus