TestRegister.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. const chef = new Chef();
  37. return chef.bake(
  38. test.input,
  39. test.recipeConfig,
  40. {},
  41. 0,
  42. false
  43. ).then(function(result) {
  44. const ret = {
  45. test: test,
  46. status: null,
  47. output: null,
  48. };
  49. if (result.error) {
  50. if (test.expectedError) {
  51. ret.status = "passing";
  52. } else {
  53. ret.status = "erroring";
  54. ret.output = result.error.displayStr;
  55. }
  56. } else {
  57. if (test.expectedError) {
  58. ret.status = "failing";
  59. ret.output = "Expected an error but did not receive one.";
  60. } else if (result.result === test.expectedOutput) {
  61. ret.status = "passing";
  62. } else {
  63. ret.status = "failing";
  64. ret.output = [
  65. "Expected",
  66. "\t" + test.expectedOutput.replace(/\n/g, "\n\t"),
  67. "Received",
  68. "\t" + result.result.replace(/\n/g, "\n\t"),
  69. ].join("\n");
  70. }
  71. }
  72. return ret;
  73. });
  74. })
  75. );
  76. };
  77. // Singleton TestRegister, keeping things simple and obvious.
  78. global.TestRegister = global.TestRegister || new TestRegister();
  79. })();
  80. export default global.TestRegister;