Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chai.js - Check if string contains substring from a list

Tags:

chai

I'm using chai.js writing some automation tests. I have a string:

url(http://somewhere.com/images/myimage.png)

I want to do something like:

expect(thatSelectedItem).contains.any('jpg', 'png', 'gif')

However can't seem to find anything in chai.js

Any have any suggestions - read the page http://chaijs.com/api/bdd/ however no luck.

Any help appreciated.

Thanks.

like image 511
userMod2 Avatar asked Jul 11 '17 07:07

userMod2


2 Answers

With plain Chai (no additional plugins), you can use match:

expect(thatSelectedItem).to.match(/(?:jpg|png|gif)/)
like image 161
robertklep Avatar answered Nov 12 '22 22:11

robertklep


expect(thatSelectedItem).to.contain.oneOf(['jpg', 'png', 'gif'])

This lands with v4.3.0 (docs). oneOf can be chained with contain, contains, include and includes, which will work with both arrays and strings. It's strongly recommended to use because the error messages you get with it are much cleaner.

like image 28
t_dom93 Avatar answered Nov 12 '22 23:11

t_dom93