assertBy is a assertion library for validating data and conditions in tests. It provides various methods to check data types, values, and structures, suitable for various testing scenarios.
- Simple and Intuitive API: Makes assertion statements more readable.
- Rich Assertion Methods: Offers various data checking methods, including equality checks, range checks, type checks, and more.
- Supports Chaining: Allows multiple assertions to be chained together to build complex validation logic.
- Works with Various Data Types: Supports checking numbers, strings, arrays, objects, functions, and more.
Install using npm:
npm install assertBy
assertBy
provides a simple and intuitive API to help you write test assertions easily.
Here are some common usage examples:
import { by } from 'assertBy'
let num = 5
by(num).to.equal(5).and.is.number
by(num).not.equal(10).and.is.not.bool
by(num).to.above(3).and.is.not.function
by(num).to.below(10)
by(num).to.belowOrEq(6)
by(num).is.above(3)
by(num).is.aboveOrEq(4)
// shorter
by(num).is.ab(4).and.is.bl(6).and.is.eq(5)
by(num).is.withIn(3, 6)
by(num).is.not.withIn(6, 10)
by(5.005).is.approximately(5, 0.01)
by(5.005).is.not.approximately(5, 0.001)
let arr = [ 1, 2, 3 ]
by( arr ).to.length( 3 )
let obj = { a: 1, b: 2 }
by( obj ).to.deepEq( { a: 1, b: 2 } ).and.is.object
by( obj ).has.property( 'a' )
by( obj ).has.property( 'a', 1 ).and.has.property( 'b', 2 )
by( obj ).has.include( { a: 1 } )
by( obj ).has.key( 'a' )
by( obj ).has.keys( [ 'a', 'b' ] )
by( obj ).not.has.property( 'c' )
by( obj ).not.has.include( { c: 3 } )
by( obj ).not.has.key( 'c' )
by( obj ).not.has.keys( [ 'c', 'd' ] )
let obj = { a: 1, b: 2, c: { c1:10, c2:20, c3:{ d:'aa', e:'bb' } } }
by( obj ).has.deep.include( { b:2 } )
by( obj ).has.deep.include( { c:{ c2:20 } } )
by( obj ).has.deep.include( { c:{ c3:{ e:'bb' } } } )
by(()=>
{
by( obj ).not.has.deep.include( { c:{ c4:'' } })
})
.to.throw( 'to not include key c' )
by( obj ).not.has.deep.include( { z:'' })
by(() => {}).not.throw()
const fnEx = () => { throw new Error(`i'm error message`) }
by(fnEx).to.throw(Error)
by(fnEx).to.throw(`i'm error message`)
by(fnEx).to.throw(Error, `i'm error message`)
by(() => {
let num = 123
by(num).to.eq(456)
}).is.function.and.is.throw(Error)
by(() => {
by(123).to.eq(456, 'Custom-Message')
}).to.throw('Custom-Message')
by(() => {
throw new Error('get the custom message')
}).to.throw('get the custom message')
Initialize an assertion chain. value
is the value to be checked.
-
by.fail( 'message' )
: Make a Error with message
Further specify assertion conditions.
-
.eq(expected)
: Assertvalue
is equal toexpected
-
.equal(expected)
: Same as.eq(expected)
-
.above(expected)
: Assertvalue
is greater thanexpected
-
.ab(expected)
: Same as.above(expected)
-
.aboveOrEq(expected)
: Assertvalue
is greater than or equal toexpected
-
.abe(expected)
: Same as.aboveOrEq(expected)
-
.below(expected)
: Assertvalue
is less thanexpected
-
.bl(expected)
: Same as.below(expected)
-
.belowOrEq(expected)
: Assertvalue
is less than or equal toexpected
-
.ble(expected)
: Same as.belowOrEq(expected)
-
.withIn(start, end)
: Assertvalue
is betweenstart
andend
-
.approximately(expected, delta)
: Assertvalue
is approximatelyexpected
, allowing fordelta
-
.deepEq(expected)
: Assertvalue
is deeply equal toexpected
-
.throw([type], [message])
: Assert functionvalue
throws an error of the specified type or message
Check the type or state of value
.
-
.number
: Assertvalue
is a number -
.num
: Same as.number
-
.bool
: Assertvalue
is a boolean -
.function
: Assertvalue
is a function -
.func
: Same as.function
-
.string
: Assertvalue
is a string -
.str
: Same as.string
-
.object
: Assertvalue
is an object -
.obj
: Same as.object
-
.array
: Assertvalue
is an array -
.symbol
: Assertvalue
is a symbol -
.null
: Assertvalue
is null -
.undefined
: Assertvalue
is undefined -
.nullOrUndef
: Assertvalue
is null or undefined -
.nan
: Assertvalue
is NaN -
.true
: Assertvalue
is true -
.false
: Assertvalue
is false -
.instanceOf(constructor)
: Assertvalue
is an instance of the specified constructor
Check properties and keys of an object or array.
-
.property(name, [value])
: Assert the objectvalue
has a property with the specified name, optionally equal tovalue
-
.include(expected)
: Assert the object or arrayvalue
includesexpected
-
.key(name)
: Assert the objectvalue
has a key with the specified name -
.keys(...names)
: Assert the objectvalue
has a set of keys with the specified names -
.deep.include(expected)
: Assert deep inclusion ofexpected
invalue
Allows chaining of multiple assertions.
-
.and
: Chain multiple assertions
Negate the assertion conditions.
-
.eq(expected)
: Assertvalue
is not equal toexpected
-
.equal(expected)
: Same as.eq(expected)
-
.above(expected)
: Assertvalue
is not greater thanexpected
-
.ab(expected)
: Same as.above(expected)
-
.aboveOrEq(expected)
: Assertvalue
is not greater than or equal toexpected
-
.abe(expected)
: Same as.aboveOrEq(expected)
-
.below(expected)
: Assertvalue
is not less thanexpected
-
.bl(expected)
: Same as.below(expected)
-
.belowOrEq(expected)
: Assertvalue
is not less than or equal toexpected
-
.ble(expected)
: Same as.belowOrEq(expected)
-
.within(start, end)
: Assertvalue
is not betweenstart
andend
-
.withIn(start, end)
: Same as.within(start, end)
-
.approximately(expected, delta)
: Assertvalue
is not approximatelyexpected
-
.deepEq(expected)
: Assertvalue
is not deeply equal toexpected
-
.throw([type], [message])
: Assert functionvalue
does not throw an error of the specified type or message