与Chai的Deep和Close数组相等

时间:2017-01-26 18:45:18

标签: javascript testing chai

所以在Chai .deep.equals中允许一个按值比较数组,而.closeTo(和.approximately)允许一个人将浮点数与指定的精度进行比较。我在如何让它做两件事时都画了一个空白,即测试"关闭"一系列浮点数相等,例如

expect([0.1,0.2,0.34]).to.beDeeplyCloseTo([0.1,0.2,0.33333333]);

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试mohayonao/chai-deep-closeto

var chai = require("chai");
var chaiDeepCloseTo = require("chai-deep-closeto");

chai.use(chaiDeepCloseTo);

expect([ 1.00001, [ 2.00001, 3.00001 ], 4.00001 ])
    .to.be.deep.closeTo([ 1, [ 2, 3 ], 4 ], 1e-4);