Chai.js - 检查字符串是否包含列表中的子字符串

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

我正在使用 chai.js 编写一些自动化测试。我有一个字符串:

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

我想做这样的事情:

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

但是似乎无法在 chai.js

中找到任何内容

任何有任何建议 - 阅读页面 http://chaijs.com/api/bdd/ 但是没有运气。

感谢任何帮助。

谢谢。

使用普通的 Chai(没有额外的插件),你可以使用 match:

expect(thatSelectedItem).to.match(/(?:jpg|png|gif)/)
expect(thatSelectedItem).to.contain.oneOf(['jpg', 'png', 'gif'])

这与 v4.3.0 (docs) 一起着陆。 oneOf 可以与 containcontainsincludeincludes 链接,这将适用于数组和字符串。强烈推荐使用它,因为你得到的错误消息更清晰。