Jasmine has very short documentation; often it's enough. Not always.
I want to know what is exactly the second parameter of toBeCloseTo. Official reference only shows:
it("The 'toBeCloseTo' matcher is for precision math comparison", function() {
var pi = 3.1415926, e = 2.78;
expect(pi).not.toBeCloseTo(e, 2);
expect(pi).toBeCloseTo(e, 0);
});
OK it is precision, but what practically means "precision" in this case? Is it the number of digits after the "." that should be the same?
My case: i want to compare two timestamps in milliseconds; if the difference between them is less than 100, it's fine for me.
As example, what's the value of X in the following case?
var timestamp1 = 1501254807000;
var timestamp2 = 1501254807099;
var timestamp3 = 1501254807100;
var precision = X;
expect(timestamp1).toBeCloseTo(timestamp2, precision); //this should pass
expect(timestamp1).toBeCloseTo(timestamp3, precision); //this should NOT pass
If the precision is only for decimal numbers, I could divide my integers by 1000 to get decimal numbers, but anyway, I don't know what is X. For now, I do in this way:
expect(Math.abs(timestamp2-timestamp1)).toBeLessThan(100);
but it's not very readable, and I would like to use toBeCloseTo (since it exests...).
Thanks
Edit. The following results may help:
expect(1000000.005).toBeCloseTo(1000000.000,3); //fails
expect(1000000.005).toBeCloseTo(1000000.000,2); //fails
expect(1000000.005).toBeCloseTo(1000000.000,1); //pass