phantom-reporter.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* eslint-disable */
  2. 'use strict';
  3. var phantom = {};
  4. if (window._phantom) {
  5. console.log = function() {
  6. phantom.sendMessage('console', Array.prototype.slice.apply(arguments).join(', '));
  7. };
  8. }
  9. (function() {
  10. phantom.sendMessage = function() {
  11. var args = [].slice.call(arguments);
  12. var payload = stringify(args);
  13. if (window._phantom) {
  14. alert(payload);
  15. }
  16. };
  17. function PhantomReporter() {
  18. this.started = false;
  19. this.finished = false;
  20. this.suites_ = [];
  21. this.results_ = {};
  22. this.buffer = '';
  23. }
  24. PhantomReporter.prototype.jasmineStarted = function(metadata) {
  25. this.started = true;
  26. phantom.sendMessage('jasmine.jasmineStarted', metadata);
  27. };
  28. PhantomReporter.prototype.specStarted = function(specMetadata) {
  29. specMetadata.startTime = Date.now();
  30. phantom.sendMessage('jasmine.specStarted', specMetadata);
  31. };
  32. PhantomReporter.prototype.suiteStarted = function(suiteMetadata) {
  33. suiteMetadata.startTime = Date.now();
  34. phantom.sendMessage('jasmine.suiteStarted', suiteMetadata);
  35. };
  36. PhantomReporter.prototype.jasmineDone = function() {
  37. this.finished = true;
  38. phantom.sendMessage('jasmine.jasmineDone');
  39. };
  40. PhantomReporter.prototype.suiteDone = function(suiteMetadata) {
  41. suiteMetadata.duration = Date.now() - suiteMetadata.startTime;
  42. phantom.sendMessage('jasmine.suiteDone', suiteMetadata);
  43. };
  44. PhantomReporter.prototype.specDone = function(specMetadata) {
  45. specMetadata.duration = Date.now() - specMetadata.startTime;
  46. this.results_[specMetadata.id] = specMetadata;
  47. phantom.sendMessage('jasmine.specDone', specMetadata);
  48. };
  49. function stringify(obj) {
  50. if (typeof obj !== 'object') {
  51. return obj;
  52. }
  53. var cache = [], keyMap = [];
  54. var string = JSON.stringify(obj, function(key, value) {
  55. // Let json stringify falsy values
  56. if (!value) {
  57. return value;
  58. }
  59. try {
  60. // If we're a node
  61. if (typeof Node !== 'undefined' && value instanceof Node) {
  62. return '[ Node ]';
  63. }
  64. // jasmine-given has expectations on Specs. We intercept to return a
  65. // String to avoid stringifying the entire Jasmine environment, which
  66. // results in exponential string growth
  67. if (value instanceof jasmine.Spec) {
  68. return '[ Spec: ' + value.description + ' ]';
  69. }
  70. // If we're a window (logic stolen from jQuery)
  71. if (value.window && value.window === value.window.window) {
  72. return '[ Window ]';
  73. }
  74. // Simple function reporting
  75. if (typeof value === 'function') {
  76. return '[ Function ]';
  77. }
  78. if (typeof value === 'object' && value !== null) {
  79. var index = cache.indexOf(value);
  80. if (index !== -1) {
  81. // If we have it in cache, report the circle with the key we first found it in
  82. return '[ Circular {' + (keyMap[index] || 'root') + '} ]';
  83. }
  84. cache.push(value);
  85. keyMap.push(key);
  86. }
  87. } catch (e) {
  88. return '[Object]';
  89. }
  90. return value;
  91. });
  92. return string;
  93. }
  94. jasmine.getEnv().addReporter(new PhantomReporter());
  95. }());