1

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.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
user70192
  • 13,786
  • 51
  • 160
  • 240
  • which version of jasmine? did you read how the docs? – Daniel A. White Sep 06 '15 at 12:19
  • @DanielA.White I did read the docs. That's why I'm super confused that its not working. In my package.json file I have `"gulp-jasmine": "^2.0.1"` – user70192 Sep 06 '15 at 12:57
  • @DanielA.White I was basing my syntax off of what is shown here: https://github.com/sindresorhus/gulp-jasmine – user70192 Sep 06 '15 at 12:58
  • just to be sure, did you export myCustomReporter in the first snippet? – Tamas Hegedus Sep 06 '15 at 13:08
  • @hege_hegedus - I did not do an export. When I added the `module.exports = myCustomReporter;`, the reporter ran. However, it is still not showing the default reporter. – user70192 Sep 06 '15 at 13:33
  • @hege_hegedus - In addition, it still spits out that concerning "[09:36:58] 'test' errored after 5.27 s [09:36:58] Error in plugin 'gulp-jasmine' Message: Tests failed " part – user70192 Sep 06 '15 at 13:38
  • @user70192 posted an answer about the default reporter. Try to run your task with --verbose to get more information on the `'test' errored` thing – Tamas Hegedus Sep 06 '15 at 14:04

1 Answers1

4

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 }))
    ;
});
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97