utils.mjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // Define global environment functions
  11. global.ENVIRONMENT_IS_WORKER = function() {
  12. return typeof importScripts === "function";
  13. };
  14. global.ENVIRONMENT_IS_NODE = function() {
  15. return typeof process === "object" && typeof require === "function";
  16. };
  17. global.ENVIRONMENT_IS_WEB = function() {
  18. return typeof window === "object";
  19. };
  20. /**
  21. * Helper function to convert a status to an icon.
  22. *
  23. * @param {string} status
  24. * @returns {string}
  25. */
  26. const statusToIcon = function statusToIcon(status) {
  27. const icons = {
  28. erroring: "🔥",
  29. failing: "❌",
  30. passing: "✔️️",
  31. };
  32. return icons[status] || "?";
  33. };
  34. /**
  35. * Displays a given test result in the console.
  36. * Counts test statuses.
  37. *
  38. * @param {Object} testStatusCounts
  39. * @param {Object} testResult
  40. */
  41. function handleTestResult(testStatus, testResult) {
  42. testStatus.allTestsPassing = testStatus.allTestsPassing && testResult.status === "passing";
  43. const newCount = (testStatus.counts[testResult.status] || 0) + 1;
  44. testStatus.counts[testResult.status] = newCount;
  45. testStatus.counts.total += 1;
  46. console.log([
  47. statusToIcon(testResult.status),
  48. testResult.test.name
  49. ].join(" "));
  50. if (testResult.output) {
  51. console.log(
  52. testResult.output
  53. .trim()
  54. .replace(/^/, "\t")
  55. .replace(/\n/g, "\n\t")
  56. );
  57. }
  58. }
  59. /**
  60. * Log each test result, count tests and failures. Log test suite run duration.
  61. *
  62. * @param {Object} testStatus - object describing test run data
  63. * @param {Object[]} results - results from TestRegister
  64. */
  65. export function logTestReport(testStatus, results) {
  66. results.forEach(r => handleTestResult(testStatus, r));
  67. console.log("\n");
  68. for (const testStatusCount in testStatus.counts) {
  69. const count = testStatus.counts[testStatusCount];
  70. if (count > 0) {
  71. console.log(testStatusCount.toUpperCase(), count);
  72. }
  73. }
  74. if (!testStatus.allTestsPassing) {
  75. console.log("\nFailing tests:\n");
  76. results.filter(r => r.status !== "passing").forEach(handleTestResult);
  77. }
  78. process.exit(testStatus.allTestsPassing ? 0 : 1);
  79. }
  80. /**
  81. * Fail if the process takes longer than 60 seconds.
  82. */
  83. export function setLongTestFailure() {
  84. setTimeout(function() {
  85. console.log("Tests took longer than 60 seconds to run, returning.");
  86. process.exit(1);
  87. }, 60 * 1000);
  88. }