TestRegister.mjs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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";
  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. return Promise.all(
  47. this.tests.map(function(test, i) {
  48. const chef = new Chef();
  49. return chef.bake(
  50. test.input,
  51. test.recipeConfig,
  52. {},
  53. 0,
  54. false
  55. ).then(function(result) {
  56. const ret = {
  57. test: test,
  58. status: null,
  59. output: null,
  60. };
  61. if (result.error) {
  62. if (test.expectedError) {
  63. ret.status = "passing";
  64. } else {
  65. ret.status = "erroring";
  66. ret.output = result.error.displayStr;
  67. }
  68. } else {
  69. if (test.expectedError) {
  70. ret.status = "failing";
  71. ret.output = "Expected an error but did not receive one.";
  72. } else if (result.result === test.expectedOutput) {
  73. ret.status = "passing";
  74. } else if (test.hasOwnProperty("expectedMatch") && test.expectedMatch.test(result.result)) {
  75. ret.status = "passing";
  76. } else {
  77. ret.status = "failing";
  78. const expected = test.expectedOutput ? test.expectedOutput :
  79. test.expectedMatch ? test.expectedMatch.toString() : "unknown";
  80. ret.output = [
  81. "Expected",
  82. "\t" + expected.replace(/\n/g, "\n\t"),
  83. "Received",
  84. "\t" + result.result.replace(/\n/g, "\n\t"),
  85. ].join("\n");
  86. }
  87. }
  88. return ret;
  89. });
  90. })
  91. );
  92. }
  93. /**
  94. * Run all api related tests and wrap results in report format
  95. */
  96. runApiTests() {
  97. return Promise.all(this.apiTests.map(async function(test, i) {
  98. const result = {
  99. test: test,
  100. status: null,
  101. output: null,
  102. };
  103. try {
  104. await test.run();
  105. result.status = "passing";
  106. } catch (e) {
  107. result.status = "erroring";
  108. result.output = e.message;
  109. }
  110. return result;
  111. }));
  112. }
  113. }
  114. // Export an instance to make a singleton
  115. export default new TestRegister();