简体   繁体   English

为什么我的摩卡记者会重复报告测试?

[英]Why is my Mocha reporter double reporting tests?

I have a custom reporter doc-output.js modified from the doc reporter. 我有一个从文档报告程序修改的自定义报告程序doc-output.js

 /** * 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: 我正在使用代码以编程方式运行Mocha:

 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. 当我运行它时,输出变量以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. 哦,而且,我在使用jade的模板引擎中称呼它。 Like so, 像这样

#{result}

So I figured it out. 所以我想通了。

The < and > were coming from calling #{result} instead of !{result} <和>来自调用#{result}而不是!{result}

The double reporting was coming from spying on process.stdout.write. 双重报告来自于对process.stdout.write的监视。 I was copying std out and spying at the same time, which cause it to report double. 我正在同时复制和监视std,这导致它报告两次错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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