From 8bffe2a24133661296c38d1f20dceccc6fa1ef62 Mon Sep 17 00:00:00 2001 From: isaacs Date: Fri, 14 Jun 2019 14:24:28 -0700 Subject: [PATCH] use function instead of => for addAssert Otherwise 'this' is bound improperly when called on the prototype. Fix #570 --- lib/test.js | 2 +- test/test.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/test.js b/lib/test.js index ea139cb4a..990d5ca1a 100644 --- a/lib/test.js +++ b/lib/test.js @@ -613,7 +613,7 @@ class Test extends Base { if (Test.prototype[name] || this[name]) throw new TypeError('attempt to re-define `' + name + '` assert') - const ASSERT = (...args) => { + const ASSERT = function (...args) { this.currentAssert = ASSERT if (typeof args[length] === 'object') { args[length + 1] = args[length] diff --git a/test/test.js b/test/test.js index c52fa8107..7bad194e5 100644 --- a/test/test.js +++ b/test/test.js @@ -1031,6 +1031,15 @@ t.test('addAssert', t => { return t.end() }) +t.test('addAssert on prototype', t => { + function foobar (found, message, extra) { + return this.ok(found, message, extra) + } + Test.prototype.addAssert('foobar', 1, foobar) + t.foobar(true, 'this is fine') + t.end() +}) + t.test('spawn', t => { const okjs = path.resolve(__dirname, '../ok.test.js') t.teardown(() => fs.unlinkSync(okjs))