TestRegister.mjs 6.1 KB

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