Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

refactor Test: add markOnly() for encapsulation (PR #4249)

Arvid Ottenberg5f9400df1d0c
diff --git a/lib/interfaces/common.js b/lib/interfaces/common.js
index 1802fcc9..7991e113 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 65122b26..87f1ccce 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 4cff662c..7bf739d4 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'
+ );
+ });
+ });
});