Ryan Cameron
Ryan Cameron

Reputation: 371

Chai expect list to contain items of type string

E.g. this is the list: lst = ["69.78","","20.60","14.70","8.20","14.20","7.70","15.30"]

How can I expect lst to contain items of type string?

For instance this should fail: [1,5,2] and ['5', 2, '1']

Upvotes: 1

Views: 1191

Answers (3)

Zohaib Ijaz
Zohaib Ijaz

Reputation: 22885

Check if any items in not string

const notStrings = lst.filter(n => typeof n !== 'string') 
expect(notStrings.length).to.equal(0);

Upvotes: 0

Ziyo
Ziyo

Reputation: 604

Or you can use expect

const { expect } = require('chai');
lst.forEach(item => expect(item).to.be.a('string'))

Upvotes: 1

Phil Booth
Phil Booth

Reputation: 4913

You could iterate through the array and call isString on each item:

const { assert } = require('chai');
lst.forEach(item => assert.isString(item));

Upvotes: 1

Related Questions