I'm trying to write a test that would pass if the input is either a string or a null value
in chai is there something similar to
expect(foo).to.be.a('string').or.a('null')
If not what would be best practice when writing a test that needs to check for multiple types?
Chai is such an assertion library, which provides certain interfaces to implement assertions for any JavaScript-based framework. Chai's interfaces are broadly classified into two: TDD styles and BDD styles.
Note expect and should uses chainable language to construct assertions, but they differ in the way an assertion is initially constructed. In the case of should , there are also some caveats and additional tools to overcome the caveats. var expect = require('chai').
Should. The should style allows for the same chainable assertions as the expect interface, however it extends each object with a should property to start your chain. This style has some issues when used with Internet Explorer, so be aware of browser compatibility.
This would probably be the easiest way as there is no or
keyword.
var str = null;
expect(str).to.satisfy(function(s){
return s === null || typeof s == 'string'
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With