Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Added doc reporter. Closes #33

Tj Holowaychukf5a994143855
diff --git a/lib/reporters/doc.js b/lib/reporters/doc.js
new file mode 100644
index 00000000..f37217cd
--- /dev/null
+++ b/lib/reporters/doc.js
@@ -0,0 +1,69 @@
+
+/**
+ * Module dependencies.
+ */
+
+var Base = require('./base');
+
+/**
+ * Expose `Doc`.
+ */
+
+exports = module.exports = Doc;
+
+/**
+ * Initialize a new `Doc` reporter.
+ *
+ * @param {Runner} runner
+ * @api public
+ */
+
+function Doc(runner) {
+ Base.call(this, runner);
+
+ var self = this
+ , stats = this.stats
+ , total = runner.total
+ , indents = 2;
+
+ function indent() {
+ return Array(indents).join(' ');
+ }
+
+ runner.on('suite', function(suite){
+ if (suite.root) return;
+ ++indents;
+ console.log('%s<section class="suite">', indent());
+ ++indents;
+ console.log('%s<h1>%s</h1>', indent(), suite.title);
+ console.log('%s<dl>', indent());
+ });
+
+ runner.on('suite end', function(suite){
+ if (suite.root) return;
+ console.log('%s</dl>', indent());
+ --indents;
+ console.log('%s</section>', indent());
+ --indents;
+ });
+
+ runner.on('pass', function(test){
+ console.log('%s<dt>%s</dt>', indent(), test.title);
+ var code = strip(test.fn.toString());
+ console.log('%s<dd><pre><code>%s</code></pre></dd>', indent(), code);
+ });
+
+ runner.on('end', function(){
+ process.exit(stats.failures);
+ });
+}
+
+/**
+ * Strip the function definition from `str`.
+ */
+
+function strip(str) {
+ return str
+ .replace(/^function *\(.*\) *{\s*/, '')
+ .replace(/\s+\}$/, '');
+}
\ No newline at end of file
diff --git a/lib/reporters/index.js b/lib/reporters/index.js
index 40835c7a..e6a8a6c8 100644
--- a/lib/reporters/index.js
+++ b/lib/reporters/index.js
@@ -1,6 +1,7 @@
exports.Base = require('./base');
exports.Dot = require('./dot');
+exports.Doc = require('./doc');
exports.TAP = require('./tap');
exports.JSON = require('./json');
exports.List = require('./list');
diff --git a/lib/suite.js b/lib/suite.js
index 368f9b52..719beec2 100644
--- a/lib/suite.js
+++ b/lib/suite.js
@@ -54,6 +54,7 @@ function Suite(title) {
this.beforeEachCallbacks = [];
this.afterAllCallbacks = [];
this.afterEachCallbacks = [];
+ this.root = !title;
}
/**
diff --git a/test/interfaces/bdd.js b/test/interfaces/bdd.js
index 80f9817f..9e95817d 100644
--- a/test/interfaces/bdd.js
+++ b/test/interfaces/bdd.js
@@ -13,3 +13,13 @@ describe('Array', function(){
})
})
})
+
+describe('Array', function(){
+ describe('#pop()', function(){
+ it('should remove and return the last value', function(){
+ var arr = [1,2,3];
+ arr.pop().should.equal(3);
+ arr.should.eql([1,2]);
+ })
+ })
+})
\ No newline at end of file