chai-quantifiers
针对 Chai 断言库的数组量词断言。
安装
npm install --save-dev chai chai-quantifiers
用法
有三种断言可用于数组。
- containAll - 断言所有数组项在谓词方面为真。
- containOne - 断言至少一个数组项在谓词方面为真。
- containExactlyOne - 断言恰好一个数组项在谓词方面为真。
快速示例
const chai = require('chai');
const chaiQuantifiers = require('chai-quantifiers');
chai.use(chaiQuantifiers);
const { expect } = chai;
describe('chai-quantifiers', () => {
it('containAll should be true if all items are true', () => {
expect([0, 1, 2, 3]).to.containAll(item => item < 4);
});
it('containOne should be true if at least one item is true', () => {
expect([0, 1, 2, 3]).to.containOne(item => item >= 2);
});
it('containExactlyOne should be true if exactly one item is true', () => {
expect([0, 1, 2, 3]).to.containExactlyOne(item => item === 2);
});
});
此模块还包括 TypeScript 的类型。