From 00d9b968d39cb4dc939f8052b4fa242080f7ce57 Mon Sep 17 00:00:00 2001 From: Whitney Young Date: Sun, 4 Oct 2015 14:49:33 -0700 Subject: [PATCH] No longer silently allowing undefined method calls. Fixes #467. --- lib/chai/utils/overwriteMethod.js | 4 +++- test/utilities.js | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/chai/utils/overwriteMethod.js b/lib/chai/utils/overwriteMethod.js index 20657a57c..4c61c181e 100644 --- a/lib/chai/utils/overwriteMethod.js +++ b/lib/chai/utils/overwriteMethod.js @@ -42,7 +42,9 @@ var flag = require('./flag'); module.exports = function (ctx, name, method) { var _method = ctx[name] - , _super = function () { return this; }; + , _super = function () { + throw new Error(name + ' is not a function'); + }; if (_method && 'function' === typeof _method) _super = _method; diff --git a/test/utilities.js b/test/utilities.js index 66d6a8178..0fcae7c03 100644 --- a/test/utilities.js +++ b/test/utilities.js @@ -302,13 +302,30 @@ describe('utilities', function () { expect(_super).to.be.a('function'); return function () { _.flag(this, 'doesnt', true); - _super.apply(this, arguments); } }); }); var dne = expect('something').to.doesnotexist(); expect(dne.__flags).to.have.property('doesnt'); + + chai.use(function (_chai, _) { + expect(_chai.Assertion).to.not.respondTo('doesnotexistfail'); + _chai.Assertion.overwriteMethod('doesnotexistfail', function (_super) { + expect(_super).to.be.a('function'); + return function () { + _.flag(this, 'doesnt', true); + _super.apply(this, arguments); + } + }); + }); + + var dneFail = expect('something'); + var dneError; + try { dneFail.to.doesnotexistfail(); } + catch (e) { dneError = e; } + expect(dneFail.__flags).to.have.property('doesnt'); + expect(dneError.message).to.eql('doesnotexistfail is not a function'); }); it('overwriteMethod returning result', function () {