TestRegister.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. * @copyright Crown Copyright 2017
  9. * @license Apache-2.0
  10. */
  11. import Chef from "../src/core/Chef.js";
  12. (function() {
  13. /**
  14. * Object to store and run the list of tests.
  15. *
  16. * @class
  17. * @constructor
  18. */
  19. function TestRegister() {
  20. this.tests = [];
  21. }
  22. /**
  23. * Add a list of tests to the register.
  24. *
  25. * @param {Object[]} tests
  26. */
  27. TestRegister.prototype.addTests = function(tests) {
  28. this.tests = this.tests.concat(tests);
  29. };
  30. /**
  31. * Runs all the tests in the register.
  32. */
  33. TestRegister.prototype.runTests = function() {
  34. return Promise.all(
  35. this.tests.map(function(test, i) {
  36. let chef = new Chef();
  37. return chef.bake(
  38. test.input,
  39. test.recipeConfig,
  40. {},
  41. 0,
  42. false
  43. )
  44. .then(function(result) {
  45. let ret = {
  46. test: test,
  47. status: null,
  48. output: null,
  49. };
  50. if (result.error) {
  51. if (test.expectedError) {
  52. ret.status = "passing";
  53. } else {
  54. ret.status = "erroring";
  55. ret.output = result.error.displayStr;
  56. }
  57. } else {
  58. if (test.expectedError) {
  59. ret.status = "failing";
  60. ret.output = "Expected an error but did not receive one.";
  61. } else if (result.result === test.expectedOutput) {
  62. ret.status = "passing";
  63. } else {
  64. ret.status = "failing";
  65. ret.output = [
  66. "Expected",
  67. "\t" + test.expectedOutput.replace(/\n/g, "\n\t"),
  68. "Received",
  69. "\t" + result.result.replace(/\n/g, "\n\t"),
  70. ].join("\n");
  71. }
  72. }
  73. return ret;
  74. });
  75. })
  76. );
  77. };
  78. // Singleton TestRegister, keeping things simple and obvious.
  79. global.TestRegister = global.TestRegister || new TestRegister();
  80. })();
  81. export default global.TestRegister;