Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Added qunit-inspired interface

Tj Holowaychukc493d449f7c9
diff --git a/Makefile b/Makefile
index 87df63ce..e5abe229 100644
--- a/Makefile
+++ b/Makefile
@@ -23,7 +23,7 @@ clean:
test: test-unit
-test-all: test-bdd test-tdd test-exports test-unit test-grep
+test-all: test-bdd test-tdd test-qunit test-exports test-unit test-grep
test-unit:
@./bin/mocha \
@@ -41,6 +41,12 @@ test-tdd:
--ui tdd \
test/interfaces/tdd
+test-qunit:
+ @./bin/mocha \
+ --reporter $(REPORTER) \
+ --ui qunit \
+ test/interfaces/qunit
+
test-exports:
@./bin/mocha \
--reporter $(REPORTER) \
@@ -82,4 +88,4 @@ tm:
mkdir -p $(TM_DEST)/$(TM_BUNDLE)
cp -fr editors/$(TM_BUNDLE) $(TM_DEST)/$(TM_BUNDLE)
-.PHONY: watch test test-all test-bdd test-tdd test-exports test-unit non-tty test-grep tm clean
+.PHONY: watch test test-all test-bdd test-tdd test-qunit test-exports test-unit non-tty test-grep tm clean
diff --git a/lib/interfaces/index.js b/lib/interfaces/index.js
index 0dc76674..f7b2655e 100644
--- a/lib/interfaces/index.js
+++ b/lib/interfaces/index.js
@@ -1,4 +1,5 @@
exports.bdd = require('./bdd');
exports.tdd = require('./tdd');
-exports.exports = require('./exports');
\ No newline at end of file
+exports.qunit = require('./qunit');
+exports.exports = require('./exports');
diff --git a/lib/interfaces/qunit.js b/lib/interfaces/qunit.js
new file mode 100644
index 00000000..cd03ab6b
--- /dev/null
+++ b/lib/interfaces/qunit.js
@@ -0,0 +1,91 @@
+
+/**
+ * Module dependencies.
+ */
+
+var Suite = require('../suite')
+ , Test = require('../test');
+
+/**
+ * QUnit-style interface:
+ *
+ * suite('Array');
+ *
+ * test('#length', function(){
+ * var arr = [1,2,3];
+ * ok(arr.length == 3);
+ * });
+ *
+ * test('#indexOf()', function(){
+ * var arr = [1,2,3];
+ * ok(arr.indexOf(1) == 0);
+ * ok(arr.indexOf(2) == 1);
+ * ok(arr.indexOf(3) == 2);
+ * });
+ *
+ * suite('String');
+ *
+ * test('#length', function(){
+ * ok('foo'.length == 3);
+ * });
+ *
+ */
+
+module.exports = function(suite){
+ var suites = [suite];
+
+ suite.on('pre-require', function(context){
+
+ /**
+ * Execute before running tests.
+ */
+
+ context.before = function(fn){
+ suites[0].beforeAll(fn);
+ };
+
+ /**
+ * Execute after running tests.
+ */
+
+ context.after = function(fn){
+ suites[0].afterAll(fn);
+ };
+
+ /**
+ * Execute before each test case.
+ */
+
+ context.beforeEach = function(fn){
+ suites[0].beforeEach(fn);
+ };
+
+ /**
+ * Execute after each test case.
+ */
+
+ context.afterEach = function(fn){
+ suites[0].afterEach(fn);
+ };
+
+ /**
+ * Describe a "suite" with the given `title`.
+ */
+
+ context.suite = function(title){
+ if (suites.length > 1) suites.shift();
+ var suite = Suite.create(suites[0], title);
+ suites.unshift(suite);
+ };
+
+ /**
+ * Describe a specification or test-case
+ * with the given `title` and callback `fn`
+ * acting as a thunk.
+ */
+
+ context.test = function(title, fn){
+ suites[0].addTest(new Test(title, fn));
+ };
+ });
+};
diff --git a/test/interfaces/qunit.js b/test/interfaces/qunit.js
new file mode 100644
index 00000000..dbdc5bb7
--- /dev/null
+++ b/test/interfaces/qunit.js
@@ -0,0 +1,24 @@
+
+function ok(expr, msg) {
+ if (!expr) throw new Error(msg);
+}
+
+suite('Array');
+
+test('#length', function(){
+ var arr = [1,2,3];
+ ok(arr.length == 3);
+});
+
+test('#indexOf()', function(){
+ var arr = [1,2,3];
+ ok(arr.indexOf(1) == 0);
+ ok(arr.indexOf(2) == 1);
+ ok(arr.indexOf(3) == 2);
+});
+
+suite('String');
+
+test('#length', function(){
+ ok('foo'.length == 3);
+});
\ No newline at end of file