double_end.js 1.6 KB

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