Jasmine's documentation is often brief, but not always sufficient.
I am curious about the second parameter of the toBeCloseTo function. The official reference only provides this example:
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);
});
While it mentions "precision," what does that mean practically in this context? Is it related to the number of digits after the decimal point that must match?
In my scenario, I need to compare two timestamps in milliseconds and consider them close if the difference is less than 100.
For instance, what value should X be in this case?
var timestamp1 = 1501254807000;
var timestamp2 = 1501254807099;
var timestamp3 = 1501254807100;
var precision = X;
expect(timestamp1).toBeCloseTo(timestamp2, precision); //this assertion should pass
expect(timestamp1).toBeCloseTo(timestamp3, precision); //this assertion should NOT pass
If the precision only deals with decimals, I could convert the integers to decimal numbers by dividing them by 1000. However, I'm unsure about the value of X. As of now, I'm using the following approach:
expect(Math.abs(timestamp2-timestamp1)).toBeLessThan(100);
Although functional, it lacks readability compared to using toBeCloseTo (which I prefer to use if possible).
Thank you.
Edit. The outcomes below might provide some clarity:
expect(1000000.005).toBeCloseTo(1000000.000,3); // this fails
expect(1000000.005).toBeCloseTo(1000000.000,2); // this fails
expect(1000000.005).toBeCloseTo(1000000.000,1); // this passes