index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * TestRunner.js
  3. *
  4. * For running the tests in the test register.
  5. *
  6. * @author tlwr [toby@toby.codes]
  7. * @author n1474335 [n1474335@gmail.com]
  8. * @copyright Crown Copyright 2017
  9. * @license Apache-2.0
  10. */
  11. import "babel-polyfill";
  12. import TestRegister from "./TestRegister.js";
  13. import "./tests/operations/Base58.js";
  14. import "./tests/operations/ByteRepr.js";
  15. import "./tests/operations/Code.js";
  16. import "./tests/operations/Compress.js";
  17. import "./tests/operations/FlowControl.js";
  18. import "./tests/operations/Image.js";
  19. import "./tests/operations/MorseCode.js";
  20. import "./tests/operations/StrUtils.js";
  21. let allTestsPassing = true;
  22. const testStatusCounts = {
  23. total: 0,
  24. };
  25. /**
  26. * Helper function to convert a status to an icon.
  27. *
  28. * @param {string} status
  29. * @returns {string}
  30. */
  31. function statusToIcon(status) {
  32. const icons = {
  33. erroring: "🔥",
  34. failing: "❌",
  35. passing: "✔️️",
  36. };
  37. return icons[status] || "?";
  38. }
  39. /**
  40. * Displays a given test result in the console.
  41. *
  42. * @param {Object} testResult
  43. */
  44. function handleTestResult(testResult) {
  45. allTestsPassing = allTestsPassing && testResult.status === "passing";
  46. const newCount = (testStatusCounts[testResult.status] || 0) + 1;
  47. testStatusCounts[testResult.status] = newCount;
  48. testStatusCounts.total += 1;
  49. console.log([
  50. statusToIcon(testResult.status),
  51. testResult.test.name
  52. ].join(" "));
  53. if (testResult.output) {
  54. console.log(
  55. testResult.output
  56. .trim()
  57. .replace(/^/, "\t")
  58. .replace(/\n/g, "\n\t")
  59. );
  60. }
  61. }
  62. /**
  63. * Fail if the process takes longer than 10 seconds.
  64. */
  65. setTimeout(function() {
  66. console.log("Tests took longer than 10 seconds to run, returning.");
  67. process.exit(1);
  68. }, 1 * 1000);
  69. TestRegister.runTests()
  70. .then(function(results) {
  71. results.forEach(handleTestResult);
  72. console.log("\n");
  73. for (const testStatus in testStatusCounts) {
  74. const count = testStatusCounts[testStatus];
  75. if (count > 0) {
  76. console.log(testStatus.toUpperCase(), count);
  77. }
  78. }
  79. if (!allTestsPassing) {
  80. console.log("\nNot all tests are passing");
  81. }
  82. process.exit(allTestsPassing ? 0 : 1);
  83. });