A karma plugin to report Karma test results as a subunit stream
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2016 Hewlett-Packard Development Company, L.P.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. 'use strict';
  15. var fs = require('fs');
  16. var subunit = require('subunit-js');
  17. function SubunitReporter(helper, logger, config) {
  18. config = config || {};
  19. var outputFile = config.outputFile || 'karma.subunit';
  20. var tags = config.tags || [];
  21. var slug = config.slug || false;
  22. var separator = config.separator || '.';
  23. function normalize(parts) {
  24. if (slug) {
  25. return parts.map(function(part) {
  26. return part.replace(/\s+/g, '_');
  27. });
  28. } else {
  29. return parts;
  30. }
  31. }
  32. var normalizeFunction = config.normalize || normalize;
  33. var startTime, stream, file;
  34. this.onRunStart = function(browsers) {
  35. startTime = new Date().getTime();
  36. stream = new subunit.ObjectToSubunitStream();
  37. file = fs.createWriteStream(outputFile);
  38. stream.pipe(file);
  39. };
  40. this.onSpecComplete = function(browser, result) {
  41. var duration = result.time - startTime;
  42. var parts = result.suite.slice();
  43. parts.push(result.description);
  44. var testId = normalizeFunction(parts).join(separator);
  45. var status;
  46. if (result.success) {
  47. status = 'success';
  48. } else if (result.skipped) {
  49. status = 'skip';
  50. } else {
  51. status = 'fail';
  52. }
  53. var testTags = tags.slice();
  54. testTags.push('browser-' + browser.id);
  55. testTags.push('spec-' + result.id.substring(4));
  56. stream.write({
  57. testId: testId,
  58. status: 'inprogress',
  59. timestamp: new Date(startTime),
  60. tags: testTags
  61. });
  62. stream.write({
  63. testId: testId,
  64. status: status,
  65. timestamp: new Date(startTime + result.time),
  66. tags: testTags,
  67. _packet: {
  68. flags: { runnable: true }
  69. }
  70. });
  71. startTime += result.time;
  72. };
  73. this.onRunComplete = function() {
  74. stream.end();
  75. };
  76. }
  77. SubunitReporter.$inject = ['helper', 'logger', 'config.subunitReporter'];
  78. module.exports = {
  79. 'reporter:subunit': ['type', SubunitReporter]
  80. };