Skip to content

Commit 7ba18ac

Browse files
committed
[New] add t.assertion
Fixes #555
1 parent 7b39e14 commit 7ba18ac

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

lib/test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,10 @@ Test.prototype.doesNotMatch = function doesNotMatch(string, regexp, msg, extra)
971971
}
972972
};
973973

974+
Test.prototype.assertion = function assertion(fn) {
975+
callBind.apply(fn)(this, $slice(arguments, 1));
976+
};
977+
974978
// eslint-disable-next-line no-unused-vars
975979
Test.skip = function skip(name_, _opts, _cb) {
976980
var args = getTestArgs.apply(null, arguments);

readme.markdown

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,25 @@ Providing both `desc.get` and `desc.set` are optional and can still be useful fo
437437
`desc` must be a valid property descriptor, meaning that `get`/`set` are mutually exclusive with `writable`/`value`.
438438
Additionally, explicitly setting `configurable` to `false` is not permitted, so that the property can be restored.
439439

440+
## t.assertion(fn, ...args)
441+
442+
If you want to write your own custom assertions, you can invoke these conveniently using this method.
443+
444+
```js
445+
function isAnswer(value, msg) {
446+
// eslint-disable-next-line no-invalid-this
447+
this.equal(value, 42, msg || 'value must be the answer to life, the universe, and everything');
448+
};
449+
450+
test('is this the answer?', (t) => {
451+
t.assertion(isAnswer, 42); // passes, default message
452+
t.assertion(isAnswer, 42, 'what is 6 * 9?'); // passes, custom message
453+
t.assertion(isAnswer, 54, 'what is 6 * 9!'); // fails, custom message
454+
455+
t.end();
456+
});
457+
```
458+
440459
## var htest = test.createHarness()
441460

442461
Create a new test harness instance, which is a function like `test()`, but with a new pending stack and test state.

test/assertion.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
var tape = require('../');
4+
var tap = require('tap');
5+
var concat = require('concat-stream');
6+
7+
var stripFullStack = require('./common').stripFullStack;
8+
9+
tap.test('using a custom assertion', function (tt) {
10+
tt.plan(1);
11+
12+
var test = tape.createHarness();
13+
var count = 0;
14+
test.createStream().pipe(concat(function (body) {
15+
tt.same(stripFullStack(body.toString('utf8')), [].concat(
16+
'TAP version 13',
17+
'# with a custom assertion',
18+
'ok ' + ++count + ' true is ok',
19+
'ok ' + ++count + ' value must be the answer to life, the universe, and everything',
20+
'ok ' + ++count + ' what is 6 * 9?',
21+
'not ok ' + ++count + ' what is 6 * 9!',
22+
' ---',
23+
' operator: equal',
24+
' expected: 42',
25+
' actual: 54',
26+
' at: Test.isAnswer ($TEST/assertion.js:$LINE:$COL)',
27+
' stack: |-',
28+
' Error: what is 6 * 9!',
29+
' [... stack stripped ...]',
30+
' at Test.isAnswer ($TEST/assertion.js:$LINE:$COL)',
31+
' [... stack stripped ...]',
32+
' at Test.<anonymous> ($TEST/assertion.js:$LINE:$COL)',
33+
' [... stack stripped ...]',
34+
' ...',
35+
'',
36+
'1..' + count,
37+
'# tests ' + count,
38+
'# pass ' + (count - 1),
39+
'# fail 1',
40+
''
41+
));
42+
}));
43+
44+
var isAnswer = function (value, msg) {
45+
// eslint-disable-next-line no-invalid-this
46+
this.equal(value, 42, msg || 'value must be the answer to life, the universe, and everything');
47+
};
48+
49+
test('with a custom assertion', function (t) {
50+
t.ok(true, 'true is ok');
51+
t.assertion(isAnswer, 42);
52+
t.assertion(isAnswer, 42, 'what is 6 * 9?');
53+
t.assertion(isAnswer, 54, 'what is 6 * 9!');
54+
55+
t.end();
56+
});
57+
});

0 commit comments

Comments
 (0)