Skip to content

Commit

Permalink
refactor Test: add markOnly() for encapsulation (PR #4249)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
arvidOtt authored and craigtaub committed May 21, 2020
1 parent 2afb9f6 commit a84aee0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/interfaces/common.js
Expand Up @@ -165,7 +165,7 @@ module.exports = function(suites, context, mocha) {
* @returns {*}
*/
only: function(mocha, test) {
test.parent.appendOnlyTest(test);
test.markOnly();
return test;
},

Expand Down
9 changes: 9 additions & 0 deletions lib/test.js
Expand Up @@ -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());
Expand Down
26 changes: 26 additions & 0 deletions test/unit/test.spec.js
Expand Up @@ -2,6 +2,7 @@

var mocha = require('../../lib/mocha');
var Test = mocha.Test;
var sinon = require('sinon');

describe('Test', function() {
describe('.clone()', function() {
Expand Down Expand Up @@ -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'
);
});
});
});

0 comments on commit a84aee0

Please sign in to comment.