1.4.2 • Published 5 months ago

hein v1.4.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Hein

npm npm npm GitHub Workflow Status (with event)

Assertion library with focus on TypeScript

Features

Plugins

npm/hein-plugin-sinon - Provides assertions like to.have.been.calledOnce() and to.have.been.calledWith() for sinon spies.

Usage

Assert

import { assert } from 'hein';
import { equal } from 'hein/assert';

equal(1, 1);
assert.equal(1, 1);

Expect

import { expect } from 'hein';
expect(1).to.equal(1);

Chainable properties

  • a
  • an
  • and - used to chain multiple assertions, clears the state (so .not will be reset)
  • be
  • have
  • not - inverts the expectation (ie expect(5).to.not.equal(5) will throw)
  • to

Assertions

above

Alias for greaterThan

array

Assert that value is an array

expect([]).to.be.an.array();
atLeast

Alias for greaterThanOrEqual

atMost

Alias for lessThanOrEqual

ballpark

Assert that value is within a range of expectation (default is 0.1 - within 10%)

expect(1.1).to.be.in.ballpark(1);
expect(120).to.be.in.ballpark(100, 0.2);
below

Alias for lessThan

bigint

Assert that value is a BigInt

expect(BigInt(5)).to.be.a.bigint();
boolean

Assert that value is a boolean

expect(true).to.be.a.boolean();
contain

Alias for include

Date

Assert that value is an instance of Date

expect(new Date()).to.be.a.date();
empty

Assert that array / object / Map / Set is empty

expect([]).to.be.empty();
expect({}).to.be.empty();
expect(new Map()).to.be.empty();
expect(new Set()).to.be.empty();
endWith

Assert that string ends with argument

expect('hein').to.endWith('in');
eql

Assert that value deep equals the expectation. When combined with .partially expected may have missing properties

expect({ a: 1 }).to.eql({ a: 1 });
expect({ a: 1, b: new Date() }).to.partially.eql({ a: 1 });

import { any, createEvaluation } from 'hein';
expect({ a: 1, b: new Date(), c: Math.random() }).to.eql({
    a: 1,
    b: any,
    c: createEvaluation((n) => typeof n === 'number')
});
eq

Alias for equal

equal

Assert that value strictly equals expectation. NaN is treated as a non-unique value. Chaining with .deep is an alias for #eql

expect('hein').to.equal('hein');
expect(NaN).to.equal(NaN);
expect({ a: 1 }).to.deep.equal({ a: 1 });
false

Assert that value is false

expect(false).to.be.false();
function

Assert that value is a function

expect(() => {}).to.be.a.function();
greaterThan

Assert that value is greater than expectation

expect(5).to.be.greaterThan(4);
gt

Alias for greaterThan

greaterThanOrEqual

Assert that value is greater than or equal to argument

expect(5).to.be.greaterThanOrEqual(5);
expect(5).to.be.greaterThanOrEqual(4);
gte

Alias for greaterThanOrEqual

include

Assert that element / substring is present in array / string

expect([1, 2, 3]).to.include(2);
expect('hein').to.include('ei');
instanceOf

Assert that value is an instance of provided constructor

expect(new Error()).to.be.an.instanceOf(Error);

keys

Assert that object has keys

expect({ a: 1, b: 2 }).to.have.keys(['a', 'b']);
expect({ a: 1, b: 2 }).to.have.keys('a');
lengthOf

Assert that value has a length equal to argument

expect([1]).to.have.a.lengthOf(1);
expect({ a: 1 }).to.have.a.lengthOf(1);
expect(new Map([['a', 1]])).to.have.a.lengthOf(1);
expect(new Set([1])).to.have.a.lengthOf(1);
expect('hein').to.have.a.lengthOf(4);
isAfter

Assert that date is before argument

expect(new Date(2020, 1, 2)).to.be.after(new Date(2020, 1, 1));
isBefore

Assert that date is before argument

expect(new Date(2020, 1, 1)).to.be.before(new Date(2020, 1, 2));
isBetween

Assert that date or number is between arguments

expect(new Date(2020, 1, 2)).to.be.between(new Date(2020, 1, 1), new Date(2020, 1, 3));
expect(2).to.be.between(1, 3);
lessThan

Assert that value is less than expectation

expect(5).to.be.lessThan(6);
lt

Alias for lessThan

lessThanOrEqual

Assert that value is less than or equal to expectation

expect(5).to.be.lessThanOrEqual(5);
expect(4).to.be.lessThanOrEqual(5);
lte

Alias for lessThanOrEqual

Map

Assert that value is an instance of Map

expect(new Map()).to.be.a.Map();
members

Assert that array contains members of expectation

expect([1, 2, 3]).to.have.members([1, 2]);
// check that the arrays have same length
expect([1, 2, 3]).to.have.same.members([1, 2, 3]);
// check that the members are found in the same order
expect([1, 2, 3]).to.have.ordered.members([1, 2, 3]);
// check for deep equality of members
expect([{ a: 1 }, { b: 2 }]).to.have.deep.members([{ a: 1 }]);
// check for members by partial equality
expect([{ a: 1 }, { b: 2 }]).to.have.partially.members([{ a: 1 }]);
NaN

Assert that value is NaN

expect(NaN).to.be.NaN();
null

Assert that value is null

expect(null).to.be.null();
number

Assert that value is a number

expect(5).to.be.a.number();
object

Assert that value is an object. Null and instances of Array don't count

expect({}).to.be.an.object();
property

Assert that actual has property. Optionally check that propery has value

expect({ a: 1 }).to.have.property('a');
expect({ a: 1 }).to.have.property('a', 1);
reject

Assert that provided Promise rejects. At the moment this is the only assertion that can't be chained, returns a promise

await expect(Promise.reject()).to.reject();
roundTo

Assert that number can be rounded to target

expect(5.5).to.be.roundTo(6);
expect(5.14).to.roundTo(5.1, 1);
expect(110).to.roundTo(100, -2);
Set

Assert that value is an instance of Set

expect(new Set()).to.be.a.Set();
sizeOf

Alias of lengthOf

startWith

Assert that string starts with argument

expect('hein').to.startWith('he');
string

Assert that value is a string

expect('hein').to.be.a.string();
symbol

Assert that value is a symbol

expect(Symbol()).to.be.a.symbol();
throw

Assert that provided function throws

expect(() => {
    throw new Error();
}).to.throw();
true

Assert that value is true

expect(true).to.be.true;
undefined

Assert that value is undefined

expect(undefined).to.be.undefined();
WeakMap

Assert that value is an instance of WeakMap

expect(new WeakMap()).to.be.a.WeakMap();
WeakSet

Assert that value is an instance of WeakSet

expect(new WeakSet()).to.be.a.WeakSet();

Modifiers

excluding

Exclude properties from assertion

expect({ a: 1, b: 2 }).excluding('a').to.eql({ b: 2 });
1.4.2

5 months ago

1.3.6

7 months ago

1.4.1

6 months ago

1.4.0

6 months ago

1.2.0

10 months ago

1.1.1

12 months ago

1.3.5

9 months ago

1.3.4

10 months ago

1.3.3

10 months ago

1.3.2

10 months ago

1.2.3

10 months ago

1.3.1

10 months ago

1.2.2

10 months ago

1.3.0

10 months ago

1.2.1

10 months ago

1.1.2

10 months ago

1.1.0

1 year ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago