@@ -26,6 +26,17 @@ const text = `
26
26
When I go to checkout process
27
27
` ;
28
28
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
+
29
40
describe ( 'BDD' , ( ) => {
30
41
beforeEach ( ( ) => {
31
42
clearSteps ( ) ;
@@ -82,18 +93,49 @@ describe('BDD', () => {
82
93
} ) ;
83
94
} ) ;
84
95
85
- it ( 'should allow failed steps' , ( done ) => {
96
+ it ( 'should allow failed steps' , async ( ) => {
86
97
let sum = 0 ;
87
98
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 ) ;
89
100
const suite = run ( text ) ;
90
101
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
+ }
97
139
} ) ;
98
140
99
141
it ( 'should work with async functions' , ( done ) => {
0 commit comments