lazy.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 () { resolve(20); }, 10);
  10. });
  11. a(invoked, false);
  12. setTimeout(function () {
  13. a(invoked, false);
  14. promise.then(function (value) {
  15. a(value, 20);
  16. setTimeout(d, 0); // Escape error swallowing
  17. });
  18. a(invoked, true);
  19. }, 15);
  20. },
  21. "Passes rejection": function (a, d) {
  22. var promise = t(function (resolve, reject) {
  23. setTimeout(function () { reject(new Error("Stop")); }, 10);
  24. });
  25. promise.catch(function (error) {
  26. a(error instanceof Error, true);
  27. a(error.message, "Stop");
  28. setTimeout(d, 0); // Escape error swallowing
  29. });
  30. },
  31. "Passes sync exception": function (a, d) {
  32. var promise = t(function () { throw new Error("Stop"); });
  33. promise.catch(function (error) {
  34. a(error instanceof Error, true);
  35. a(error.message, "Stop");
  36. setTimeout(d, 0); // Escape error swallowing
  37. });
  38. }
  39. };
  40. };