Skip to content

armgit / mocha

Public Imported from GitHub
0

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

When using .only on a specific test, make sure that other tests with a subset of the .only tests title isn't also run

Fredrik Enestad179ffd3598f1
diff --git a/Makefile b/Makefile
index 1b922c06..ed6d8083 100644
--- a/Makefile
+++ b/Makefile
@@ -32,7 +32,7 @@ lib-cov:
test: test-unit
-test-all: test-bdd test-tdd test-qunit test-exports test-unit test-grep test-jsapi test-compilers test-glob test-requires test-reporters
+test-all: test-bdd test-tdd test-qunit test-exports test-unit test-grep test-jsapi test-compilers test-glob test-requires test-reporters test-only
test-jsapi:
@node test/jsapi
@@ -118,6 +118,22 @@ test-reporters:
--reporter $(REPORTER) \
test/reporters/*.js
+test-only:
+ @./bin/mocha \
+ --reporter $(REPORTER) \
+ --ui tdd \
+ test/acceptance/misc/only/tdd
+
+ @./bin/mocha \
+ --reporter $(REPORTER) \
+ --ui bdd \
+ test/acceptance/misc/only/bdd
+
+ @./bin/mocha \
+ --reporter $(REPORTER) \
+ --ui qunit \
+ test/acceptance/misc/only/qunit
+
non-tty:
@./bin/mocha \
--reporter dot \
diff --git a/lib/interfaces/bdd.js b/lib/interfaces/bdd.js
index 96b12c8b..e838109d 100644
--- a/lib/interfaces/bdd.js
+++ b/lib/interfaces/bdd.js
@@ -4,7 +4,8 @@
*/
var Suite = require('../suite')
- , Test = require('../test');
+ , Test = require('../test')
+ , utils = require('../utils');
/**
* BDD-style interface:
@@ -117,7 +118,8 @@ module.exports = function(suite){
context.it.only = function(title, fn){
var test = context.it(title, fn);
- mocha.grep(test.fullTitle());
+ var reString = '^' + utils.escapeRegexp(test.fullTitle()) + '$';
+ mocha.grep(new RegExp(reString));
};
/**
diff --git a/lib/interfaces/qunit.js b/lib/interfaces/qunit.js
index 521a91cd..30f6748f 100644
--- a/lib/interfaces/qunit.js
+++ b/lib/interfaces/qunit.js
@@ -4,7 +4,8 @@
*/
var Suite = require('../suite')
- , Test = require('../test');
+ , Test = require('../test')
+ , utils = require('../utils');
/**
* QUnit-style interface:
@@ -106,7 +107,8 @@ module.exports = function(suite){
context.test.only = function(title, fn){
var test = context.test(title, fn);
- mocha.grep(test.fullTitle());
+ var reString = '^' + utils.escapeRegexp(test.fullTitle()) + '$';
+ mocha.grep(new RegExp(reString));
};
/**
diff --git a/lib/interfaces/tdd.js b/lib/interfaces/tdd.js
index 7bf838b3..da5ca2c5 100644
--- a/lib/interfaces/tdd.js
+++ b/lib/interfaces/tdd.js
@@ -4,7 +4,8 @@
*/
var Suite = require('../suite')
- , Test = require('../test');
+ , Test = require('../test')
+ , utils = require('../utils');;
/**
* TDD-style interface:
@@ -122,7 +123,8 @@ module.exports = function(suite){
context.test.only = function(title, fn){
var test = context.test(title, fn);
- mocha.grep(test.fullTitle());
+ var reString = '^' + utils.escapeRegexp(test.fullTitle()) + '$';
+ mocha.grep(new RegExp(reString));
};
/**
diff --git a/test/acceptance/misc/only/bdd.js b/test/acceptance/misc/only/bdd.js
new file mode 100644
index 00000000..d2f1e7c7
--- /dev/null
+++ b/test/acceptance/misc/only/bdd.js
@@ -0,0 +1,15 @@
+
+describe('should only run .only test in this bdd suite', function() {
+ it('should not run this test', function() {
+ var zero = 0;
+ zero.should.equal(1, 'this test should have been skipped');
+ });
+ it.only('should run this test', function() {
+ var zero = 0;
+ zero.should.equal(0, 'this .only test should run');
+ });
+ it('should run this test, not (including a subset of the .only test title)', function() {
+ var zero = 0;
+ zero.should.equal(1, 'this test should have been skipped');
+ });
+});
\ No newline at end of file
diff --git a/test/acceptance/misc/only/qunit.js b/test/acceptance/misc/only/qunit.js
new file mode 100644
index 00000000..944214d0
--- /dev/null
+++ b/test/acceptance/misc/only/qunit.js
@@ -0,0 +1,16 @@
+
+function ok(expr, msg) {
+ if (!expr) throw new Error(msg);
+}
+
+suite('should only run .only test in this qunit suite');
+
+test('should not run this test', function() {
+ ok(0 === 1, 'this test should have been skipped');
+});
+test.only('should run this test', function() {
+ ok(0 === 0, 'this .only test should run');
+});
+test('should run this test, not (including a subset of the .only test title)', function() {
+ ok(0 === 1, 'this test should have been skipped');
+});
\ No newline at end of file
diff --git a/test/acceptance/misc/only/tdd.js b/test/acceptance/misc/only/tdd.js
new file mode 100644
index 00000000..85e247f0
--- /dev/null
+++ b/test/acceptance/misc/only/tdd.js
@@ -0,0 +1,15 @@
+
+suite('should only run .only test in this tdd suite', function() {
+ test('should not run this test', function() {
+ var zero = 0;
+ zero.should.equal(1, 'this test should have been skipped');
+ });
+ test.only('should run this test', function() {
+ var zero = 0;
+ zero.should.equal(0, 'this .only test should run');
+ });
+ test('should run this test, not (including a subset of the .only test title)', function() {
+ var zero = 0;
+ zero.should.equal(1, 'this test should have been skipped');
+ });
+});
\ No newline at end of file