Skip to content

Commit 2ad86d4

Browse files
committedJun 16, 2024··
[Fix] assertion: pass through assertion return value, for promises
1 parent 998d9cd commit 2ad86d4

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
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

+7-2
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,15 @@ module.exports.stripFullStack = function (output) {
9595
/at(:?) Test.tap.test.([^ ]+)/g,
9696
'at$1 Test.$2'
9797
).replace(
98-
// Handle stack trace variation in Node v0.8
98+
// Handle more stack trace variation in Node v0.8
9999
/(\[\.\.\. stack stripped \.\.\.\]\r?\n *at) <anonymous> \(([^)]+)\)/g,
100100
'$1 $2'
101-
).split(/\r?\n/g);
101+
).replace(
102+
// Handle more stack trace variation in Node v0.8
103+
/at(:?) Test\.t /g,
104+
'at$1 Test.<anonymous> '
105+
)
106+
.split(/\r?\n/g);
102107
};
103108

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

0 commit comments

Comments
 (0)
Please sign in to comment.