You can use above(val)
to check if a number is above a value.
This will make sure that the value is both a number and that it's above whatever value you want. In this case, since you want it to be a positive value, we'll use above(0)
.
If you want to also check if it's a whole number, you can use expect(val % 1).to.equal(0)
as shown in Chai Unittesting - expect(42).to.be.an('integer')
chai.expect(5).to.be.above(0)
try {
chai.expect(-5).to.be.above(0)
} catch (error) {
console.log(error.message);
}
try {
chai.expect(0).to.be.above(0)
} catch (error) {
console.log(error.message);
}
try {
chai.expect('5').to.be.above(0)
} catch (error) {
console.log(error.message);
}
try {
let val = 2.5;
chai.expect(val).to.be.above(0);
chai.expect(val % 1,'to be a whole number').to.equal(0)
} catch (error) {
console.log(error.message);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.2.0/chai.min.js"></script>