Reputation: 145
I have these form fields I need to validate with chai expect. They all work (I get errors) except for the fields that require characters - expect(value).to.be.NaN is not working... The code:
var inputs = [response.fname, response.surname,
response.age, response.password
];
for (var i = 0; i < inputs.length; i++) {
var stackErrors = validate(i);
console.log(stackErrors);
}
function validate(i) {
var fields = inputs[i];
try {
if (i == 0) {
expect(fields).to.be.Nan;
}
if (i == 1) {
expect(fields).to.be.Nan;
}
if (i == 2) {
expect(fields).not.to.be.NaN;
}
if (i == 3) {
expect(fields).to.have.length.above(6);
}
return "OK";
} catch (error) {
console.log(error);
return error;
}
}
But if test the field like in the example bellow, right after the inputs array, I get the expected assertion error...:
expect(inputs[0]).to.be.NaN;
Why is this happening? Thanks in advance
Upvotes: 0
Views: 3059
Reputation: 2856
From playwright official documentation toBeNaN Added in: v1.9
Ensures that value is NaN:
const value = NaN;
expect(value).toBeNaN();
Upvotes: 0
Reputation: 145
Thanks to the hint of @Lazyexpert (I've noticed you have Nan in your code, it is not the same as NaN.), I found the obvious solution...(a case sensitive issue, actualy)
try {
if (i == 0) {
expect(fields).to.be.NaN; //instead of Nan...
}
Upvotes: 3
Reputation: 3154
Assuming this code:
Number.isNaN(NaN) // true
And:
Number.isNaN(5) // false
I would do a small workaround(just to make it fast), something like this:
expect(Number.isNaN(inputs[0])).to.be.eq(false);
UPDATE: I've noticed you have Nan in your code, it is not the same as NaN.
I'm not sure how it is implemented, but as far as it is chained, try this:
expect(undefined).to.be.eq.Nan
I think you should receive true in this case.
Upvotes: 2