From a84aee03a26f8c1c385321c60fc4e9eab973b51d Mon Sep 17 00:00:00 2001 From: Arvid Ottenberg Date: Fri, 24 Apr 2020 20:36:32 +0200 Subject: [PATCH] refactor Test: add markOnly() for encapsulation (PR #4249) * add markOnly instance method to test class * add test cases for markOnly method * use markOnly method of test class instead of accessing parent properties method * refactor cases for test markOnly * refactor test class unit test markOnly to exhaustively satisfy Ref: #3689 --- lib/interfaces/common.js | 2 +- lib/test.js | 9 +++++++++ test/unit/test.spec.js | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/interfaces/common.js b/lib/interfaces/common.js index 1802fcc9dc..7991e113f7 100644 --- a/lib/interfaces/common.js +++ b/lib/interfaces/common.js @@ -165,7 +165,7 @@ module.exports = function(suites, context, mocha) { * @returns {*} */ only: function(mocha, test) { - test.parent.appendOnlyTest(test); + test.markOnly(); return test; }, diff --git a/lib/test.js b/lib/test.js index 65122b260c..87f1ccce7d 100644 --- a/lib/test.js +++ b/lib/test.js @@ -48,6 +48,15 @@ Test.prototype.retriedTest = function(n) { this._retriedTest = n; }; +/** + * Add test to the list of tests marked `only`. + * + * @private + */ +Test.prototype.markOnly = function() { + this.parent.appendOnlyTest(this); +}; + Test.prototype.clone = function() { var test = new Test(this.title, this.fn); test.timeout(this.timeout()); diff --git a/test/unit/test.spec.js b/test/unit/test.spec.js index 4cff662c89..7bf739d47e 100644 --- a/test/unit/test.spec.js +++ b/test/unit/test.spec.js @@ -2,6 +2,7 @@ var mocha = require('../../lib/mocha'); var Test = mocha.Test; +var sinon = require('sinon'); describe('Test', function() { describe('.clone()', function() { @@ -83,4 +84,29 @@ describe('Test', function() { expect(this._test.isPending(), 'to be', true); }); }); + + describe('.markOnly()', function() { + var sandbox; + + beforeEach(function() { + sandbox = sinon.createSandbox(); + }); + + afterEach(function() { + sandbox.restore(); + }); + + it('should call appendOnlyTest on parent', function() { + var test = new Test('foo'); + var spy = sandbox.spy(); + test.parent = { + appendOnlyTest: spy + }; + test.markOnly(); + + expect(spy, 'to have a call exhaustively satisfying', [test]).and( + 'was called once' + ); + }); + }); });