简体   繁体   中英

Why is my Mocha reporter double reporting tests?

I have a custom reporter doc-output.js modified from the doc reporter.

 /** * Module dependencies. */ var Base = require('./base') , utils = require('../utils'); /** * 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('start', function() { console.log('<ul id="mocha-report">'); ++indents; }); runner.on('suite', function(suite){ if (suite.root) return; ++indents; console.log('%s<li class="suite">', indent()); ++indents; console.log('%s<h1>%s</h1>', indent(), utils.escape(suite.title)); console.log('%s<ul>', indent()); }); runner.on('suite end', function(suite){ if (suite.root) return; console.log('%s</ul>', indent()); --indents; console.log('%s</section>', indent()); --indents; }); runner.on('pass', function(test){ console.log('<li class="test pass fast">'); ++indents; console.log('%s <h2 id="pass">%s</h2>', indent(), utils.escape(test.title)); var code = utils.escape(utils.clean(test.fn.toString())); console.log('%s <pre style="display: none;"><code>%s</code></pre></li>', indent(), code); }); runner.on('fail', function(test, err){ console.log('<li class="test fail">'); ++indents; // console.log('%s::before', indents()); console.log('<h2 id="fail">%s</h2>', utils.escape(test.title)); console.log('%s <pre class="error">%s</pre>', indent(), utils.escape(err)); var code = utils.escape(utils.clean(test.fn.toString())); console.log('%s <pre style="display: block;"><code>%s</code></pre></li>', indent(), code); }); runner.on('end', function() { --indents; console.log('</ul>'); }); } 

I'm running Mocha programmatically with the code:

 var Mocha = require('mocha'); mocha = new Mocha({ reporter: 'doc-output', ui: 'bdd', quiet: true }); // a file with mocha tests in it mocha.addFile('./simp'); var write = process.stdout.write; var output = ""; process.stdout.write = function(str) { output += str; }; mocha.run(function(failures) { process.stdout.write = write; process.on('exit', function () { process.exit(failures); }); }); 

When I run it, the output variable is printed in html. It is reporting itself twice.

 <div id="mocha"> &lt; <ul id="mocha-report"> <li class="suite"> <h1>describe level 1</h1> <ul> <li class="suite"> <h1>describe level 2</h1> <ul> <li class="test pass fast"> <h2 id="pass">it first</h2> <pre style="display: none;"><code>('hello').should.be.a('string'); done();</code></pre> </li> <li class="test pass fast"> <h2 id="pass">it second</h2> <pre style="display: none;"><code>('hello').should.be.a('string'); done();</code></pre> </li> </ul> </li> <li class="suite"> <h1>second nested describe</h1> <ul> <li class="test pass fast"> <h2 id="pass">it</h2> <pre style="display: none;"><code>('hello').should.be.a('string'); done();</code></pre> </li> <li class="test fail"> <h2 id="fail">it should fail</h2> <pre class="error">AssertionError: expected 'hello' to be a number</pre> <pre style="display: block;"><code>('hello').should.be.a('number'); done();</code></pre> </li> </ul> </li> </ul> </li> </ul> &gt;<!--<ul id="mocha-report"--> <li class="suite"> <h1>describe level 1</h1> <ul> <li class="suite"> <h1>describe level 2</h1> <ul> <li class="test pass fast"> <h2 id="pass">it first</h2> <pre style="display: none;"><code>('hello').should.be.a('string'); done();</code></pre> </li> <li class="test pass fast"> <h2 id="pass">it second</h2> <pre style="display: none;"><code>('hello').should.be.a('string'); done();</code></pre> </li> </ul> </li> <li class="suite"> <h1>second nested describe</h1> <ul> <li class="test pass fast"> <h2 id="pass">it</h2> <pre style="display: none;"><code>('hello').should.be.a('string'); done();</code></pre> </li> <li class="test fail"> <h2 id="fail">it should fail</h2> <pre class="error">AssertionError: expected 'hello' to be a number</pre> <pre style="display: block;"><code>('hello').should.be.a('number'); done();</code></pre> </li> </ul> </li> </ul> &gt; </li> </div> 

Also, it inserts random "<" and ">" at the beginning and ends. I'm not sure if this is a related issue.

Why is my test reporting twice? I don't see anything wrong with the reporter.

Oh, and also, I'm calling this in a templating engine using jade. Like so,

#{result}

So I figured it out.

The < and > were coming from calling #{result} instead of !{result}

The double reporting was coming from spying on process.stdout.write. I was copying std out and spying at the same time, which cause it to report double.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM