茉莉花,关掉默认记者

时间:2015-07-06 12:47:38

标签: javascript jasmine

我有自定义记者的Jasmine

var myReporter = {
  jasmineStarted: function(suiteInfo) {

    console.log('Running suite with ' + suiteInfo.totalSpecsDefined);
  },

  suiteStarted: function(result) {

    console.log('Suite started: ' + result.description + ' whose full description is: ' + result.fullName);
  },

  specStarted: function(result) {

    console.log('Spec started: ' + result.description + ' whose full description is: ' + result.fullName);
  },

  specDone: function(result) {

    console.log('Spec: ' + result.description + ' was ' + result.status);
    for(var i = 0; i < result.failedExpectations.length; i++) {


      console.log('Failure: ' + result.failedExpectations[i].message);
      console.log(result.failedExpectations[i].stack);
    }
  },

  suiteDone: function(result) {

    console.log('Suite: ' + result.description + ' was ' + result.status);
    for(var i = 0; i < result.failedExpectations.length; i++) {

      console.log('AfterAll ' + result.failedExpectations[i].message);
      console.log(result.failedExpectations[i].stack);
    }
  },
  jasmineDone: function() {
    console.log('Finished suite');
  }
};

jasmine.getEnv().addReporter(myReporter);

describe('Top Level suite', function() {
  it('spec', function() {
    expect(1).toBe(1);
  });

  describe('Nested suite', function() {
    it('nested spec', function() {
      expect(true).toBe(true);
    });
  });
});

直播:http://jsfiddle.net/wLnmbh88/

现在结果显示在html和控制台上,但我需要在html中关闭它。如何仅在控制台中显示测试结果? 代码示例来自jasmine文档。

2 个答案:

答案 0 :(得分:8)

无需移除jasmine-html.js并摆弄自定义boot.js。在添加您选择的记者之前,请删除所有记者:

var jasmineReporters = require('jasmine-reporters');
jasmine.getEnv().clearReporters();
jasmine.getEnv().addReporter(new jasmineReporters.TapReporter());

答案 1 :(得分:-1)

默认HTML记者添加到文件<index enabled="true"> jasmine-html.js in your JSFiddle中。

boot.jssource) - 实际上是记者本身,通过Jasmine报告API实现; jasmine-html.jssource) - 一个文件,为Jasmine配置环境并执行它。此外,它还会激活默认的HTML报告器,因为这两个文件都被认为是Jasmine的核心,因此通过将它们全部包含在内,您将拥有基本的Jasmine安装。

在您的情况下,您希望实现自定义报告者+您希望摆脱默认的HTML报告者,因此您应该删除boot.js并将jasmine-html.js替换为自定义报告者。 Jasmine文档中有一个关于configuring custom bootannotated source的文章。

没有启用任何记者的Jasmine 2.x的基本自定义启动脚本应该看起来像这样:

boot.js

确保在(function() { window.jasmine = jasmineRequire.core(jasmineRequire); var env = jasmine.getEnv(); var jasmineInterface = jasmineRequire.interface(jasmine, env); extend(window, jasmineInterface); window.setTimeout = window.setTimeout; window.setInterval = window.setInterval; window.clearTimeout = window.clearTimeout; window.clearInterval = window.clearInterval; var currentWindowOnload = window.onload; window.onload = function() { if (currentWindowOnload) { currentWindowOnload(); } env.execute(); }; function extend(destination, source) { for (var property in source) destination[property] = source[property]; return destination; } }()); 之后包含此启动脚本。

设置好引导后,您可以照常注册记者:

jasmine.js

甚至更好 - 将它添加到启动脚本中。

检查JSFiddle:http://jsfiddle.net/wLnmbh88/2/

相关问题