33var tape = require ( '../' ) ;
44var tap = require ( 'tap' ) ;
55var concat = require ( 'concat-stream' ) ;
6+ var satisfies = require ( 'semver' ) . satisfies ;
67
78var 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} ) ;
0 commit comments