template.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const { XMLHttpRequest } = require("xmlhttprequest");
  2. // const jsl = require("svjsl");
  3. const settings = require("../settings");
  4. const meta = {
  5. name: "Test Name",
  6. category: "Test Category"
  7. };
  8. const baseURL = `http://127.0.0.1:${settings.httpServer.port}`;
  9. /**
  10. * @typedef {Object} UnitTestResult
  11. * @prop {Object} meta
  12. * @prop {String} meta.name
  13. * @prop {String} meta.category
  14. * @prop {Array<String>} errors
  15. */
  16. /**
  17. * Runs this unit test
  18. * @returns {Promise<UnitTestResult>}
  19. */
  20. function run()
  21. {
  22. return new Promise((resolve, reject) => {
  23. let errors = [];
  24. let run = () => new Promise(xhrResolve => {
  25. let xhr = new XMLHttpRequest();
  26. xhr.open("GET", `${baseURL}/ENDPOINT`); // < set endpoint here
  27. xhr.onreadystatechange = () => {
  28. if(xhr.readyState == 4)
  29. {
  30. if(xhr.status >= 200 && xhr.status < 300)
  31. {
  32. // unit tests here
  33. }
  34. else
  35. {
  36. errors.push(`Couldn't reach endpoint - HTTP status: ${xhr.status}`);
  37. return xhrResolve();
  38. }
  39. }
  40. };
  41. xhr.send();
  42. });
  43. run().then(() => {
  44. if(errors.length == 0)
  45. return resolve({ meta });
  46. else
  47. return reject({ meta, errors });
  48. });
  49. });
  50. }
  51. module.exports = { meta, run };