Assertion Error when using an integer data type

Hi I have a test which should test if returned id exists and if its integer. But I am getting this error which does not make sense to me.
AssertionError: expected 104 to be an integer

var id = data.id ? data.id : null;

pm.test('Required id test', function() {
      pm.expect(id).to.be.an('integer');
});

What is wrong with it?

Hey @vladimir.camaj

I believe the syntax for that data type is .to.be.a('number'). Does that work if you change it to that?

1 Like

Here is another way of doing it.

var id = 123

pm.test('Required id test', function() {
    pm.expect(Number.isInteger(id)).to.be.true
});
2 Likes

This works, but can you explain me please why integer does not work but number works? It does not make any sense. Also number can be also a float…

This works too but why integer test does not work?

All of that magic and functionality within the pm.expect() function comes directly from the Chai Js library.

Having a number keyword is how the creators/maintainers of that library have implemented that functionality. I wouldn’t really be able to provide any reasoning behind that decision. :sweat:

1 Like

Postman is using the CHAI JS assertion library.

Expect / Should - Chai (chaijs.com)

Integer is not a valid string type that can be used with “to.be.a”.

CHAI actually uses type detect for this aspect.

GitHub - chaijs/type-detect: Improved typeof detection for node.js and the browser.

When hit with issues like this, Postman is using JavaScript under the hood so searching the web including “JavaScript” in the search terms will usually give you a few hits and point you in the right direction.

Number.isInteger() is the recommended way of checking if a number is an integer.

2 Likes

Randomly, I was just notified of an upvote on this post from a couple of years ago :joy:

1 Like

I like the custom message that you included in that article. I’ll have to remember that exists so you don’t just get a bland “expected false to be true” message.

2 Likes

That upvote is from me :slight_smile:

1 Like

Thank you, Sir.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.