lazy.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. module.exports = function (t) {
  3. if (typeof Promise !== "function") return null; // Run tests only in ES2015+ env
  4. return {
  5. "Delays execution": function (a, d) {
  6. var invoked = false;
  7. var promise = t(function (resolve) {
  8. invoked = true;
  9. setTimeout(function () {
  10. resolve(20);
  11. }, 10);
  12. });
  13. a(invoked, false);
  14. setTimeout(function () {
  15. a(invoked, false);
  16. promise.then(function (value) {
  17. a(value, 20);
  18. setTimeout(d, 0); // Escape error swallowing
  19. });
  20. a(invoked, true);
  21. }, 15);
  22. },
  23. "Passes rejection": function (a, d) {
  24. var promise = t(function (resolve, reject) {
  25. setTimeout(function () {
  26. reject(new Error("Stop"));
  27. }, 10);
  28. });
  29. promise.catch(function (error) {
  30. a(error instanceof Error, true);
  31. a(error.message, "Stop");
  32. setTimeout(d, 0); // Escape error swallowing
  33. });
  34. },
  35. "Passes sync exception": function (a, d) {
  36. var promise = t(function () {
  37. throw new Error("Stop");
  38. });
  39. promise.catch(function (error) {
  40. a(error instanceof Error, true);
  41. a(error.message, "Stop");
  42. setTimeout(d, 0); // Escape error swallowing
  43. });
  44. }
  45. };
  46. };