We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 25bcaab commit 93409cdCopy full SHA for 93409cd
lib/chai/core/assertions.js
@@ -3150,8 +3150,12 @@ function closeTo(expected, delta, msg) {
3150
3151
const abs = (x) => (x < 0n ? -x : x);
3152
3153
+ // Used to round floating point number precision arithmetics
3154
+ // See: https://stackoverflow.com/a/3644302
3155
+ const strip = (number) => parseFloat(parseFloat(number).toPrecision(12));
3156
+
3157
this.assert(
- abs(obj - expected) <= delta,
3158
+ strip(abs(obj - expected)) <= delta,
3159
'expected #{this} to be close to ' + expected + ' +/- ' + delta,
3160
'expected #{this} not to be close to ' + expected + ' +/- ' + delta
3161
);
test/assert.js
@@ -1894,6 +1894,8 @@ describe('assert', function () {
1894
assert.closeTo(10, 20, 20);
1895
assert.closeTo(-10, 20, 30);
1896
assert.closeTo(10, 10, 0);
1897
+ assert.closeTo(1682.6, 1682.7, 0.1);
1898
+ assert.closeTo(1n, 2n, 1n);
1899
1900
err(function(){
1901
assert.closeTo(2, 1.0, 0.5, 'blah');
@@ -1928,6 +1930,8 @@ describe('assert', function () {
1928
1930
assert.approximately(1.5, 1.0, 0.5);
1929
1931
assert.approximately(10, 20, 20);
1932
assert.approximately(-10, 20, 30);
1933
+ assert.approximately(10, 10, 0);
1934
+ assert.approximately(1682.6, 1682.7, 0.1);
1935
assert.approximately(1n, 2n, 1n);
1936
1937
test/expect.js
@@ -3267,6 +3267,9 @@ describe('expect', function () {
3267
expect(1.5).to.be.closeTo(1.0, 0.5);
3268
expect(10).to.be.closeTo(20, 20);
3269
expect(-10).to.be.closeTo(20, 30);
3270
+ expect(10).to.be.closeTo(10, 0);
3271
+ expect(1682.6).to.be.closeTo(1682.7, 0.1);
3272
+ expect(1n).to.be.closeTo(2n, 1n);
3273
3274
3275
expect(2).to.be.closeTo(1.0, 0.5, 'blah');
@@ -3313,6 +3316,9 @@ describe('expect', function () {
3313
3316
expect(1.5).to.be.approximately(1.0, 0.5);
3314
3317
expect(10).to.be.approximately(20, 20);
3315
3318
expect(-10).to.be.approximately(20, 30);
3319
+ expect(10).to.be.approximately(10, 0);
3320
+ expect(1682.6).to.be.approximately(1682.7, 0.1);
3321
+ expect(1n).to.be.approximately(2n, 1n);
3322
3323
3324
expect(2).to.be.approximately(1.0, 0.5, 'blah');
test/should.js
@@ -2749,6 +2749,11 @@ describe('should', function() {
2749
2750
it('closeTo', function(){
2751
(1.5).should.be.closeTo(1.0, 0.5);
2752
+ (10).should.be.closeTo(20, 20);
2753
+ (-10).should.be.closeTo(20, 30);
2754
+ (10).should.be.closeTo(10, 0);
2755
+ (1682.6).should.be.closeTo(1682.7, 0.1);
2756
+ (1n).should.be.closeTo(2n, 1n);
2757
2758
2759
(2).should.be.closeTo(1.0, 0.5, 'blah');
@@ -2773,6 +2778,11 @@ describe('should', function() {
2773
2778
2774
2779
it('approximately', function(){
2775
2780
(1.5).should.be.approximately(1.0, 0.5);
2781
+ (10).should.be.approximately(20, 20);
2782
+ (-10).should.be.approximately(20, 30);
2783
+ (10).should.be.approximately(10, 0);
2784
+ (1682.6).should.be.approximately(1682.7, 0.1);
2785
+ (1n).should.be.approximately(2n, 1n);
2776
2786
2777
2787
2788
(2).should.be.approximately(1.0, 0.5, 'blah');
0 commit comments