summaryrefslogtreecommitdiff
path: root/index.js
blob: 8f43fa4e04e19a8cb0909e8f09db02112bbb2f27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2016 Hewlett-Packard Development Company, L.P.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
'use strict';

var fs = require('fs');
var subunit = require('subunit-js');

function SubunitReporter(helper, logger, config) {
  config = config || {};
  var outputFile = config.outputFile || 'karma.subunit';
  var tags = config.tags || [];
  var slug = config.slug || false;
  var separator = config.separator || '.';

  function normalize(parts) {
    if (slug) {
      return parts.map(function(part) {
        return part.replace(/\s+/g, '_');
      });
    } else {
      return parts;
    }
  }

  var normalizeFunction = config.normalize || normalize;

  var startTime, stream, file;

  this.onRunStart = function(browsers) {
    startTime = new Date().getTime();

    stream = new subunit.ObjectToSubunitStream();
    file = fs.createWriteStream(outputFile);
    stream.pipe(file);
  };

  this.onSpecComplete = function(browser, result) {
    var duration = result.time - startTime;

    var parts = result.suite.slice();
    parts.push(result.description);

    var testId = normalizeFunction(parts).join(separator);

    var status;
    if (result.success) {
      status = 'success';
    } else if (result.skipped) {
      status = 'skip';
    } else {
      status = 'fail';
    }

    var testTags = tags.slice();
    testTags.push('browser-' + browser.id);
    testTags.push('spec-' + result.id.substring(4));

    stream.write({
      testId: testId,
      status: 'inprogress',
      timestamp: new Date(startTime),
      tags: testTags
    });

    stream.write({
      testId: testId,
      status: status,
      timestamp: new Date(startTime + result.time),
      tags: testTags,
      _packet: {
        flags: { runnable: true }
      }
    });

    startTime += result.time;
  };

  this.onRunComplete = function() {
    stream.end();
  };
}

SubunitReporter.$inject = ['helper', 'logger', 'config.subunitReporter'];

module.exports = {
  'reporter:subunit': ['type', SubunitReporter]
};