testreporter.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const TestReporter = function (browser) {
  2. function send(action, json) {
  3. return new Promise(resolve => {
  4. json.browser = browser;
  5. fetch(action, {
  6. method: "POST",
  7. headers: {
  8. "Content-Type": "application/json",
  9. },
  10. body: JSON.stringify(json),
  11. })
  12. .then(response => {
  13. // Retry until successful.
  14. if (!response.ok || response.status !== 200) {
  15. throw new Error(response.statusText);
  16. }
  17. resolve();
  18. })
  19. .catch(reason => {
  20. console.warn(`TestReporter - send failed (${action}): ${reason}`);
  21. resolve();
  22. send(action, json);
  23. });
  24. });
  25. }
  26. function sendInfo(message) {
  27. send("/info", { message });
  28. }
  29. function sendResult(status, description, error) {
  30. const message = {
  31. status,
  32. description,
  33. };
  34. if (error !== undefined) {
  35. message.error = error;
  36. }
  37. send("/submit_task_results", message);
  38. }
  39. function sendQuitRequest() {
  40. send(`/tellMeToQuit?browser=${escape(browser)}`, {});
  41. }
  42. this.now = function () {
  43. return Date.now();
  44. };
  45. this.jasmineStarted = function (suiteInfo) {
  46. this.runnerStartTime = this.now();
  47. const total = suiteInfo.totalSpecsDefined;
  48. const seed = suiteInfo.order.seed;
  49. sendInfo(`Started ${total} tests for ${browser} with seed ${seed}.`);
  50. };
  51. this.suiteStarted = function (result) {
  52. // Normally suite starts don't have to be reported because the individual
  53. // specs inside them are reported, but it can happen that the suite cannot
  54. // start, for instance due to an uncaught exception in `beforeEach`. This
  55. // is problematic because the specs inside the suite will never be found
  56. // and run, so if we don't report the suite start failure here it would be
  57. // ignored silently, leading to passing tests even though some did not run.
  58. if (result.failedExpectations.length > 0) {
  59. let failedMessages = "";
  60. for (const item of result.failedExpectations) {
  61. failedMessages += `${item.message} `;
  62. }
  63. sendResult("TEST-UNEXPECTED-FAIL", result.description, failedMessages);
  64. }
  65. };
  66. this.specStarted = function (result) {};
  67. this.specDone = function (result) {
  68. if (result.failedExpectations.length === 0) {
  69. sendResult("TEST-PASSED", result.description);
  70. } else {
  71. let failedMessages = "";
  72. for (const item of result.failedExpectations) {
  73. failedMessages += `${item.message} `;
  74. }
  75. sendResult("TEST-UNEXPECTED-FAIL", result.description, failedMessages);
  76. }
  77. };
  78. this.suiteDone = function (result) {};
  79. this.jasmineDone = function () {
  80. // Give the test runner some time process any queued requests.
  81. setTimeout(sendQuitRequest, 500);
  82. };
  83. };
  84. export { TestReporter };