index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/BCD.js";
  15. import "./tests/operations/ByteRepr.js";
  16. import "./tests/operations/CharEnc.js";
  17. import "./tests/operations/Cipher.js";
  18. import "./tests/operations/Code.js";
  19. import "./tests/operations/Compress.js";
  20. import "./tests/operations/DateTime.js";
  21. import "./tests/operations/FlowControl.js";
  22. import "./tests/operations/Image.js";
  23. import "./tests/operations/MorseCode.js";
  24. import "./tests/operations/StrUtils.js";
  25. import "./tests/operations/SeqUtils.js";
  26. let allTestsPassing = true;
  27. const testStatusCounts = {
  28. total: 0,
  29. };
  30. /**
  31. * Helper function to convert a status to an icon.
  32. *
  33. * @param {string} status
  34. * @returns {string}
  35. */
  36. function statusToIcon(status) {
  37. const icons = {
  38. erroring: "🔥",
  39. failing: "❌",
  40. passing: "✔️️",
  41. };
  42. return icons[status] || "?";
  43. }
  44. /**
  45. * Displays a given test result in the console.
  46. *
  47. * @param {Object} testResult
  48. */
  49. function handleTestResult(testResult) {
  50. allTestsPassing = allTestsPassing && testResult.status === "passing";
  51. const newCount = (testStatusCounts[testResult.status] || 0) + 1;
  52. testStatusCounts[testResult.status] = newCount;
  53. testStatusCounts.total += 1;
  54. console.log([
  55. statusToIcon(testResult.status),
  56. testResult.test.name
  57. ].join(" "));
  58. if (testResult.output) {
  59. console.log(
  60. testResult.output
  61. .trim()
  62. .replace(/^/, "\t")
  63. .replace(/\n/g, "\n\t")
  64. );
  65. }
  66. }
  67. /**
  68. * Fail if the process takes longer than 10 seconds.
  69. */
  70. setTimeout(function() {
  71. console.log("Tests took longer than 10 seconds to run, returning.");
  72. process.exit(1);
  73. }, 10 * 1000);
  74. TestRegister.runTests()
  75. .then(function(results) {
  76. results.forEach(handleTestResult);
  77. console.log("\n");
  78. for (const testStatus in testStatusCounts) {
  79. const count = testStatusCounts[testStatus];
  80. if (count > 0) {
  81. console.log(testStatus.toUpperCase(), count);
  82. }
  83. }
  84. if (!allTestsPassing) {
  85. console.log("\nNot all tests are passing");
  86. }
  87. process.exit(allTestsPassing ? 0 : 1);
  88. });