Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. var assert = require('assert');
  2. var ourProcess = require('./browser');
  3. describe('test against our process', function () {
  4. test(ourProcess);
  5. });
  6. if (!process.browser) {
  7. describe('test against node', function () {
  8. test(process);
  9. });
  10. vmtest();
  11. }
  12. function test (ourProcess) {
  13. describe('test arguments', function () {
  14. it ('works', function (done) {
  15. var order = 0;
  16. ourProcess.nextTick(function (num) {
  17. assert.equal(num, order++, 'first one works');
  18. ourProcess.nextTick(function (num) {
  19. assert.equal(num, order++, 'recursive one is 4th');
  20. }, 3);
  21. }, 0);
  22. ourProcess.nextTick(function (num) {
  23. assert.equal(num, order++, 'second one starts');
  24. ourProcess.nextTick(function (num) {
  25. assert.equal(num, order++, 'this is third');
  26. ourProcess.nextTick(function (num) {
  27. assert.equal(num, order++, 'this is last');
  28. done();
  29. }, 5);
  30. }, 4);
  31. }, 1);
  32. ourProcess.nextTick(function (num) {
  33. assert.equal(num, order++, '3rd schedualed happens after the error');
  34. }, 2);
  35. });
  36. });
  37. if (!process.browser) {
  38. describe('test errors', function (t) {
  39. it ('works', function (done) {
  40. var order = 0;
  41. process.removeAllListeners('uncaughtException');
  42. process.once('uncaughtException', function(err) {
  43. assert.equal(2, order++, 'error is third');
  44. ourProcess.nextTick(function () {
  45. assert.equal(5, order++, 'schedualed in error is last');
  46. done();
  47. });
  48. });
  49. ourProcess.nextTick(function () {
  50. assert.equal(0, order++, 'first one works');
  51. ourProcess.nextTick(function () {
  52. assert.equal(4, order++, 'recursive one is 4th');
  53. });
  54. });
  55. ourProcess.nextTick(function () {
  56. assert.equal(1, order++, 'second one starts');
  57. throw(new Error('an error is thrown'));
  58. });
  59. ourProcess.nextTick(function () {
  60. assert.equal(3, order++, '3rd schedualed happens after the error');
  61. });
  62. });
  63. });
  64. }
  65. describe('rename globals', function (t) {
  66. var oldTimeout = setTimeout;
  67. var oldClear = clearTimeout;
  68. it('clearTimeout', function (done){
  69. var ok = true;
  70. clearTimeout = function () {
  71. ok = false;
  72. }
  73. var ran = false;
  74. function cleanup() {
  75. clearTimeout = oldClear;
  76. var err;
  77. try {
  78. assert.ok(ok, 'fake clearTimeout ran');
  79. assert.ok(ran, 'should have run');
  80. } catch (e) {
  81. err = e;
  82. }
  83. done(err);
  84. }
  85. setTimeout(cleanup, 1000);
  86. ourProcess.nextTick(function () {
  87. ran = true;
  88. });
  89. });
  90. it('just setTimeout', function (done){
  91. setTimeout = function () {
  92. setTimeout = oldTimeout;
  93. try {
  94. assert.ok(false, 'fake setTimeout called')
  95. } catch (e) {
  96. done(e);
  97. }
  98. }
  99. ourProcess.nextTick(function () {
  100. setTimeout = oldTimeout;
  101. done();
  102. });
  103. });
  104. });
  105. }
  106. function vmtest() {
  107. var vm = require('vm');
  108. var fs = require('fs');
  109. var process = fs.readFileSync('./browser.js', {encoding: 'utf8'});
  110. describe('should work in vm in strict mode with no globals', function () {
  111. it('should parse', function (done) {
  112. var str = '"use strict";var module = {exports:{}};';
  113. str += process;
  114. str += 'this.works = process.browser;';
  115. var script = new vm.Script(str);
  116. var context = {
  117. works: false
  118. };
  119. script.runInNewContext(context);
  120. assert.ok(context.works);
  121. done();
  122. });
  123. it('setTimeout throws error', function (done) {
  124. var str = '"use strict";var module = {exports:{}};';
  125. str += process;
  126. str += 'try {process.nextTick(function () {})} catch (e){this.works = e;}';
  127. var script = new vm.Script(str);
  128. var context = {
  129. works: false
  130. };
  131. script.runInNewContext(context);
  132. assert.ok(context.works);
  133. done();
  134. });
  135. it('should generally work', function (done) {
  136. var str = '"use strict";var module = {exports:{}};';
  137. str += process;
  138. str += 'process.nextTick(function () {assert.ok(true);done();})';
  139. var script = new vm.Script(str);
  140. var context = {
  141. clearTimeout: clearTimeout,
  142. setTimeout: setTimeout,
  143. done: done,
  144. assert: assert
  145. };
  146. script.runInNewContext(context);
  147. });
  148. it('late defs setTimeout', function (done) {
  149. var str = '"use strict";var module = {exports:{}};';
  150. str += process;
  151. str += 'var setTimeout = hiddenSetTimeout;process.nextTick(function () {assert.ok(true);done();})';
  152. var script = new vm.Script(str);
  153. var context = {
  154. clearTimeout: clearTimeout,
  155. hiddenSetTimeout: setTimeout,
  156. done: done,
  157. assert: assert
  158. };
  159. script.runInNewContext(context);
  160. });
  161. it('late defs clearTimeout', function (done) {
  162. var str = '"use strict";var module = {exports:{}};';
  163. str += process;
  164. str += 'var clearTimeout = hiddenClearTimeout;process.nextTick(function () {assert.ok(true);done();})';
  165. var script = new vm.Script(str);
  166. var context = {
  167. hiddenClearTimeout: clearTimeout,
  168. setTimeout: setTimeout,
  169. done: done,
  170. assert: assert
  171. };
  172. script.runInNewContext(context);
  173. });
  174. it('late defs setTimeout and then redefine', function (done) {
  175. var str = '"use strict";var module = {exports:{}};';
  176. str += process;
  177. str += 'var setTimeout = hiddenSetTimeout;process.nextTick(function () {setTimeout = function (){throw new Error("foo")};hiddenSetTimeout(function(){process.nextTick(function (){assert.ok(true);done();});});});';
  178. var script = new vm.Script(str);
  179. var context = {
  180. clearTimeout: clearTimeout,
  181. hiddenSetTimeout: setTimeout,
  182. done: done,
  183. assert: assert
  184. };
  185. script.runInNewContext(context);
  186. });
  187. });
  188. }