@@ -26,6 +26,17 @@ const text = `
2626 When I go to checkout process
2727` ;
2828
29+ const checkTestForErrors = ( test ) => {
30+ return new Promise ( ( resolve , reject ) => {
31+ test . fn ( ( err ) => {
32+ if ( err ) {
33+ return reject ( err ) ;
34+ }
35+ resolve ( ) ;
36+ } ) ;
37+ } ) ;
38+ } ;
39+
2940describe ( 'BDD' , ( ) => {
3041 beforeEach ( ( ) => {
3142 clearSteps ( ) ;
@@ -82,18 +93,49 @@ describe('BDD', () => {
8293 } ) ;
8394 } ) ;
8495
85- it ( 'should allow failed steps' , ( done ) => {
96+ it ( 'should allow failed steps' , async ( ) => {
8697 let sum = 0 ;
8798 Given ( / I h a v e p r o d u c t w i t h ( \d + ) p r i c e / , param => sum += parseInt ( param , 10 ) ) ;
88- When ( 'I go to checkout process' , ( ) => expect ( false ) . is . false ) ;
99+ When ( 'I go to checkout process' , ( ) => expect ( false ) . is . true ) ;
89100 const suite = run ( text ) ;
90101 expect ( 'checkout process' ) . is . equal ( suite . title ) ;
91- let errored = false ;
92- suite . tests [ 0 ] . fn ( ( err ) => {
93- errored = ! ! err ;
94- expect ( errored ) . is . exist ;
95- done ( ) ;
96- } ) ;
102+ try {
103+ await checkTestForErrors ( suite . tests [ 0 ] ) ;
104+ return Promise . reject ( ( new Error ( 'Test should have thrown with failed step, but did not' ) ) ) ;
105+ } catch ( err ) {
106+ const errored = ! ! err ;
107+ expect ( errored ) . is . true ;
108+ }
109+ } ) ;
110+
111+ it ( 'handles errors in steps' , async ( ) => {
112+ let sum = 0 ;
113+ Given ( / I h a v e p r o d u c t w i t h ( \d + ) p r i c e / , param => sum += parseInt ( param , 10 ) ) ;
114+ When ( 'I go to checkout process' , ( ) => { throw new Error ( 'errored step' ) ; } ) ;
115+ const suite = run ( text ) ;
116+ expect ( 'checkout process' ) . is . equal ( suite . title ) ;
117+ try {
118+ await checkTestForErrors ( suite . tests [ 0 ] ) ;
119+ return Promise . reject ( ( new Error ( 'Test should have thrown with error, but did not' ) ) ) ;
120+ } catch ( err ) {
121+ const errored = ! ! err ;
122+ expect ( errored ) . is . true ;
123+ }
124+ } ) ;
125+
126+ it ( 'handles async errors in steps' , async ( ) => {
127+ let sum = 0 ;
128+ Given ( / I h a v e p r o d u c t w i t h ( \d + ) p r i c e / , param => sum += parseInt ( param , 10 ) ) ;
129+ When ( 'I go to checkout process' , ( ) => Promise . reject ( new Error ( 'step failed' ) ) ) ;
130+ const suite = run ( text ) ;
131+ expect ( 'checkout process' ) . is . equal ( suite . title ) ;
132+ try {
133+ await checkTestForErrors ( suite . tests [ 0 ] ) ;
134+ return Promise . reject ( ( new Error ( 'Test should have thrown with error, but did not' ) ) ) ;
135+ } catch ( err ) {
136+ const errored = ! ! err ;
137+ expect ( errored ) . is . true ;
138+ }
97139 } ) ;
98140
99141 it ( 'should work with async functions' , ( done ) => {
0 commit comments