utils.mjs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Utils for test suite
  3. *
  4. * @author d98762625@gmail.com
  5. * @author tlwr [toby@toby.codes]
  6. * @author n1474335 [n1474335@gmail.com]
  7. * @copyright Crown Copyright 2018
  8. * @license Apache-2.0
  9. */
  10. /**
  11. * Helper function to convert a status to an icon.
  12. *
  13. * @param {string} status
  14. * @returns {string}
  15. */
  16. const statusToIcon = function statusToIcon(status) {
  17. const icons = {
  18. erroring: "🔥",
  19. failing: "❌",
  20. passing: "✔️️",
  21. };
  22. return icons[status] || "?";
  23. };
  24. /**
  25. * Displays a given test result in the console.
  26. * Counts test statuses.
  27. *
  28. * @param {Object} testStatusCounts
  29. * @param {Object} testResult
  30. */
  31. function handleTestResult(testStatus, testResult) {
  32. testStatus.allTestsPassing = testStatus.allTestsPassing && testResult.status === "passing";
  33. const newCount = (testStatus.counts[testResult.status] || 0) + 1;
  34. testStatus.counts[testResult.status] = newCount;
  35. testStatus.counts.total += 1;
  36. console.log([
  37. statusToIcon(testResult.status),
  38. testResult.test.name
  39. ].join(" "));
  40. if (testResult.output) {
  41. console.log(
  42. testResult.output
  43. .trim()
  44. .replace(/^/, "\t")
  45. .replace(/\n/g, "\n\t")
  46. );
  47. }
  48. }
  49. /**
  50. * Log each test result, count tests and failures. Log test suite run duration.
  51. *
  52. * @param {Object} testStatus - object describing test run data
  53. * @param {Object[]} results - results from TestRegister
  54. */
  55. export function logTestReport(testStatus, results) {
  56. results.forEach(r => handleTestResult(testStatus, r));
  57. console.log("\n");
  58. for (const testStatusCount in testStatus.counts) {
  59. const count = testStatus.counts[testStatusCount];
  60. if (count > 0) {
  61. console.log(testStatusCount.toUpperCase(), count);
  62. }
  63. }
  64. process.exit(testStatus.allTestsPassing ? 0 : 1);
  65. }
  66. /**
  67. * Fail if the process takes longer than 60 seconds.
  68. */
  69. export function setLongTestFailure() {
  70. setTimeout(function() {
  71. console.log("Tests took longer than 60 seconds to run, returning.");
  72. process.exit(1);
  73. }, 60 * 1000);
  74. }