简体   繁体   English

茉莉花-使用自定义记者

[英]Jasmine - Using a Custom Reporter

I am testing some JavaScript with Jasmine via Gulp . 我正在通过GulpJasmine测试一些JavaScript。 I want to create my own reporter. 我想创建自己的记者。 At this time, my reporter is as basic as it gets. 这时候,我的记者已经很基本了。 It looks like this: 看起来像这样:

'use strict';

var myCustomReporter = {
    jasmineStarted: function(suiteInfo) {
        console.log('Running suite with ' + suiteInfo.totalSpecsDefined);
        console.log('Reporting via MyCustomReporter');      
    },

    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) {
    },

    suiteDone: function(result) {
    },

    jasmineDone: function() {
        console.log('Finished suite');
    }   
};

The above code is essentially the example custom reporter provided by Jasmine. 上面的代码实质上是Jasmine提供的示例自定义报告程序 My challenge is, I cannot figure out how to get Jasmine to actually use it. 我的挑战是,我不知道如何让茉莉花真正使用它。 Some how, I'm adding it incorrectly. 某些方法,我加错了。 I'm adding it like this: 我正在这样添加它:

 gulp.task('test', function() {
    // Load the reporters to use with Jasmine
    var myReporter = require('./reporters/myCustomReporter');   
    var reporters = [
        myReporter
    ];

    return gulp.src(input.tests)
        .pipe(jasmine({ reporter: reporters }))
    ;
 });

When I execute the test task via Gulp, I get the following output: 通过Gulp执行test任务时,得到以下输出:

[08:04:15] Using gulpfile ~/MyProject/gulpfile.js
[08:04:15] Starting 'test'...
[08:04:20] 'test' errored after 5.25 s
[08:04:20] Error in plugin 'gulp-jasmine'
Message:
    Tests failed

If I do NOT pass along { reporter: reporters } in my call to Jasmine, my tests run just fine. 如果我在给Jasmine打电话时没有通过{ reporter: reporters } ,我的测试就可以正常进行。 I'm trying to learn how to a) Add my reporter and b) Still use the default reporter. 我正在尝试学习如何a)添加我的记者和b)仍使用默认记者。 Essentially, I'm trying to figure out how to send the results to multiple reporters. 本质上,我试图弄清楚如何将结果发送给多个记者。 I think my approach is correct. 我认为我的方法是正确的。 Clearly, the results are showing I'm wrong though. 显然,结果表明我错了。

First of all make sure that you export the custom reporter like module.exports = myCustomReporter; 首先,请确保您导出自定义报告程序,例如module.exports = myCustomReporter; .

Based on the source of gulp-jasmine, the default reporter is not exposed. 基于gulp-jasmine的来源,默认的报告程序未公开。 The relevant code: 相关代码:

var Reporter = require('jasmine-terminal-reporter');
...
module.exports = function(options) {
  ...
  var color = process.argv.indexOf('--no-color') === -1;
  var reporter = options.reporter;

  if (reporter) {
    (Array.isArray(reporter) ? reporter : [reporter]).forEach(function (el) {
      jasmine.addReporter(el);
    });
  } else {
    jasmine.addReporter(new Reporter({
      isVerbose: options.verbose,
      showColors: color,
      includeStackTrace: options.includeStackTrace
    }));
  }
  ...
};

So you can add the default reporter like this: 因此,您可以像这样添加默认报告器:

gulp.task('test', function() {
    // Load the reporters to use with Jasmine
    var myReporter = require('./reporters/myCustomReporter');   

    var Reporter = require('jasmine-terminal-reporter');
    var defaultReporter = new Reporter({
      isVerbose: false,
      showColors: true,
      includeStackTrace: false
    });

    var reporters = [
        defaultReporter,
        myReporter
    ];

    return gulp.src(input.tests)
        .pipe(jasmine({ reporter: reporters }))
    ;
});

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

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