UNPKG

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