Skip to content

Commit a84aee0

Browse files
arvidOttcraigtaub
authored andcommitted
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
1 parent 2afb9f6 commit a84aee0

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

lib/interfaces/common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ module.exports = function(suites, context, mocha) {
165165
* @returns {*}
166166
*/
167167
only: function(mocha, test) {
168-
test.parent.appendOnlyTest(test);
168+
test.markOnly();
169169
return test;
170170
},
171171

lib/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ Test.prototype.retriedTest = function(n) {
4848
this._retriedTest = n;
4949
};
5050

51+
/**
52+
* Add test to the list of tests marked `only`.
53+
*
54+
* @private
55+
*/
56+
Test.prototype.markOnly = function() {
57+
this.parent.appendOnlyTest(this);
58+
};
59+
5160
Test.prototype.clone = function() {
5261
var test = new Test(this.title, this.fn);
5362
test.timeout(this.timeout());

test/unit/test.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var mocha = require('../../lib/mocha');
44
var Test = mocha.Test;
5+
var sinon = require('sinon');
56

67
describe('Test', function() {
78
describe('.clone()', function() {
@@ -83,4 +84,29 @@ describe('Test', function() {
8384
expect(this._test.isPending(), 'to be', true);
8485
});
8586
});
87+
88+
describe('.markOnly()', function() {
89+
var sandbox;
90+
91+
beforeEach(function() {
92+
sandbox = sinon.createSandbox();
93+
});
94+
95+
afterEach(function() {
96+
sandbox.restore();
97+
});
98+
99+
it('should call appendOnlyTest on parent', function() {
100+
var test = new Test('foo');
101+
var spy = sandbox.spy();
102+
test.parent = {
103+
appendOnlyTest: spy
104+
};
105+
test.markOnly();
106+
107+
expect(spy, 'to have a call exhaustively satisfying', [test]).and(
108+
'was called once'
109+
);
110+
});
111+
});
86112
});

0 commit comments

Comments
 (0)