PromiseRejectionTracking.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // Test HostPromiseRejectionTracker - see ecma262 section 25.4.1.9
  6. let tests = [
  7. {
  8. name: "Reject promise with no reactions.",
  9. body: function(index)
  10. {
  11. let controller;
  12. let promise = new Promise((resolve, reject)=>{
  13. controller = {resolve, reject};
  14. });
  15. controller.reject("Rejection from test " + index);//Should notify rejected
  16. }
  17. },
  18. {
  19. name: "Reject promise with a catch reaction only.",
  20. body: function(index)
  21. {
  22. let controller;
  23. let promise = new Promise((resolve, reject)=>{
  24. controller = {resolve, reject};
  25. }).catch(()=>{});
  26. controller.reject("Rejection from test " + index);//Should NOT notify
  27. }
  28. },
  29. {
  30. name: "Reject promise with catch and then reactions.",
  31. body: function(index)
  32. {
  33. let controller;
  34. let promise = new Promise((resolve, reject)=>{
  35. controller = {resolve, reject};
  36. }).then(()=>{}).catch(()=>{});
  37. controller.reject("Rejection from test " + index);//Should NOT notify
  38. }
  39. },
  40. {
  41. name: "Reject promise then add a catch afterwards.",
  42. body: function(index)
  43. {
  44. let controller;
  45. let promise = new Promise((resolve, reject)=>{
  46. controller = {resolve, reject};
  47. });
  48. controller.reject("Rejection from test " + index);//Should notify rejected
  49. promise.catch(()=>{});//Should notify handled
  50. }
  51. },
  52. {
  53. name: "Reject promise then add two catches afterwards.",
  54. body: function(index)
  55. {
  56. let controller;
  57. let promise = new Promise((resolve, reject)=>{
  58. controller = {resolve, reject};
  59. });
  60. controller.reject("Rejection from test " + index);//Should notify rejected
  61. promise.catch(()=>{});//Should notify handled
  62. promise.catch(()=>{});//Should NOT notify
  63. }
  64. },
  65. {
  66. name: "Async function that throws.",
  67. body: function(index)
  68. {
  69. async function aFunction()
  70. {
  71. throw ("Rejection from test " + index);
  72. }
  73. aFunction();//Should notify rejected
  74. }
  75. },
  76. {
  77. name: "Async function that throws but is caught.",
  78. body: function(index)
  79. {
  80. async function aFunction()
  81. {
  82. throw ("Rejection from test " + index);
  83. }
  84. aFunction().catch(()=>{});//Should notify rejected AND then handled
  85. }
  86. },
  87. {
  88. name: "Async function that awaits a function that throws.",
  89. body: function(index)
  90. {
  91. async function aFunction()
  92. {
  93. throw ("Rejection from test " + index);//Should notify rejected
  94. }
  95. async function bFunction()
  96. {
  97. await aFunction();//Should notify handled
  98. }
  99. bFunction();//Should notify rejected in the async section
  100. },
  101. },
  102. {
  103. name: "Reject a handled promise then handle one of the handles but not the other.",
  104. body: function(index)
  105. {
  106. let controller;
  107. let promise = new Promise((resolve, reject) => { controller = {resolve, reject};});
  108. let a = promise.then(() => {});//a is not handled
  109. let b = promise.then(() => {});//b is not handled
  110. controller.reject("Rejection from test " + index);//no notification as handled
  111. let c = a.then(() => {}); //handle a
  112. c.catch(() => {b.then(()=>{})}); // handle c
  113. //b is still not handled -> notify once in async section
  114. //b has an async handler -> will notify handled in async section
  115. //the .then() on b is not handled so will notify in async section
  116. },
  117. },
  118. {
  119. name: "Reject a handled promise and don't handle either path.",
  120. body: function(index)
  121. {
  122. let controller;
  123. let promise = new Promise((resolve, reject) => { controller = {resolve, reject};});
  124. let a = promise.then(() => {});//a is not handled
  125. let b = promise.then(() => {});//b is not handled
  126. controller.reject("Rejection from test " + index);//no notification as handled
  127. let c = a.then(() => {}); //handle a
  128. //b is not handled -> will notify in async section
  129. //c is not handled -> will notify in async section
  130. }
  131. }
  132. ];
  133. for(let i = 0; i < tests.length; ++i)
  134. {
  135. WScript.Echo('Executing test #' + (i + 1) + ' - ' + tests[i].name);
  136. tests[i].body(i+1);
  137. }
  138. WScript.Echo("Begin async results:");