36

According to http://chaijs.com/api/bdd/#a, a/an can be used to check for the type of a variable.

.a(type)

@param{ String } type

@param{ String } message _optional_

The a and an assertions are aliases that can be used either as language chains or to assert a value's type.

However, I'm not able to check for the variable beeing an integer. The given examples, e.g. expect('1337').to.be.a('string'); work for me, but the following does not:

expect(42).to.be.an('integer');
expect(42).to.be.an('Integer');
expect(42).to.be.an('int');
expect(42).to.be.an('Int');

All of them are giving me the following error when running mocha:

Uncaught AssertionError: expected 42 to be an integer

How do I test with chai for a variable beeing an integer?

soerface
  • 6,417
  • 6
  • 29
  • 50
  • I tried the docs and didn't find any comment in them about types, so I'll take an educated guess. Try typeof `Number` since Javascript doesn't have an integer type. – Jeremy J Starcher May 11 '14 at 20:31
  • 1
    @JeremyJStarcher: On the left side you need to click on "a / an". But you were right, I can check for "number". May you write that as an answer so I can accept it? Thanks! :) – soerface May 11 '14 at 20:34

6 Answers6

52

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.

tleb
  • 4,395
  • 3
  • 25
  • 33
  • What's the difference if we don't use `be`. This works for me `expect(foo % 1).to.equal(0)` ? – Ashutosh Chamoli Feb 02 '19 at 12:31
  • As per [the doc](https://www.chaijs.com/api/bdd/), to/be/is/etc are only here to provide readability. This is confirmed by [the code](https://github.com/chaijs/chai/blob/0064f2604959260cf91282083cdcb7ba8acfce30/lib/chai/core/assertions.js#L43-L48). I'll edit as `expect(foo % 1).to.equal(0)` is more straight forward than `expect(foo % 1).to.be.equal(0)`. – tleb Feb 03 '19 at 11:14
  • If foo is 1.0 this assertion will return true. This may not matter for many use cases, but it's worth pointing out – Reagankm Aug 15 '23 at 23:51
  • @Reagankm: sadly, JavaScript only has two numeric types: Number or BigInt. Number is a 64-bit floating point number. There is no integer support. See the [ECMAScript 262 spec about the Number type](https://262.ecma-international.org/14.0/#sec-ecmascript-language-types-number-type). – tleb Aug 21 '23 at 16:51
25

JavaScript doesn't have a separate integer type.

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

Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
  • 1
    Probably because while you technically answered the question "How do I test with chai for a variable beeing an integer?", the spirit of the question is more like "How do I test with chai for a variable being a number which has a modulus 1 equal to 0? But then again, your answer was accepted, so maybe the downvote is unwarranted. – AndrewW Dec 01 '16 at 22:27
  • Can't edit my previous comment anymore, but a better rewording would be "How do I test with chai for a variable being a whole number?". Edit: The unfortunate thing is though that the definition of "whole number" in the dictionary says "a number without fractions; an integer." – AndrewW Dec 02 '16 at 00:12
14

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);
Convolver
  • 231
  • 3
  • 4
5

I feel your pain, this is what I came up with:

var assert = require('chai').assert;

describe('Something', function() {

    it('should be an integer', function() {

        var result = iShouldReturnInt();

        assert.isNumber(result);

        var isInt = result % 1 === 0;
        assert(isInt, 'not an integer:' + result);
    });
});
3

Depending on the browser/context you are running in there is also a function hanging off of Number that would be of some use.

var value = 42;
Number.isInteger(value).should.be.true;

It has not been adopted everywhere, but most of the places that matter (Chrome, FFox, Opera, Node)

More Info here

Bob_Gneu
  • 1,591
  • 1
  • 18
  • 30
0

Another [not optimal] solution (why not?!)

const actual = String(val).match(/^\d+$/);
expect(actual).to.be.an('array');
expect(actual).to.have.lengthOf(1);
rodrigo-silveira
  • 12,607
  • 11
  • 69
  • 123