index.js 2.2 KB

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