utils.mjs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. function statusToIcon(status) {
  17. return {
  18. erroring: "🔥",
  19. failing: "❌",
  20. passing: "✔️️",
  21. }[status] || "?";
  22. }
  23. /**
  24. * Counts test statuses.
  25. *
  26. * @param {Object} testStatus
  27. * @param {Object} testResult
  28. */
  29. function handleTestResult(testStatus, testResult) {
  30. testStatus.allTestsPassing = testStatus.allTestsPassing && testResult.status === "passing";
  31. testStatus.counts[testResult.status] = (testStatus.counts[testResult.status] || 0) + 1;
  32. testStatus.counts.total += 1;
  33. }
  34. /**
  35. * Log each test result, count tests and failures.
  36. *
  37. * @param {Object} testStatus - object describing test run data
  38. * @param {Object[]} results - results from TestRegister
  39. */
  40. export function logTestReport(testStatus, results) {
  41. console.log("Tests completed.");
  42. results.forEach(r => handleTestResult(testStatus, r));
  43. console.log();
  44. for (const testStatusCount in testStatus.counts) {
  45. const count = testStatus.counts[testStatusCount];
  46. if (count > 0) {
  47. console.log(testStatusCount.toUpperCase() + "\t" + count);
  48. }
  49. }
  50. console.log();
  51. // Print error messages for tests that didn't pass
  52. results.filter(res => res.status !== "passing").forEach(testResult => {
  53. console.log([
  54. statusToIcon(testResult.status),
  55. testResult.test.name
  56. ].join(" "));
  57. if (testResult.output) {
  58. console.log(
  59. testResult.output
  60. .trim()
  61. .replace(/^/, "\t")
  62. .replace(/\n/g, "\n\t")
  63. );
  64. }
  65. });
  66. console.log();
  67. process.exit(testStatus.allTestsPassing ? 0 : 1);
  68. }
  69. /**
  70. * Fail if the process takes longer than 60 seconds.
  71. */
  72. export function setLongTestFailure() {
  73. setTimeout(function() {
  74. console.log("Tests took longer than 60 seconds to run, returning.");
  75. process.exit(1);
  76. }, 60 * 1000);
  77. }