例如,这是列表:lst = ["69.78","","20.60","14.70","8.20","14.20","7.70","15.30"]
如何期望lst
包含字符串类型的项目?
例如,这应该失败:[1,5,2]
和['5', 2, '1']
您可以遍历数组并对每个项目调用isString
:
const { assert } = require('chai');
lst.forEach(item => assert.isString(item));
或者你可以使用expect
const { expect } = require('chai');
lst.forEach(item => expect(item).to.be.a('string'))
检查是否有任何项目不是字符串
const notStrings = lst.filter(n => typeof n !== 'string')
expect(notStrings.length).to.equal(0);