Node mocha array must contain an element

I want to make a simple statement of something like

knownArray.should.include('known value') 

The array is correct, but I just cannot figure out which correct statement to use to check if the array has this value (the index does not matter). I also tried should.contain , but both of them cause an error that Object #<Assertion> has no method 'contain' (or 'include' )

How to verify that an array contains an element using should ?

+7
source share
4 answers

Should.js has a containEql method. In your case:

 knownArray.should.containEql('known value'); 

You will find methods include , includes and contain in chai.js.

+15
source

Mostly from mocha docs you can do

 var assert = require('assert'); var should = require('should'); describe('Array', function(){ describe('#indexOf(thing)', function(){ it('should not be -1 when thing is present', function(){ [1,2,3].indexOf(3).should.not.equal(-1); }); }); }); 

or if you do not mind using, you can always

 assert.notEqual(-1, knownArray.indexOf(thing)); 
+2
source

I really like someone's stuff:

https://github.com/RubenVerborgh/Chai-Things

This allows you to do things like:

 [{ a: 'cat' }, { a: 'dog' }].should.include({ a: 'cat' }); ['cat', 'dog'].should.include('cat'); 
+1
source

In case someone else comes across this and uses tea, this is what I used based on documents for should / enable

  it('Should grab all li elements using double $$', () => { const liEl = $$('ul li'); const fruitList = ['Bananas', 'Apples', 'Oranges', 'Pears']; liEl.forEach((el) => { expect(fruitList).to.include(el.getText()); }); }); 

I use webdriver.io, so you can ignore the part where I take the element, just included for completeness.

0
source

Source: https://habr.com/ru/post/984531/


All Articles