Node: Feature request: add closeTo (or approximate, delta, tolerance epsilon) for assert

Created on 8 Aug 2017  路  7Comments  路  Source: nodejs/node

  • Version: 8.x
  • Subsystem: assert
const assert = require('assert');

const actual = { a: { b: 0.33333 }};
const expected = { a: { b: 1/3 }};
assert.deepEqual(actual, expected);     // will currently throw

// proposal 1:
const options = { delta: 1e-2 };
assert.deepEqual(actual, expected, options);     // would pass without error

// proposal 2:
assert.closeTo(actual, expected, 1e-6);     // would pass without error
assert feature request

Most helpful comment

I'm -1 on adding more features to assert module in general. There is a variety of great userspace alternatives that already have this feature with much more clearly defined semantics and flexibility for change. Though the assert module API has become unfrozen, assert.deepEqual alone has drawn numerous discussions/bikeshedding around what it should do regarding special edge cases like Maps and Sets. If assert must be used, this specific feature can be implemented fairly easily even on a per line basis with assert(Math.abs(expected - actual) < eps).

All 7 comments

I am definitely +1 on having assert.closeTo(actual, expected, delta), assuming actual and expected are numbers. For all other instances, e.g. numbers inside objects as in your snippet, I am not sure this is a good idea.

What is our current stance on extending assert? Are we still saying it's more or less frozen or did we backtrack on that?

cc @Trott - you probably have an opinion.

I'm -1 on adding more features to assert module in general. There is a variety of great userspace alternatives that already have this feature with much more clearly defined semantics and flexibility for change. Though the assert module API has become unfrozen, assert.deepEqual alone has drawn numerous discussions/bikeshedding around what it should do regarding special edge cases like Maps and Sets. If assert must be used, this specific feature can be implemented fairly easily even on a per line basis with assert(Math.abs(expected - actual) < eps).

I think I would rather see this in the ecosystem / published to npm than put in core.

Hello @AJamesPhillips and welcome!
node core has a general notion of "small core" in that there is motivation to keep core small, adding only functionality that is impossible or hard to implement in "userland".
So while your suggestion has obvious merits, as the people above commented, it's easy to implement, and thous it's not an obvious candidate for inclusion in a core module.
node operates under a consensus-seeking decision making process, so as long as there are Collaborators who object, such a feature request will not accepted easily.

Anyway thank you for the contribution, and keep being creative. IMHO the project could benefit from your future interaction.

P.S. I closed this feature request, but feel free to reopen if you think I'm wrong. And @AJamesPhillips you can contact me directly if you would like more information.

Was this page helpful?
0 / 5 - 0 ratings