Reputation: 11337
Using Mocha and Chai, I have notice many unreliable tests. I would like these ALL to fail. Similar StackOverflow questions are because of Async and not using done(). Even if this was my case, I would surely want it to fail!
I have a typo in what should be exist
. It doesnt tell me this unless I make a type in a particular position.
expect(component.find('textarea')).to.exest;
expect(component.find('textarea')).to.not.exest;
expect(component.find('textarea')).to.exsdafdsafdsfdsafest;
expect(component.find('textarea')).BLAHASDF.exest;
TypeError: Cannot read property 'exest' of undefined
Well, thank you Chai for telling me now!
Ok I am being a bit silly, but I can assure you this frustrate me a lot!
Is there some settings, or is there something I am missing, or is there another library that trumps this one?
Upvotes: 0
Views: 97
Reputation: 7055
What are you describing is the "normal" javascript behaviour when you try to access undefined properties. It's not something related to Chai, Mocha or any other library.
In Javascript, trying to access an undefined properties of an object doesn't cause any errors. You can do for example
var a = { foo: 'bar'};
console.log(a.baz); // This logs undefined, doesn't cause any error.
So when you do expect(component.find('textarea')).BLAHASDF.exest;
that instead will throw an error because you first try to access BLAHASDF
, which evaluates to undefined
and then you try to do undefined.exest
, which will raise an error because undefined
is a primitive, not an object.
I agree with you though that Chai Expect/Should APIs for these cases are not ideal. I'd prefer to do something like expect(component.find('textarea')).exist()
so that a typo there (e.g. .exests()) would indeed cause an error ("Undefined is not a function").
As noted in the comments though, you can indeed use different assertion styles in Chai: http://chaijs.com/api/assert/
If you look for another alternative library in this regard, I'd recommend either Jest https://facebook.github.io/jest/) or the minimalistic Tapejs (https://github.com/substack/tape)
Upvotes: 2