@@ -12,13 +12,13 @@ const _ = require('lodash'),
12
12
13
13
OPTIONS = {
14
14
When : 'when' ,
15
- RunCount : 'runCount ' ,
16
- RunUntil : 'runUntil '
15
+ Count : 'count ' ,
16
+ Timeout : 'timeout '
17
17
} ,
18
18
OPTION_TYPE = {
19
19
[ OPTIONS . When ] : 'function' ,
20
- [ OPTIONS . RunCount ] : 'number' ,
21
- [ OPTIONS . RunUntil ] : 'number'
20
+ [ OPTIONS . Count ] : 'number' ,
21
+ [ OPTIONS . Timeout ] : 'number'
22
22
} ;
23
23
24
24
/**
@@ -76,7 +76,7 @@ module.exports = function (pm, testsState, onAssertion) {
76
76
...( options ? _ . pick ( options , _ . values ( OPTIONS ) ) : { } ) ,
77
77
testId : uuid ( ) ,
78
78
timer : null ,
79
- currRunCount : 0 ,
79
+ runCount : 0 ,
80
80
pending : true
81
81
} ;
82
82
} ,
@@ -120,6 +120,8 @@ module.exports = function (pm, testsState, onAssertion) {
120
120
assertionData . passed = false ;
121
121
} ,
122
122
123
+ // Processes the assertion data and test state to determine if the assertion should be resolved or not
124
+ // and then calls the onAssertion callback
123
125
processAssertion = function ( _testId , assertionData , options ) {
124
126
const testState = testsState [ _testId ] ;
125
127
@@ -135,8 +137,8 @@ module.exports = function (pm, testsState, onAssertion) {
135
137
assertionData . error || // TODO: Make conditions (test status) to mark a test resolved, configurable.
136
138
assertionData . skipped ||
137
139
_ . isEmpty ( options ) ||
138
- isOptionConfigured ( options , OPTIONS . RunCount ) && testState . runCount === testState . currRunCount ||
139
- isOptionConfigured ( options , OPTIONS . RunUntil ) && testState . currRunCount && ! testState . timer
140
+ isOptionConfigured ( options , OPTIONS . Count ) && testState . count === testState . runCount ||
141
+ isOptionConfigured ( options , OPTIONS . Timeout ) && testState . runCount && ! testState . timer
140
142
) ;
141
143
142
144
testState . pending = assertionData . pending = ! shouldResolve ;
@@ -149,23 +151,35 @@ module.exports = function (pm, testsState, onAssertion) {
149
151
onAssertion ( assertionData ) ;
150
152
} ,
151
153
154
+
155
+ // Processes the provided options and updates the test state accordingly every time a test spec is encountered
152
156
processOptions = function ( _testId , assertionData , options ) {
153
157
const testState = testsState [ _testId ] ,
154
158
shouldRun = testState . pending &&
155
159
( isOptionConfigured ( options , OPTIONS . When ) ? Boolean ( options . when ( ) ) : true ) &&
156
- ( isOptionConfigured ( options , OPTIONS . RunCount ) ? testState . currRunCount < options . runCount : true ) ;
160
+ ( isOptionConfigured ( options , OPTIONS . Count ) ? testState . runCount < options . count : true ) ,
161
+ startTimer =
162
+ isOptionConfigured ( options , OPTIONS . Timeout ) &&
163
+ testState . runCount === 0 &&
164
+ ! testState . timer ;
157
165
158
166
if ( shouldRun ) {
159
- testState . currRunCount ++ ;
167
+ testState . runCount ++ ;
168
+ }
160
169
161
- const startTimer = isOptionConfigured ( options , OPTIONS . RunUntil ) && ! testState . timer ;
170
+ if ( startTimer ) {
171
+ testState . timer = setTimeout ( ( ) => {
172
+ const shouldFail =
173
+ ( ! testState . count && testState . runCount === 0 ) ||
174
+ ( isOptionConfigured ( options , OPTIONS . Count ) && testState . runCount < options . count ) ;
162
175
163
- if ( startTimer ) {
164
- testState . timer = setTimeout ( ( ) => {
165
- testState . timer = null ;
166
- processAssertion ( _testId , assertionData , options ) ;
167
- } , testState . runUntil ) ;
168
- }
176
+ if ( shouldFail ) {
177
+ markAssertionAsFailure ( assertionData , new Error ( 'Test timed out' ) ) ;
178
+ }
179
+
180
+ testState . timer = null ;
181
+ processAssertion ( _testId , assertionData , options ) ;
182
+ } , testState . timeout ) ;
169
183
}
170
184
171
185
return shouldRun ;
0 commit comments