1 | 'use strict';
|
2 |
|
3 | var tap = require('tap');
|
4 |
|
5 | var stripFullStack = require('./common').stripFullStack;
|
6 | var runProgram = require('./common').runProgram;
|
7 |
|
8 | var nodeVersion = process.versions.node;
|
9 | var majorVersion = nodeVersion.split('.')[0];
|
10 |
|
11 | if (Number(majorVersion) < 8) {
|
12 | process.exit(0);
|
13 | }
|
14 |
|
15 | tap.test('async1', function (t) {
|
16 | runProgram('async-await', 'async1.js', function (r) {
|
17 | t.same(r.stdout.toString('utf8'), [
|
18 | 'TAP version 13',
|
19 | '# async1',
|
20 | 'ok 1 before await',
|
21 | 'ok 2 after await',
|
22 | '',
|
23 | '1..2',
|
24 | '# tests 2',
|
25 | '# pass 2',
|
26 | '',
|
27 | '# ok'
|
28 | ].join('\n') + '\n\n');
|
29 | t.same(r.exitCode, 0);
|
30 | t.same(r.stderr.toString('utf8'), '');
|
31 | t.end();
|
32 | });
|
33 | });
|
34 |
|
35 | tap.test('async2', function (t) {
|
36 | runProgram('async-await', 'async2.js', function (r) {
|
37 | var stdout = r.stdout.toString('utf8');
|
38 | var lines = stdout.split('\n');
|
39 | lines = lines.filter(function (line) {
|
40 | return ! /^(\s+)at(\s+)<anonymous>$/.test(line);
|
41 | });
|
42 | stdout = lines.join('\n');
|
43 |
|
44 | t.same(stripFullStack(stdout), [
|
45 | 'TAP version 13',
|
46 | '# async2',
|
47 | 'ok 1 before await',
|
48 | 'not ok 2 after await',
|
49 | ' ---',
|
50 | ' operator: ok',
|
51 | ' expected: true',
|
52 | ' actual: false',
|
53 | ' at: Test.myTest ($TEST/async-await/async2.js:$LINE:$COL)',
|
54 | ' stack: |-',
|
55 | ' Error: after await',
|
56 | ' [... stack stripped ...]',
|
57 | ' at Test.myTest ($TEST/async-await/async2.js:$LINE:$COL)',
|
58 | ' ...',
|
59 | '',
|
60 | '1..2',
|
61 | '# tests 2',
|
62 | '# pass 1',
|
63 | '# fail 1'
|
64 | ].join('\n') + '\n\n');
|
65 | t.same(r.exitCode, 1);
|
66 | t.same(r.stderr.toString('utf8'), '');
|
67 | t.end();
|
68 | });
|
69 | });
|
70 |
|
71 | tap.test('async3', function (t) {
|
72 | runProgram('async-await', 'async3.js', function (r) {
|
73 | t.same(r.stdout.toString('utf8'), [
|
74 | 'TAP version 13',
|
75 | '# async3',
|
76 | 'ok 1 before await',
|
77 | 'ok 2 after await',
|
78 | '',
|
79 | '1..2',
|
80 | '# tests 2',
|
81 | '# pass 2',
|
82 | '',
|
83 | '# ok'
|
84 | ].join('\n') + '\n\n');
|
85 | t.same(r.exitCode, 0);
|
86 | t.same(r.stderr.toString('utf8'), '');
|
87 | t.end();
|
88 | });
|
89 | });
|
90 |
|
91 | tap.test('async4', function (t) {
|
92 | runProgram('async-await', 'async4.js', function (r) {
|
93 | t.same(stripFullStack(r.stdout.toString('utf8')), [
|
94 | 'TAP version 13',
|
95 | '# async4',
|
96 | 'ok 1 before await',
|
97 | 'not ok 2 Error: oops',
|
98 | ' ---',
|
99 | ' operator: error',
|
100 | ' at: Test.myTest ($TEST/async-await/async4.js:$LINE:$COL)',
|
101 | ' stack: |-',
|
102 | ' Error: oops',
|
103 | ' at Timeout.myTimeout [as _onTimeout] ($TEST/async-await/async4.js:$LINE:$COL)',
|
104 | ' [... stack stripped ...]',
|
105 | ' ...',
|
106 | '',
|
107 | '1..2',
|
108 | '# tests 2',
|
109 | '# pass 1',
|
110 | '# fail 1'
|
111 | ].join('\n') + '\n\n');
|
112 | t.same(r.exitCode, 1);
|
113 | t.same(r.stderr.toString('utf8'), '');
|
114 | t.end();
|
115 | });
|
116 | });
|
117 |
|
118 | tap.test('async5', function (t) {
|
119 | runProgram('async-await', 'async5.js', function (r) {
|
120 | t.same(stripFullStack(r.stdout.toString('utf8')), [
|
121 | 'TAP version 13',
|
122 | '# async5',
|
123 | 'ok 1 before server',
|
124 | 'ok 2 after server',
|
125 | 'ok 3 before request',
|
126 | 'ok 4 after request',
|
127 | 'ok 5 res.statusCode is 200',
|
128 | 'not ok 6 .end() already called: mockDb.state is new',
|
129 | ' ---',
|
130 | ' operator: fail',
|
131 | ' at: Timeout._onTimeout ($TEST/async-await/async5.js:$LINE:$COL)',
|
132 | ' stack: |-',
|
133 | ' Error: .end() already called: mockDb.state is new',
|
134 | ' [... stack stripped ...]',
|
135 | ' at Timeout._onTimeout ($TEST/async-await/async5.js:$LINE:$COL)',
|
136 | ' [... stack stripped ...]',
|
137 | ' ...',
|
138 | 'not ok 7 .end() already called: error on close',
|
139 | ' ---',
|
140 | ' operator: fail',
|
141 | ' at: Server.<anonymous> ($TEST/async-await/async5.js:$LINE:$COL)',
|
142 | ' stack: |-',
|
143 | ' Error: .end() already called: error on close',
|
144 | ' [... stack stripped ...]',
|
145 | ' at Server.<anonymous> ($TEST/async-await/async5.js:$LINE:$COL)',
|
146 | ' [... stack stripped ...]',
|
147 | ' ...',
|
148 | 'not ok 8 .end() already called',
|
149 | ' ---',
|
150 | ' operator: fail',
|
151 | ' at: Server.<anonymous> ($TEST/async-await/async5.js:$LINE:$COL)',
|
152 | ' stack: |-',
|
153 | ' Error: .end() already called',
|
154 | ' [... stack stripped ...]',
|
155 | ' at Server.<anonymous> ($TEST/async-await/async5.js:$LINE:$COL)',
|
156 | ' [... stack stripped ...]',
|
157 | ' ...',
|
158 | '',
|
159 | '1..8',
|
160 | '# tests 8',
|
161 | '# pass 5',
|
162 | '# fail 3'
|
163 | ].join('\n') + '\n\n');
|
164 | t.same(r.exitCode, 1);
|
165 | t.same(r.stderr.toString('utf8'), '');
|
166 | t.end();
|
167 | });
|
168 | });
|
169 |
|
170 | tap.test('sync-error', function (t) {
|
171 | runProgram('async-await', 'sync-error.js', function (r) {
|
172 | t.same(stripFullStack(r.stdout.toString('utf8')), [
|
173 | 'TAP version 13',
|
174 | '# sync-error',
|
175 | 'ok 1 before throw',
|
176 | ''
|
177 | ].join('\n'));
|
178 | t.same(r.exitCode, 1);
|
179 |
|
180 | var stderr = r.stderr.toString('utf8');
|
181 | var lines = stderr.split('\n');
|
182 | lines = lines.filter(function (line) {
|
183 | return ! /\(timers.js:/.test(line) &&
|
184 | ! /\(internal\/timers.js:/.test(line) &&
|
185 | ! /Immediate\.next/.test(line);
|
186 | });
|
187 | stderr = lines.join('\n');
|
188 |
|
189 | t.same(stripFullStack(stderr), [
|
190 | '$TEST/async-await/sync-error.js:7',
|
191 | ' throw new Error(\'oopsie\');',
|
192 | ' ^',
|
193 | '',
|
194 | 'Error: oopsie',
|
195 | ' at Test.myTest ($TEST/async-await/sync-error.js:$LINE:$COL)',
|
196 | ' at Test.bound [as _cb] ($TAPE/lib/test.js:$LINE:$COL)',
|
197 | ' at Test.run ($TAPE/lib/test.js:$LINE:$COL)',
|
198 | ' at Test.bound [as run] ($TAPE/lib/test.js:$LINE:$COL)',
|
199 | ''
|
200 | ].join('\n'));
|
201 | t.end();
|
202 | });
|
203 | });
|
204 |
|
205 | tap.test('async-error', function (t) {
|
206 | runProgram('async-await', 'async-error.js', function (r) {
|
207 | var stdout = r.stdout.toString('utf8');
|
208 | var lines = stdout.split('\n');
|
209 | lines = lines.filter(function (line) {
|
210 | return ! /^(\s+)at(\s+)<anonymous>$/.test(line);
|
211 | });
|
212 | stdout = lines.join('\n');
|
213 |
|
214 | t.same(stripFullStack(stdout.toString('utf8')), [
|
215 | 'TAP version 13',
|
216 | '# async-error',
|
217 | 'ok 1 before throw',
|
218 | 'not ok 2 Error: oopsie',
|
219 | ' ---',
|
220 | ' operator: fail',
|
221 | ' stack: |-',
|
222 | ' Error: Error: oopsie',
|
223 | ' [... stack stripped ...]',
|
224 | ' ...',
|
225 | '',
|
226 | '1..2',
|
227 | '# tests 2',
|
228 | '# pass 1',
|
229 | '# fail 1',
|
230 | '',
|
231 | '',
|
232 | ].join('\n'));
|
233 | t.same(r.exitCode, 1);
|
234 |
|
235 | var stderr = r.stderr.toString('utf8');
|
236 | var lines = stderr.split('\n');
|
237 | lines = lines.filter(function (line) {
|
238 | return ! /\(timers.js:/.test(line) &&
|
239 | ! /\(internal\/timers.js:/.test(line) &&
|
240 | ! /Immediate\.next/.test(line);
|
241 | });
|
242 | stderr = lines.join('\n');
|
243 |
|
244 | t.same(stderr, '');
|
245 | t.end();
|
246 | });
|
247 | });
|