chai throw error code example

Example 1: mocha should throw error

describe('createMap', () => {
    it('should throw an error if no contents were sent as argument', () => {
      expect(() => createMap()).to.throw('You have to send a list of things to map.');
    });
  });

Example 2: chai test throw error

expect(model.get.bind(model, 'z')).to.throw('Property does not exist in model schema.');
expect(model.get.bind(model, 'z')).to.throw(new Error('Property does not exist in model schema.'));

Example 3: chai expect to be type array

expect([1, 2]).to.be.an('array');

Example 4: chai length greater than

expect('foo').to.have.lengthOf(3); // Recommended
expect('foo').to.have.lengthOf.above(2); // Not recommended

expect([1, 2, 3]).to.have.lengthOf(3); // Recommended
expect([1, 2, 3]).to.have.lengthOf.above(2); // Not recommended

Tags:

Misc Example