Jasmine - Using a Custom Reporter

5.3k views Asked by At

I am testing some JavaScript with Jasmine via Gulp. 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. 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:

[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. I'm trying to learn how to a) Add my reporter and b) Still use the default reporter. 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.

1

There are 1 answers

0
Tamas Hegedus On BEST ANSWER

First of all make sure that you export the custom reporter like module.exports = myCustomReporter;.

Based on the source of gulp-jasmine, the default reporter is not exposed. 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 }))
    ;
});