qunit-assert-close
A QUnit plugin for asserting that a number is approximately equal (or not) to an expected number, within a given tolerance.
npm install qunit-assert-close
Want to see pretty graphs? Log in now!
7 | downloads in the last week |
12 | downloads in the last month |
Last Published By | |
---|---|
Version | 1.1.0 last updated a month ago |
License | MIT |
Keywords | qunit-plugin, qunit-assert, qunit, assert, close, close-enough, roughlyEquals, closeTo, approximate |
Repository | git://github.com/JamesMGreene/qunit-assert-close.git (git) |
Homepage | https://github.com/JamesMGreene/qunit-assert-close |
Bugs | https://github.com/JamesMGreene/qunit-assert-close/issues |
Dependencies | None |
QUnit Close assertion plugin
This plugin for QUnit adds close
, notClose
, close.percent
, and notClose.percent
assertion methods to test that a number is approximately equal (or not) to an expected number, within a given tolerance.
Usage
assert.close(actual, expected, maxDifference, message);
assert.notClose(actual, expected, minDifference, message);
assert.close.percent(actual, expected, maxPercentDifference, message);
assert.notClose.percent(actual, expected, minPercentDifference, message);
Where:
maxDifference
: the maximum inclusive difference allowed (tolerance) between theactual
andexpected
numbersminDifference
: the minimum exclusive difference allowed (tolerance) between theactual
andexpected
numbersactual
,expected
,message
: The usual
Examples
Example 1: Number differences
QUnit.test('Example number unit test', function(assert) {
assert.close(3.141, Math.PI, 0.001);
assert.notClose(3.1, Math.PI, 0.001);
});
Example 2: Percentage differences
QUnit.test('Example percentage unit test', function(assert) {
assert.close.percent(155, 150, 3.4); // Difference is ~3.33%
assert.notClose.percent(156, 150, 3.5); // Difference is 4.0%
});
For more examples, refer to the unit tests.