Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Added markdown reporter (github flavour)

TJ Holowaychuk1951c39ef816
diff --git a/bin/_mocha b/bin/_mocha
index ae28dd70..a499e5fa 100755
--- a/bin/_mocha
+++ b/bin/_mocha
@@ -77,6 +77,7 @@ program.on('reporters', function(){
console.log(' html-cov - HTML test coverage');
console.log(' json-cov - JSON test coverage');
console.log(' json-stream - newline delimited json events');
+ console.log(' markdown - markdown documentation (github flavour)');
console.log();
process.exit();
});
diff --git a/lib/reporters/markdown.js b/lib/reporters/markdown.js
new file mode 100644
index 00000000..a55721b8
--- /dev/null
+++ b/lib/reporters/markdown.js
@@ -0,0 +1,68 @@
+
+/**
+ * Module dependencies.
+ */
+
+var Base = require('./base')
+ , utils = require('../utils');
+
+/**
+ * Expose `Markdown`.
+ */
+
+exports = module.exports = Markdown;
+
+/**
+ * Initialize a new `Markdown` reporter.
+ *
+ * @param {Runner} runner
+ * @api public
+ */
+
+function Markdown(runner) {
+ Base.call(this, runner);
+
+ var self = this
+ , stats = this.stats
+ , total = runner.total
+ , level = 0;
+
+ function title(str) {
+ return Array(level).join('#') + ' ' + str;
+ }
+
+ runner.on('suite', function(suite){
+ ++level;
+ process.stdout.write(title(suite.title) + '\n');
+ });
+
+ runner.on('suite end', function(suite){
+ --level;
+ });
+
+ runner.on('pass', function(test){
+ var code = utils.escape(clean(test.fn.toString()));
+ process.stdout.write('\n ' + test.title + '.\n');
+ process.stdout.write('\n```js');
+ process.stdout.write(code + '\n');
+ process.stdout.write('```\n\n');
+ });
+}
+
+/**
+ * Strip the function definition from `str`,
+ * and re-indent for pre whitespace.
+ */
+
+function clean(str) {
+ str = str
+ .replace(/^function *\(.*\) *{/, '')
+ .replace(/\s+\}$/, '');
+
+ var spaces = str.match(/^\n?( *)/)[1].length
+ , re = new RegExp('^ {' + spaces + '}', 'gm');
+
+ str = str.replace(re, '');
+
+ return str;
+}
\ No newline at end of file