{"action":"created","issue":{"url":"https://api.github.com/repos/substack/tape/issues/154","labels_url":"https://api.github.com/repos/substack/tape/issues/154/labels{/name}","comments_url":"https://api.github.com/repos/substack/tape/issues/154/comments","events_url":"https://api.github.com/repos/substack/tape/issues/154/events","html_url":"https://github.com/substack/tape/issues/154","id":82075155,"number":154,"title":"How to define new test methods?","user":{"login":"mbostock","id":230541,"avatar_url":"https://avatars.githubusercontent.com/u/230541?v=3","gravatar_id":"","url":"https://api.github.com/users/mbostock","html_url":"https://github.com/mbostock","followers_url":"https://api.github.com/users/mbostock/followers","following_url":"https://api.github.com/users/mbostock/following{/other_user}","gists_url":"https://api.github.com/users/mbostock/gists{/gist_id}","starred_url":"https://api.github.com/users/mbostock/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mbostock/subscriptions","organizations_url":"https://api.github.com/users/mbostock/orgs","repos_url":"https://api.github.com/users/mbostock/repos","events_url":"https://api.github.com/users/mbostock/events{/privacy}","received_events_url":"https://api.github.com/users/mbostock/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-05-28T18:40:07Z","updated_at":"2015-05-28T20:05:04Z","closed_at":null,"body":"Say I want something like test.equal but tolerant of limited floating point precision. I might be tempted to say:\r\n\r\n```js\r\ntape.Test.prototype.inDelta = function(actual, expected) {\r\n  this._assert(expected - 1e-6 < actual && actual < expected + 1e-6, {\r\n    message: 'should be in delta',\r\n    operator: 'inDelta',\r\n    actual: actual,\r\n    expected: expected\r\n  });\r\n};\r\n```\r\n\r\nThis mostly works, but fails to produce the correct “at” line when an error is reported due to how test._assert looks at the stack trace. This makes it hard to identify where the test is broken. For example:\r\n\r\n```\r\n  not ok 20 should be in delta\r\n    ---\r\n      operator: inDelta\r\n      expected: 0.1\r\n      actual:   0.010000000000000002\r\n      at: Test.tape.Test.inDelta (/Users/mbostock/Development/d3-transition/test/ease-test.js:6:8)\r\n    ...\r\n```\r\n\r\nNote that ease-test.js:6 is where the test._assert is called, not where test.inDelta is called.\r\n\r\nI could do something like this instead:\r\n\r\n```js\r\nfunction inDelta(actual, expected) {\r\n  return expected - 1e-6 < actual && actual < expected + 1e-6;\r\n}\r\n```\r\n\r\nAnd then `test.ok(inDelta(actual, expected))`. That fixes the “at” line, but now I can’t see the helpful “expected” and “actual” values:\r\n\r\n```\r\n  not ok 20 (unnamed assert)\r\n    ---\r\n      operator: ok\r\n      expected: true\r\n      actual:   false\r\n      at: Test.<anonymous> (/Users/mbostock/Development/d3-transition/test/ease-test.js:60:8)\r\n    ...\r\n```\r\n\r\nApologies if this is answered somewhere already. I couldn’t find an answer."},"comment":{"url":"https://api.github.com/repos/substack/tape/issues/comments/106582082","html_url":"https://github.com/substack/tape/issues/154#issuecomment-106582082","issue_url":"https://api.github.com/repos/substack/tape/issues/154","id":106582082,"user":{"login":"substack","id":12631,"avatar_url":"https://avatars.githubusercontent.com/u/12631?v=3","gravatar_id":"","url":"https://api.github.com/users/substack","html_url":"https://github.com/substack","followers_url":"https://api.github.com/users/substack/followers","following_url":"https://api.github.com/users/substack/following{/other_user}","gists_url":"https://api.github.com/users/substack/gists{/gist_id}","starred_url":"https://api.github.com/users/substack/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/substack/subscriptions","organizations_url":"https://api.github.com/users/substack/orgs","repos_url":"https://api.github.com/users/substack/repos","events_url":"https://api.github.com/users/substack/events{/privacy}","received_events_url":"https://api.github.com/users/substack/received_events","type":"User","site_admin":false},"created_at":"2015-05-28T20:05:04Z","updated_at":"2015-05-28T20:05:04Z","body":"I usually do this kind of thing instead, for what it's worth:\r\n\r\n``` js\r\nfunction approx (t, a, b, msg) {\r\n  return t.ok(expected - 1e-6 < actual && actual < expected + 1e-6, msg);\r\n}\r\n```\r\n\r\nand then you just pass in the `t` reference, instead of modifying the prototype globally."}}