簡體   English   中英

茉莉花-使用自定義記者

[英]Jasmine - Using a Custom Reporter

我正在通過GulpJasmine測試一些JavaScript。 我想創建自己的記者。 這時候,我的記者已經很基本了。 看起來像這樣:

'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');
    }   
};

上面的代碼實質上是Jasmine提供的示例自定義報告程序 我的挑戰是,我不知道如何讓茉莉花真正使用它。 某些方法,我加錯了。 我正在這樣添加它:

 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 }))
    ;
 });

通過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

如果我在給Jasmine打電話時沒有通過{ reporter: reporters } ,我的測試就可以正常進行。 我正在嘗試學習如何a)添加我的記者和b)仍使用默認記者。 本質上,我試圖弄清楚如何將結果發送給多個記者。 我認為我的方法是正確的。 顯然,結果表明我錯了。

首先,請確保您導出自定義報告程序,例如module.exports = myCustomReporter;

基於gulp-jasmine的來源,默認的報告程序未公開。 相關代碼:

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
    }));
  }
  ...
};

因此,您可以像這樣添加默認報告器:

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