Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a test that checks for multiple types in chai

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?

like image 581
Austin Davis Avatar asked Dec 17 '14 21:12

Austin Davis


People also ask

What assertion styles are present in Chai testing assertion library?

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.

Should I assert vs vs expect?

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').

Do you need assertion in 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.


1 Answers

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'
});
like image 69
juunas Avatar answered Sep 20 '22 12:09

juunas