为什么我的摩卡记者双重报告测试?
Why is my Mocha reporter double reporting tests?
我有一个自定义报告器 doc-output.js
从 doc 报告器修改而来。
/**
* 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>');
});
}
我运行使用代码以编程方式安装 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);
});
});
当我运行它时,输出变量打印在html中。它报告自己两次。
<div id="mocha">
<
<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>
><!--<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>
</div>
此外,它会在开头和结尾插入随机的“<”和“>”。我不确定这是否是相关问题。
为什么我的测试报告两次?我没看出记者有什么不妥。
哦,还有,我在使用 jade 的模板引擎中调用它。像这样,
#{result}
所以我想通了。
< 和 > 来自调用 #{result} 而不是 !{result}
双重报告来自对 process.stdout.write 的监视。我正在复制 std 并同时进行间谍活动,这导致它报告双重。
我有一个自定义报告器 doc-output.js
从 doc 报告器修改而来。
/**
* 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>');
});
}
我运行使用代码以编程方式安装 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);
});
});
当我运行它时,输出变量打印在html中。它报告自己两次。
<div id="mocha">
<
<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>
><!--<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>
</div>
此外,它会在开头和结尾插入随机的“<”和“>”。我不确定这是否是相关问题。
为什么我的测试报告两次?我没看出记者有什么不妥。
哦,还有,我在使用 jade 的模板引擎中调用它。像这样,
#{result}
所以我想通了。
< 和 > 来自调用 #{result} 而不是 !{result}
双重报告来自对 process.stdout.write 的监视。我正在复制 std 并同时进行间谍活动,这导致它报告双重。