TestRegister.mjs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. * TestRegister.js
  3. *
  4. * This is so individual files can register their tests in one place, and
  5. * ensure that they will get run by the frontend.
  6. *
  7. * @author tlwr [toby@toby.codes]
  8. * @author d98762625 [d98762625@gmail.com]
  9. * @copyright Crown Copyright 2018
  10. * @license Apache-2.0
  11. */
  12. import Chef from "../../src/core/Chef.mjs";
  13. import Utils from "../../src/core/Utils.mjs";
  14. import cliProgress from "cli-progress";
  15. /**
  16. * Object to store and run the list of tests.
  17. *
  18. * @class
  19. * @constructor
  20. */
  21. class TestRegister {
  22. /**
  23. * initialise with no tests
  24. */
  25. constructor() {
  26. this.tests = [];
  27. this.apiTests = [];
  28. }
  29. /**
  30. * Add a list of tests to the register.
  31. *
  32. * @param {Object[]} tests
  33. */
  34. addTests(tests) {
  35. this.tests = this.tests.concat(tests);
  36. }
  37. /**
  38. * Add a list of api tests to the register
  39. * @param {Object[]} tests
  40. */
  41. addApiTests(tests) {
  42. this.apiTests = this.apiTests.concat(tests);
  43. }
  44. /**
  45. * Runs all the tests in the register.
  46. */
  47. async runTests () {
  48. const progBar = new cliProgress.SingleBar({
  49. format: formatter,
  50. stopOnComplete: true
  51. }, cliProgress.Presets.shades_classic);
  52. const testResults = [];
  53. console.log("Running operation tests...");
  54. progBar.start(this.tests.length, 0, {
  55. msg: "Setting up"
  56. });
  57. for (const test of this.tests) {
  58. progBar.update(testResults.length, {
  59. msg: test.name
  60. });
  61. const chef = new Chef();
  62. const result = await chef.bake(
  63. test.input,
  64. test.recipeConfig,
  65. {},
  66. 0,
  67. false
  68. );
  69. const ret = {
  70. test: test,
  71. status: null,
  72. output: null,
  73. duration: result.duration
  74. };
  75. if (result.error) {
  76. if (test.expectedError) {
  77. ret.status = "passing";
  78. } else {
  79. ret.status = "erroring";
  80. ret.output = result.error.displayStr;
  81. }
  82. } else {
  83. if (test.expectedError) {
  84. ret.status = "failing";
  85. ret.output = "Expected an error but did not receive one.";
  86. } else if (result.result === test.expectedOutput) {
  87. ret.status = "passing";
  88. } else if ("expectedMatch" in test && test.expectedMatch.test(result.result)) {
  89. ret.status = "passing";
  90. } else if ("unexpectedMatch" in test && !test.unexpectedMatch.test(result.result)) {
  91. ret.status = "passing";
  92. } else {
  93. ret.status = "failing";
  94. const expected = test.expectedOutput ? test.expectedOutput :
  95. test.expectedMatch ? test.expectedMatch.toString() :
  96. test.unexpectedMatch ? "to not find " + test.unexpectedMatch.toString() :
  97. "unknown";
  98. ret.output = [
  99. "Expected",
  100. "\t" + expected.replace(/\n/g, "\n\t"),
  101. "Received",
  102. "\t" + result.result.replace(/\n/g, "\n\t"),
  103. ].join("\n");
  104. }
  105. }
  106. testResults.push(ret);
  107. progBar.increment();
  108. }
  109. return testResults;
  110. }
  111. /**
  112. * Run all api related tests and wrap results in report format
  113. */
  114. async runApiTests() {
  115. const progBar = new cliProgress.SingleBar({
  116. format: formatter,
  117. stopOnComplete: true
  118. }, cliProgress.Presets.shades_classic);
  119. const testResults = [];
  120. console.log("Running Node API tests...");
  121. progBar.start(this.apiTests.length, 0, {
  122. msg: "Setting up"
  123. });
  124. global.TESTING = true;
  125. for (const test of this.apiTests) {
  126. progBar.update(testResults.length, {
  127. msg: test.name
  128. });
  129. const result = {
  130. test: test,
  131. status: null,
  132. output: null
  133. };
  134. try {
  135. await test.run();
  136. result.status = "passing";
  137. } catch (e) {
  138. result.status = "erroring";
  139. result.output = e.message;
  140. }
  141. testResults.push(result);
  142. progBar.increment();
  143. }
  144. return testResults;
  145. }
  146. }
  147. /**
  148. * Formatter for the progress bar
  149. *
  150. * @param {Object} options
  151. * @param {Object} params
  152. * @param {Object} payload
  153. * @returns {string}
  154. */
  155. function formatter(options, params, payload) {
  156. const bar = options.barCompleteString.substr(0, Math.round(params.progress * options.barsize)) +
  157. options.barIncompleteString.substr(0, Math.round((1-params.progress) * options.barsize));
  158. const percentage = Math.floor(params.progress * 100),
  159. duration = Math.floor((Date.now() - params.startTime) / 1000);
  160. let testName = payload.msg ? payload.msg : "";
  161. if (params.value >= params.total) testName = "Tests completed";
  162. testName = Utils.truncate(testName, 25).padEnd(25, " ");
  163. return `${testName} ${bar} ${params.value}/${params.total} | ${percentage}% | Duration: ${duration}s`;
  164. }
  165. // Export an instance to make a singleton
  166. export default new TestRegister();