TestRegister.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /**
  14. * Object to store and run the list of tests.
  15. *
  16. * @class
  17. * @constructor
  18. */
  19. class TestRegister {
  20. /**
  21. * initialise with no tests
  22. */
  23. constructor() {
  24. this.tests = [];
  25. this.apiTests = [];
  26. }
  27. /**
  28. * Add a list of tests to the register.
  29. *
  30. * @param {Object[]} tests
  31. */
  32. addTests(tests) {
  33. this.tests = this.tests.concat(tests);
  34. }
  35. /**
  36. * Add a list of api tests to the register
  37. * @param {Object[]} tests
  38. */
  39. addApiTests(tests) {
  40. this.apiTests = this.apiTests.concat(tests);
  41. }
  42. /**
  43. * Runs all the tests in the register.
  44. */
  45. runTests () {
  46. console.log("Running tests...");
  47. return Promise.all(
  48. this.tests.map(function(test, i) {
  49. const chef = new Chef();
  50. return chef.bake(
  51. test.input,
  52. test.recipeConfig,
  53. {},
  54. 0,
  55. false
  56. ).then(function(result) {
  57. const ret = {
  58. test: test,
  59. status: null,
  60. output: null,
  61. };
  62. if (result.error) {
  63. if (test.expectedError) {
  64. ret.status = "passing";
  65. } else {
  66. ret.status = "erroring";
  67. ret.output = result.error.displayStr;
  68. }
  69. } else {
  70. if (test.expectedError) {
  71. ret.status = "failing";
  72. ret.output = "Expected an error but did not receive one.";
  73. } else if (result.result === test.expectedOutput) {
  74. ret.status = "passing";
  75. } else if ("expectedMatch" in test && test.expectedMatch.test(result.result)) {
  76. ret.status = "passing";
  77. } else {
  78. ret.status = "failing";
  79. const expected = test.expectedOutput ? test.expectedOutput :
  80. test.expectedMatch ? test.expectedMatch.toString() : "unknown";
  81. ret.output = [
  82. "Expected",
  83. "\t" + expected.replace(/\n/g, "\n\t"),
  84. "Received",
  85. "\t" + result.result.replace(/\n/g, "\n\t"),
  86. ].join("\n");
  87. }
  88. }
  89. return ret;
  90. });
  91. })
  92. );
  93. }
  94. /**
  95. * Run all api related tests and wrap results in report format
  96. */
  97. runApiTests() {
  98. console.log("Running tests...");
  99. return Promise.all(this.apiTests.map(async function(test, i) {
  100. const result = {
  101. test: test,
  102. status: null,
  103. output: null,
  104. };
  105. try {
  106. await test.run();
  107. result.status = "passing";
  108. } catch (e) {
  109. result.status = "erroring";
  110. result.output = e.message;
  111. }
  112. return result;
  113. }));
  114. }
  115. }
  116. // Export an instance to make a singleton
  117. export default new TestRegister();