From 524b2dec4152573d952c4eed34451ffa0d2e7e50 Mon Sep 17 00:00:00 2001 From: Arvid Ottenberg Date: Thu, 23 Apr 2020 22:31:14 +0200 Subject: [PATCH 1/5] add markOnly instance method to test class --- lib/test.js | 9 +++++++++ 1 file changed, 9 insertions(+) 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()); From b202e34028613885b2035f6ab9f727596bc8ebc3 Mon Sep 17 00:00:00 2001 From: Arvid Ottenberg Date: Thu, 23 Apr 2020 22:31:58 +0200 Subject: [PATCH 2/5] add test cases for markOnly method --- test/unit/test.spec.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/unit/test.spec.js b/test/unit/test.spec.js index 4cff662c89..ffe8656130 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,40 @@ 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, 'was called times', 1); + }); + + it('should pass itself as an argument', function() { + var test = new Test('foo'); + var spy = sandbox.spy(); + test.parent = { + appendOnlyTest: spy + }; + + test.markOnly(); + + expect(spy.getCall(0).args[0], 'to be', test); + }); + }); }); From 79b8270a5c7ee143fe82a7c94bfff6d4ad02b432 Mon Sep 17 00:00:00 2001 From: Arvid Ottenberg Date: Thu, 23 Apr 2020 22:34:10 +0200 Subject: [PATCH 3/5] use markOnly method of test class instead of accessing parent properties method --- lib/interfaces/common.js | 2 +- 1 file changed, 1 insertion(+), 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; }, From 88b24ef7df3dab0e0ac37eb0410cb79ff293dbdd Mon Sep 17 00:00:00 2001 From: Arvid Ottenberg Date: Thu, 23 Apr 2020 23:38:04 +0200 Subject: [PATCH 4/5] refactor cases for test markOnly --- test/unit/test.spec.js | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/test/unit/test.spec.js b/test/unit/test.spec.js index ffe8656130..8ad33d5a07 100644 --- a/test/unit/test.spec.js +++ b/test/unit/test.spec.js @@ -105,19 +105,7 @@ describe('Test', function() { test.markOnly(); - expect(spy, 'was called times', 1); - }); - - it('should pass itself as an argument', function() { - var test = new Test('foo'); - var spy = sandbox.spy(); - test.parent = { - appendOnlyTest: spy - }; - - test.markOnly(); - - expect(spy.getCall(0).args[0], 'to be', test); + expect(spy, 'to have a call satisfying', [test]).and('was called once'); }); }); }); From be48dc8cef7a70b2dfa1e37952f0a271d13851e5 Mon Sep 17 00:00:00 2001 From: Arvid Ottenberg Date: Fri, 24 Apr 2020 00:19:59 +0200 Subject: [PATCH 5/5] refactor test class unit test markOnly to exhaustively satisfy --- test/unit/test.spec.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/unit/test.spec.js b/test/unit/test.spec.js index 8ad33d5a07..7bf739d47e 100644 --- a/test/unit/test.spec.js +++ b/test/unit/test.spec.js @@ -102,10 +102,11 @@ describe('Test', function() { test.parent = { appendOnlyTest: spy }; - test.markOnly(); - expect(spy, 'to have a call satisfying', [test]).and('was called once'); + expect(spy, 'to have a call exhaustively satisfying', [test]).and( + 'was called once' + ); }); }); });