NodeJS has built-in assertion funtions . Basic
const assert = require('assert')
describe('assert', function () {
it.only('should pass', function () {
assert(1 > 0)
})
})
test
✔ assert
1 passing (1ms)
Methods equal , ok , assert , notEqual checks if a value is true with (==) strictEqual , notStrictEqual checks if equal with (===) deepStrictEqual , deepEqual , notDeepStrictEqual checks if equal with (===) match , doesNotMatch expects to match the regular expression ifError test if value is null or undefined throws , doesNotThrow , rejects , doesNotReject , fail NOT clear what they do, check later ok() Same as assert.equal() and assert()
const assert = require('assert')
describe('test', function () {
it.only('assert', function () {
assert.ok() // fail
assert.ok(true)
assert.ok(false) // fail
assert.ok(false, 'its false') // fail
assert.ok(0) // fail
assert.ok(1)
assert.ok(typeof 123 === 'number')
assert.ok(typeof 123 === 'string') // fail
})
})
assert() Same as previous
it('assert', function () {
assert() // fail
assert(true)
assert(false) // fail
assert(false, 'its false') // fail
assert(0) // fail
assert(1)
assert(typeof 123 === 'number')
assert(typeof 123 === 'string') // fail
})
strictEqual
const assert = require('assert')
describe.only('node assert', function () {
it('strictEqual', function () {
assert.strictEqual(1, 2) // fail
assert.strictEqual(1, 1)
assert.strictEqual('Hello foobar', 'Hello World!') // fail
const apples = 1
const oranges = 2
assert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`) // fail
assert.strictEqual(1, '1', new TypeError('Inputs are not identical')) // fail
})
})
deepStrictEqual
it('deepStrictEqual', function () {
assert.deepStrictEqual({ a: 1 }, { a: '1' }) // false
const date = new Date()
const object = {}
const fakeDate = {}
Object.setPrototypeOf(fakeDate, Date.prototype)
assert.deepStrictEqual(object, fakeDate) // false
assert.deepStrictEqual(date, fakeDate) // false
assert.deepStrictEqual(NaN, NaN)
assert.deepStrictEqual(new Number(1), new Number(1))
assert.deepStrictEqual(new String('foo'), Object('foo'))
assert.deepStrictEqual(-0, -0)
assert.deepStrictEqual(0, -0) // false
const symbol1 = Symbol()
const symbol2 = Symbol()
assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 })
assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 }) // false
})
match
it('match', function () {
assert.match('I will fail', /w/)
assert.match(123, /pass/) // fail
assert.match('I will pass', /pass/)
})
fail
it('ifError', function () {
assert.ifError(null)
assert.ifError(0) // fail
assert.ifError('error') // fail
assert.ifError(new Error()) // fail
})