Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

double_end.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var test = require('tap').test;
  2. var path = require('path');
  3. var concat = require('concat-stream');
  4. var spawn = require('child_process').spawn;
  5. var stripFullStack = require('./common').stripFullStack;
  6. test(function (t) {
  7. t.plan(2);
  8. var ps = spawn(process.execPath, [path.join(__dirname, 'double_end', 'double.js')]);
  9. ps.on('exit', function (code) {
  10. t.equal(code, 1);
  11. });
  12. ps.stdout.pipe(concat(function (body) {
  13. // The implementation of node's timer library has changed over time. We
  14. // need to reverse engineer the error we expect to see.
  15. // This code is unfortunately by necessity highly coupled to node
  16. // versions, and may require tweaking with future versions of the timers
  17. // library.
  18. function doEnd() { throw new Error(); };
  19. var to = setTimeout(doEnd, 5000);
  20. clearTimeout(to);
  21. to._onTimeout = doEnd;
  22. var stackExpected;
  23. var atExpected;
  24. try {
  25. to._onTimeout();
  26. }
  27. catch (e) {
  28. stackExpected = stripFullStack(e.stack).split('\n')[1];
  29. stackExpected = stackExpected.replace('double_end.js', 'double_end/double.js');
  30. stackExpected = stackExpected.trim();
  31. atExpected = stackExpected.replace(/^at\s+/, 'at: ');
  32. }
  33. var stripped = stripFullStack(body.toString('utf8'));
  34. t.equal(stripped, [
  35. 'TAP version 13',
  36. '# double end',
  37. 'ok 1 should be equal',
  38. 'not ok 2 .end() called twice',
  39. ' ---',
  40. ' operator: fail',
  41. ' ' + atExpected,
  42. ' stack: |-',
  43. ' Error: .end() called twice',
  44. ' [... stack stripped ...]',
  45. ' ' + stackExpected,
  46. ' [... stack stripped ...]',
  47. ' ...',
  48. '',
  49. '1..2',
  50. '# tests 2',
  51. '# pass 1',
  52. '# fail 1',
  53. ].join('\n') + '\n\n');
  54. }));
  55. });