UNPKG

3.38 kBJavaScriptView Raw
1'use strict';
2
3var tap = require('tap');
4var path = require('path');
5var spawn = require('child_process').spawn;
6var concat = require('concat-stream');
7
8var stripFullStack = require('./common').stripFullStack;
9
10tap.test('callback returning rejected promise should cause that test (and only that test) to fail', function (tt) {
11 tt.plan(1);
12
13 var ps = spawn(process.execPath, [path.join(__dirname, 'promises', 'fail.js')]);
14
15 ps.stdout.pipe(concat(function (rows) {
16 var rowsString = rows.toString('utf8');
17
18 if (/^skip\n$/.test(rowsString)) {
19 return tt.pass('the test file indicated it should be skipped');
20 }
21
22 var strippedString = stripFullStack(rowsString);
23
24 // hack for consistency across all versions of node
25 // some versions produce a longer stack trace for some reason
26 // since this doesn't affect the validity of the test, the extra line is removed if present
27 // the regex just removes the lines "at <anonymous>" and "[... stack stripped ...]" if they occur together
28 strippedString = strippedString.replace(/.+at <anonymous>\n.+\[\.\.\. stack stripped \.\.\.\]\n/, '');
29
30 tt.same(strippedString, [
31 'TAP version 13',
32 '# promise',
33 'not ok 1 Error: rejection message',
34 ' ---',
35 ' operator: fail',
36 ' stack: |-',
37 ' Error: Error: rejection message',
38 ' [... stack stripped ...]',
39 ' ...',
40 '# after',
41 'ok 2 should be truthy',
42 '',
43 '1..2',
44 '# tests 2',
45 '# pass 1',
46 '# fail 1',
47 '',
48 ''
49 ].join('\n'));
50 }));
51});
52
53tap.test('subtest callback returning rejected promise should cause that subtest (and only that subtest) to fail', function (tt) {
54 tt.plan(1);
55
56 var ps = spawn(process.execPath, [path.join(__dirname, 'promises', 'subTests.js')]);
57
58 ps.stdout.pipe(concat(function (rows) {
59 var rowsString = rows.toString('utf8');
60
61 if (/^skip\n$/.test(rowsString)) {
62 return tt.pass('the test file indicated it should be skipped');
63 }
64
65 var strippedString = stripFullStack(rowsString);
66
67 // hack for consistency across all versions of node
68 // some versions produce a longer stack trace for some reason
69 // since this doesn't affect the validity of the test, the extra line is removed if present
70 // the regex just removes the lines "at <anonymous>" and "[... stack stripped ...]" if they occur together
71 strippedString = strippedString.replace(/.+at <anonymous>\n.+\[\.\.\. stack stripped \.\.\.\]\n/, '');
72
73 tt.same(strippedString, [
74 'TAP version 13',
75 '# promise',
76 '# sub test that should fail',
77 'not ok 1 Error: rejection message',
78 ' ---',
79 ' operator: fail',
80 ' stack: |-',
81 ' Error: Error: rejection message',
82 ' [... stack stripped ...]',
83 ' ...',
84 '# sub test that should pass',
85 'ok 2 should be truthy',
86 '',
87 '1..2',
88 '# tests 2',
89 '# pass 1',
90 '# fail 1',
91 '',
92 ''
93 ].join('\n'));
94 }));
95});