chai-bn
Chai
斷言,用於使用 bn.js 函式庫比較任意精度的整數。從 chai-bignumber 分支而來,後者使用 bignumber.js 函式庫。
安裝
npm install --save-dev chai-bn
使用方式
const chai = require('chai');
const BN = require('bn.js');
// Enable and inject BN dependency
chai.use(require('chai-bn')(BN));
斷言
如果 bignumber
屬性設定為斷言鏈的一部分,則會提供以下斷言方法,並覆蓋現有的內建斷言。
- equal/equals/eq
- above/gt/greaterThan
- least/gte
- below/lt/lessThan
- most/lte
- closeTo
還提供了一組額外的斷言屬性
- negative
- zero
實際值(被斷言的值)和期望值(實際值預期要匹配的值)都可以是 BN
的實例,或可以轉換為有效數字的字串。這與 chai-bignumber 的主要區別在於,後者會自動將 JavaScript 數字轉換為 BigNumber
實例,用於實際值和期望值。
僅支援 BDD 風格(expect
或 should
)斷言。
範例
方法
const actual = new BN('100000000000000000').plus(new BN('1'));
const expected = '100000000000000001';
actual.should.be.a.bignumber.that.equals(expected);
expect(actual).to.be.a.bignumber.that.is.at.most(expected);
(new BN('1000')).should.be.a.bignumber.that.is.lessThan('2000');
屬性
(new BN('-100')).should.be.a.bignumber.that.is.negative;
expect(new BN('1').sub(new BN('1'))).to.be.a.bignumber.that.is.zero;
某些 Chai
屬性(例如 that.is
鏈)除了提高可讀性之外,沒有其他作用,如果希望減少冗長度,可以省略。