Skip to content

Commit

Permalink
[Breaking] if a test callback returns a rejected thenable, fail the t…
Browse files Browse the repository at this point in the history
…est.

Also, implicitly call `.end()` if not already called when a Promise is returned, because the promise itself marks the end of the test.
  • Loading branch information
ljharb committed Jun 24, 2019
1 parent 8d3f03a commit f248610
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 40 deletions.
19 changes: 11 additions & 8 deletions lib/test.js
Expand Up @@ -97,15 +97,18 @@ Test.prototype.run = function () {
if (
typeof Promise === 'function' &&
callbackReturn &&
typeof callbackReturn.then === 'function' &&
typeof callbackReturn.catch === 'function'
typeof callbackReturn.then === 'function'
) {
callbackReturn.catch(function onError(err) {
nextTick(function rethrowError() {
throw err
})
})
return
var self = this;
Promise.resolve(callbackReturn).then(function onResolve() {
if (!self.calledEnd) {
self.end();
}
}).catch(function onError(err) {
self.fail(err);
self.end();
});
return;
}

this.emit('run');
Expand Down
35 changes: 18 additions & 17 deletions test/async-await.js
Expand Up @@ -128,11 +128,10 @@ tap.test('async5', function (t) {
'ok 4 after request',
'ok 5 should be equal',
'ok 6 should be equal',
'ok 7 undefined',
'',
'1..7',
'# tests 7',
'# pass 7',
'1..6',
'# tests 6',
'# pass 6',
'',
'# ok'
].join('\n') + '\n\n');
Expand Down Expand Up @@ -190,7 +189,20 @@ tap.test('async-error', function (t) {
'TAP version 13',
'# async-error',
'ok 1 before throw',
''
'not ok 2 Error: oopsie',
' ---',
' operator: fail',
' stack: |-',
' Error: Error: oopsie',
' [... stack stripped ...]',
' ...',
'',
'1..2',
'# tests 2',
'# pass 1',
'# fail 1',
'',
'',
].join('\n'));
t.same(r.exitCode, 1);

Expand All @@ -203,18 +215,7 @@ tap.test('async-error', function (t) {
});
stderr = lines.join('\n');

t.same(stripFullStack(stderr), [
'$TAPE/lib/test.js:106',
' throw err',
' ^',
'',
'Error: oopsie',
' at Test.myTest ($TEST/async-await/async-error.js:$LINE:$COL)',
' at Test.bound [as _cb] ($TAPE/lib/test.js:$LINE:$COL)',
' at Test.run ($TAPE/lib/test.js:$LINE:$COL)',
' at Test.bound [as run] ($TAPE/lib/test.js:$LINE:$COL)',
''
].join('\n'));
t.same(stderr, '');
t.end();
});
});
1 change: 0 additions & 1 deletion test/async-await/async-error.js
Expand Up @@ -4,5 +4,4 @@ test('async-error', async function myTest(t) {
t.ok(true, 'before throw');
throw new Error('oopsie');
t.ok(true, 'after throw');
t.end();
});
2 changes: 0 additions & 2 deletions test/async-await/async1.js
Expand Up @@ -7,9 +7,7 @@ test('async1', async function myTest(t) {
setTimeout(resolve, 10);
});
t.ok(true, 'after await');
t.end();
} catch (err) {
t.ifError(err);
t.end();
}
});
2 changes: 0 additions & 2 deletions test/async-await/async2.js
Expand Up @@ -7,9 +7,7 @@ test('async2', async function myTest(t) {
setTimeout(resolve, 10);
});
t.ok(false, 'after await');
t.end();
} catch (err) {
t.ifError(err);
t.end();
}
});
1 change: 0 additions & 1 deletion test/async-await/async3.js
Expand Up @@ -6,5 +6,4 @@ test('async3', async function myTest(t) {
setTimeout(resolve, 10);
});
t.ok(true, 'after await');
t.end();
});
2 changes: 0 additions & 2 deletions test/async-await/async4.js
Expand Up @@ -9,9 +9,7 @@ test('async4', async function myTest(t) {
}, 10);
});
t.ok(true, 'after await');
t.end();
} catch (err) {
t.ifError(err);
t.end();
}
});
16 changes: 9 additions & 7 deletions test/async-await/async5.js
Expand Up @@ -42,14 +42,16 @@ test('async5', async function myTest(t) {
res.resume();
t.equal(res.statusCode, 200);

setTimeout(function () {
t.equal(mockDb.state, 'new');
await new Promise(function (resolve, reject) {
setTimeout(function () {
t.equal(mockDb.state, 'new');

server.close(function (err) {
t.ifError(err);
t.end();
});
}, 50);
server.close(function (err) {
if (err) { reject(err); }
else { resolve(); }
});
}, 50);
});
} catch (err) {
t.ifError(err);
t.end();
Expand Down

0 comments on commit f248610

Please sign in to comment.