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