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

时间:2015-06-05 18:52:52

标签: javascript html mocha

我有一位自定义记者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>');
  });
}
&#13;
&#13;
&#13;

我使用代码以编程方式运行Mocha:

&#13;
&#13;
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);
    });
});
&#13;
&#13;
&#13;

当我运行它时,输出变量以html格式打印。它报告了两次。

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

此外,它会随机插入&#34;&lt;&#34;和&#34;&gt;&#34;在开始和结束。我不确定这是否是一个相关问题。

为什么我的测试报告两次?我没有看到记者的任何问题。

哦,而且,我在使用玉的模板引擎中调用它。像这样,

#{result}

1 个答案:

答案 0 :(得分:0)

所以我想通了。

&lt;和&gt;来自调用#{result}而不是!{result}

双重报告来自对process.stdout.write的间谍活动。我正在同时复制std并进行间谍活动,这会导致报告双重。