Skip to content

Commit

Permalink
[Fix] assertion: pass through assertion return value, for promises
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jun 16, 2024
1 parent 998d9cd commit 2ad86d4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ Test.prototype.doesNotMatch = function doesNotMatch(string, regexp, msg, extra)
};

Test.prototype.assertion = function assertion(fn) {
callBind.apply(fn)(this, $slice(arguments, 1));
return callBind.apply(fn)(this, $slice(arguments, 1));
};

// eslint-disable-next-line no-unused-vars
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"npmignore": "^0.3.1",
"nyc": "^10.3.2",
"safe-publish-latest": "^2.0.0",
"semver": "^6.3.1",
"tap": "^8.0.1",
"tap-parser": "^5.4.0"
},
Expand Down
53 changes: 50 additions & 3 deletions test/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var tape = require('../');
var tap = require('tap');
var concat = require('concat-stream');
var satisfies = require('semver').satisfies;

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

Expand Down Expand Up @@ -32,17 +33,34 @@ tap.test('using a custom assertion', function (tt) {
' at Test.<anonymous> ($TEST/assertion.js:$LINE:$COL)',
' [... stack stripped ...]',
' ...',
typeof Promise === 'undefined'
? '# SKIP custom assertion returns a promise'
: [].concat(
'# custom assertion returns a promise',
'ok ' + ++count + ' promise rejected!',
'not ok ' + ++count + ' SyntaxError: expected promise to reject; it fulfilled',
' ---',
' operator: error',
' stack: |-',
' SyntaxError: expected promise to reject; it fulfilled',
' at $TEST/assertion.js:$LINE:$COL',
satisfies(process.version, '^8 || ^9')
? ' at <anonymous>'
: [],
' [... stack stripped ...]',
' ...'
),
'',
'1..' + count,
'# tests ' + count,
'# pass ' + (count - 1),
'# fail 1',
'# pass ' + (count - (typeof Promise === 'undefined' ? 1 : 2)),
'# fail ' + (typeof Promise === 'undefined' ? 1 : 2),
''
));
}));

var isAnswer = function (value, msg) {
// eslint-disable-next-line no-invalid-this

this.equal(value, 42, msg || 'value must be the answer to life, the universe, and everything');
};

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

t.end();
});

var rejects = function assertRejects(fn, expected, msg, extra) {
var t = this;
/* eslint no-invalid-this: 0 */
return new Promise(function (resolve) { resolve(fn()); }).then(
function () {
throw new SyntaxError('expected promise to reject; it fulfilled');
},
function (err) {
t['throws'](function () { throw err; }, expected, msg, extra);
}
);
};

test('custom assertion returns a promise', { skip: typeof Promise !== 'function' }, function (t) {
return Promise.all([
t.assertion(
rejects,
function () { return Promise.resolve(); },
SyntaxError,
'expected promise to reject; it fulfilled'
),
t.assertion(
rejects,
function () { return Promise.reject(new Error('foo')); },
'promise rejected!'
)
]);
});
});
9 changes: 7 additions & 2 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,15 @@ module.exports.stripFullStack = function (output) {
/at(:?) Test.tap.test.([^ ]+)/g,
'at$1 Test.$2'
).replace(
// Handle stack trace variation in Node v0.8
// Handle more stack trace variation in Node v0.8
/(\[\.\.\. stack stripped \.\.\.\]\r?\n *at) <anonymous> \(([^)]+)\)/g,
'$1 $2'
).split(/\r?\n/g);
).replace(
// Handle more stack trace variation in Node v0.8
/at(:?) Test\.t /g,
'at$1 Test.<anonymous> '
)
.split(/\r?\n/g);
};

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

0 comments on commit 2ad86d4

Please sign in to comment.