Skip to content

Commit c16dde3

Browse files
committed
[Fix] assertion: pass through assertion return value, for promises
1 parent 998d9cd commit c16dde3

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

lib/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ Test.prototype.doesNotMatch = function doesNotMatch(string, regexp, msg, extra)
972972
};
973973

974974
Test.prototype.assertion = function assertion(fn) {
975-
callBind.apply(fn)(this, $slice(arguments, 1));
975+
return callBind.apply(fn)(this, $slice(arguments, 1));
976976
};
977977

978978
// eslint-disable-next-line no-unused-vars

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"npmignore": "^0.3.1",
6262
"nyc": "^10.3.2",
6363
"safe-publish-latest": "^2.0.0",
64+
"semver": "^6.3.1",
6465
"tap": "^8.0.1",
6566
"tap-parser": "^5.4.0"
6667
},

test/assertion.js

+50-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var tape = require('../');
44
var tap = require('tap');
55
var concat = require('concat-stream');
6+
var satisfies = require('semver').satisfies;
67

78
var stripFullStack = require('./common').stripFullStack;
89

@@ -32,17 +33,34 @@ tap.test('using a custom assertion', function (tt) {
3233
' at Test.<anonymous> ($TEST/assertion.js:$LINE:$COL)',
3334
' [... stack stripped ...]',
3435
' ...',
36+
typeof Promise === 'undefined'
37+
? '# SKIP custom assertion returns a promise'
38+
: [].concat(
39+
'# custom assertion returns a promise',
40+
'ok ' + ++count + ' promise rejected!',
41+
'not ok ' + ++count + ' SyntaxError: expected promise to reject; it fulfilled',
42+
' ---',
43+
' operator: error',
44+
' stack: |-',
45+
' SyntaxError: expected promise to reject; it fulfilled',
46+
' at $TEST/assertion.js:$LINE:$COL',
47+
satisfies(process.version, '^8 || ^9')
48+
? ' at <anonymous>'
49+
: [],
50+
' [... stack stripped ...]',
51+
' ...'
52+
),
3553
'',
3654
'1..' + count,
3755
'# tests ' + count,
38-
'# pass ' + (count - 1),
39-
'# fail 1',
56+
'# pass ' + (count - (typeof Promise === 'undefined' ? 1 : 2)),
57+
'# fail ' + (typeof Promise === 'undefined' ? 1 : 2),
4058
''
4159
));
4260
}));
4361

4462
var isAnswer = function (value, msg) {
45-
// eslint-disable-next-line no-invalid-this
63+
4664
this.equal(value, 42, msg || 'value must be the answer to life, the universe, and everything');
4765
};
4866

@@ -54,4 +72,33 @@ tap.test('using a custom assertion', function (tt) {
5472

5573
t.end();
5674
});
75+
76+
var rejects = function assertRejects(fn, expected, msg, extra) {
77+
var t = this;
78+
/* eslint no-invalid-this: 0 */
79+
return new Promise(function (resolve) { resolve(fn()); }).then(
80+
function () {
81+
throw new SyntaxError('expected promise to reject; it fulfilled');
82+
},
83+
function (err) {
84+
t['throws'](function () { throw err; }, expected, msg, extra);
85+
}
86+
);
87+
};
88+
89+
test('custom assertion returns a promise', { skip: typeof Promise !== 'function' }, function (t) {
90+
return Promise.all([
91+
t.assertion(
92+
rejects,
93+
function () { return Promise.resolve(); },
94+
SyntaxError,
95+
'expected promise to reject; it fulfilled'
96+
),
97+
t.assertion(
98+
rejects,
99+
function () { return Promise.reject(new Error('foo')); },
100+
'promise rejected!'
101+
)
102+
]);
103+
});
57104
});

test/common.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,18 @@ module.exports.stripFullStack = function (output) {
8888
'at$1 Test.<anonymous>'
8989
).replace(
9090
// Handle stack trace variation in Node v0.8
91-
/at(:?) (Test\.)?tap\.test\.test\.skip/g,
92-
'at$1 $2<anonymous>'
93-
).replace(
94-
// Handle more stack trace variation in Node v0.8
9591
/at(:?) Test.tap.test.([^ ]+)/g,
9692
'at$1 Test.$2'
9793
).replace(
9894
// Handle stack trace variation in Node v0.8
9995
/(\[\.\.\. stack stripped \.\.\.\]\r?\n *at) <anonymous> \(([^)]+)\)/g,
10096
'$1 $2'
101-
).split(/\r?\n/g);
97+
).replace(
98+
// Handle stack trace variation in Node v0.8
99+
/at(:?) (Test\.)?(tap\.test\.test\.skip|t) /g,
100+
'at$1 $2<anonymous> '
101+
)
102+
.split(/\r?\n/g);
102103
};
103104

104105
module.exports.runProgram = function (folderName, fileName, cb) {

0 commit comments

Comments
 (0)