Chai unittesting - expect(42).to.be.an('integer')

A bit late, but for people coming from search engines, here is another solution:

var expect = require('chai').expect

expect(foo).to.be.a('number')
expect(foo % 1).to.equal(0)

The number check is required because of things such as true % 1 === 0 or null % 1 === 0.


JavaScript doesn't have a separate integer type.

Everything is a IEE 754 floating point number, which would of type number.


This is also possible (at least whithin node):

expect(42).to.satisfy(Number.isInteger);

Here is a more advanced example:

expect({NUM: 1}).to.have.property('NUM').which.is.a('number').above(0).and.satisfy(Number.isInteger);