hein

    hein

    Hein

    npm npm npm GitHub Workflow Status (with event)

    Assertion library with focus on TypeScript

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

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

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

    There are multiple side-effect free tokens to chain assertions together. These include:

    • to
    • be
    • a
    • an
    • have
    • in
    • of
    expect(5).to.be.a.number(); // same as expect(5).number();
    

    Not inverts the conditions inside the expectation.

    expect(5).to.not.equal(4);
    

    Run the assertion for each member of an array

    expect([1, 2, 3]).every.to.be.a.number();
    

    Run the assertion on the length of an array

    expect([1, 2, 3]).length.to.be.greaterThan(2);
    

    Clears most of the state in the chain (with the exception of the effects of .every);

    expect(1).to.not.be.greaterThan(5).and.be.greaterThan(0);
    

    Alias for greaterThan

    Assert that value is an array

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

    Alias for greaterThanOrEqual

    Alias for lessThanOrEqual

    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);

    Alias for lessThan

    Assert that value is a BigInt

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

    Assert that value is a boolean

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

    Alias for include

    Assert that value is an instance of Date

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

    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();

    Assert that string ends with argument

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

    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')
    });

    Alias for 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 });

    Assert that value is false

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

    Assert that value is a function

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

    Assert that value is greater than expectation

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

    Alias for greaterThan

    Assert that value is greater than or equal to argument

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

    Alias for greaterThanOrEqual

    Assert that element / substring is present in array / string

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

    Assert that value is an instance of provided constructor

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

    Assert that object has keys

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

    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);

    Assert that date is before argument

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

    Assert that date is before argument

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

    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);

    Assert that value is less than expectation

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

    Alias for lessThan

    Assert that value is less than or equal to expectation

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

    Alias for lessThanOrEqual

    Assert that value is an instance of Map

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

    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 }]);

    Assert that value is NaN

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

    Assert that value is null

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

    Assert that value is a number

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

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

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

    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);

    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();
    

    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);

    Assert that value is an instance of Set

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

    Alias of lengthOf

    Assert that string starts with argument

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

    Assert that value is a string

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

    Assert that value is a symbol

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

    Assert that provided function throws

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

    Assert that value is true

    expect(true).to.be.true;
    

    Assert that value is undefined

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

    Assert that value is an instance of WeakMap

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

    Assert that value is an instance of WeakSet

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

    Exclude properties from assertion

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