From 52dcc3ffa85464b035ef637d1adea88ac4883872 Mon Sep 17 00:00:00 2001 From: David da Silva Date: Wed, 25 May 2016 10:38:26 +0200 Subject: [PATCH] Rebuild mocha.js --- mocha.js | 152 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 83 insertions(+), 69 deletions(-) diff --git a/mocha.js b/mocha.js index d2fceb8b90..af90adac66 100644 --- a/mocha.js +++ b/mocha.js @@ -2559,7 +2559,8 @@ function HTML(runner) { } // pass toggle - on(passesLink, 'click', function() { + on(passesLink, 'click', function(evt) { + evt.preventDefault(); unhide(); var name = (/pass/).test(report.className) ? '' : ' pass'; report.className = report.className.replace(/fail|pass/g, '') + name; @@ -2569,7 +2570,8 @@ function HTML(runner) { }); // failure toggle - on(failuresLink, 'click', function() { + on(failuresLink, 'click', function(evt) { + evt.preventDefault(); unhide(); var name = (/fail/).test(report.className) ? '' : ' fail'; report.className = report.className.replace(/fail|pass/g, '') + name; @@ -2607,88 +2609,82 @@ function HTML(runner) { stack.shift(); }); - runner.on('fail', function(test) { - // For type = 'test' its possible that the test failed due to multiple - // done() calls. So report the issue here. - if (test.type === 'hook' - || test.type === 'test') { - runner.emit('test end', test); - } + runner.on('pass', function(test) { + var url = self.testURL(test); + var markup = '
  • %e%ems ' + + '

  • '; + var el = fragment(markup, test.speed, test.title, test.duration, url); + self.addCodeToggle(el, test.body); + appendToStack(el); + updateStats(); }); - runner.on('test end', function(test) { - // TODO: add to stats - var percent = stats.tests / this.total * 100 | 0; - if (progress) { - progress.update(percent).draw(ctx); - } - - // update stats - var ms = new Date() - stats.start; - text(passes, stats.passes); - text(failures, stats.failures); - text(duration, (ms / 1000).toFixed(2)); - - // test - var el; - if (test.state === 'passed') { - var url = self.testURL(test); - el = fragment('
  • %e%ems

  • ', test.speed, test.title, test.duration, url); - } else if (test.isPending()) { - el = fragment('
  • %e

  • ', test.title); - } else { - el = fragment('
  • %e

  • ', test.title, self.testURL(test)); - var stackString; // Note: Includes leading newline - var message = test.err.toString(); - - // <=IE7 stringifies to [Object Error]. Since it can be overloaded, we - // check for the result of the stringifying. - if (message === '[object Error]') { - message = test.err.message; - } - - if (test.err.stack) { - var indexOfMessage = test.err.stack.indexOf(test.err.message); - if (indexOfMessage === -1) { - stackString = test.err.stack; - } else { - stackString = test.err.stack.substr(test.err.message.length + indexOfMessage); - } - } else if (test.err.sourceURL && test.err.line !== undefined) { - // Safari doesn't give you a stack. Let's at least provide a source line. - stackString = '\n(' + test.err.sourceURL + ':' + test.err.line + ')'; - } + runner.on('fail', function(test) { + var el = fragment('
  • %e

  • ', + test.title, self.testURL(test)); + var stackString; // Note: Includes leading newline + var message = test.err.toString(); - stackString = stackString || ''; + // <=IE7 stringifies to [Object Error]. Since it can be overloaded, we + // check for the result of the stringifying. + if (message === '[object Error]') { + message = test.err.message; + } - if (test.err.htmlMessage && stackString) { - el.appendChild(fragment('
    %s\n
    %e
    ', test.err.htmlMessage, stackString)); - } else if (test.err.htmlMessage) { - el.appendChild(fragment('
    %s
    ', test.err.htmlMessage)); + if (test.err.stack) { + var indexOfMessage = test.err.stack.indexOf(test.err.message); + if (indexOfMessage === -1) { + stackString = test.err.stack; } else { - el.appendChild(fragment('
    %e%e
    ', message, stackString)); + stackString = test.err.stack.substr(test.err.message.length + indexOfMessage); } + } else if (test.err.sourceURL && test.err.line !== undefined) { + // Safari doesn't give you a stack. Let's at least provide a source line. + stackString = '\n(' + test.err.sourceURL + ':' + test.err.line + ')'; } - // toggle code - // TODO: defer - if (!test.isPending()) { - var h2 = el.getElementsByTagName('h2')[0]; + stackString = stackString || ''; - on(h2, 'click', function() { - pre.style.display = pre.style.display === 'none' ? 'block' : 'none'; - }); - - var pre = fragment('
    %e
    ', utils.clean(test.body)); - el.appendChild(pre); - pre.style.display = 'none'; + if (test.err.htmlMessage && stackString) { + el.appendChild(fragment('
    %s\n
    %e
    ', + test.err.htmlMessage, stackString)); + } else if (test.err.htmlMessage) { + el.appendChild(fragment('
    %s
    ', test.err.htmlMessage)); + } else { + el.appendChild(fragment('
    %e%e
    ', message, stackString)); } + self.addCodeToggle(el, test.body); + appendToStack(el); + updateStats(); + }); + + runner.on('pending', function(test) { + var el = fragment('
  • %e

  • ', test.title); + appendToStack(el); + updateStats(); + }); + + function appendToStack(el) { // Don't call .appendChild if #mocha-report was already .shift()'ed off the stack. if (stack[0]) { stack[0].appendChild(el); } - }); + } + + function updateStats() { + // TODO: add to stats + var percent = stats.tests / this.total * 100 | 0; + if (progress) { + progress.update(percent).draw(ctx); + } + + // update stats + var ms = new Date() - stats.start; + text(passes, stats.passes); + text(failures, stats.failures); + text(duration, (ms / 1000).toFixed(2)); + } } /** @@ -2726,6 +2722,24 @@ HTML.prototype.testURL = function(test) { return makeUrl(test.fullTitle()); }; +/** + * Adds code toggle functionality for the provided test's list element. + * + * @param {HTMLLIElement} el + * @param {string} contents + */ +HTML.prototype.addCodeToggle = function(el, contents) { + var h2 = el.getElementsByTagName('h2')[0]; + + on(h2, 'click', function() { + pre.style.display = pre.style.display === 'none' ? 'block' : 'none'; + }); + + var pre = fragment('
    %e
    ', utils.clean(contents)); + el.appendChild(pre); + pre.style.display = 'none'; +}; + /** * Display error `msg`. *