Skip to content

Commit 6385798

Browse files
committed
Modify behavior of runUntil test option
1 parent 1a479bc commit 6385798

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

lib/sandbox/pmapi-setup-runner.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ const _ = require('lodash'),
1212

1313
OPTIONS = {
1414
When: 'when',
15-
RunCount: 'runCount',
16-
RunUntil: 'runUntil'
15+
Count: 'count',
16+
Timeout: 'timeout'
1717
},
1818
OPTION_TYPE = {
1919
[OPTIONS.When]: 'function',
20-
[OPTIONS.RunCount]: 'number',
21-
[OPTIONS.RunUntil]: 'number'
20+
[OPTIONS.Count]: 'number',
21+
[OPTIONS.Timeout]: 'number'
2222
};
2323

2424
/**
@@ -76,7 +76,7 @@ module.exports = function (pm, testsState, onAssertion) {
7676
...(options ? _.pick(options, _.values(OPTIONS)) : {}),
7777
testId: uuid(),
7878
timer: null,
79-
currRunCount: 0,
79+
runCount: 0,
8080
pending: true
8181
};
8282
},
@@ -120,6 +120,8 @@ module.exports = function (pm, testsState, onAssertion) {
120120
assertionData.passed = false;
121121
},
122122

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
123125
processAssertion = function (_testId, assertionData, options) {
124126
const testState = testsState[_testId];
125127

@@ -135,8 +137,8 @@ module.exports = function (pm, testsState, onAssertion) {
135137
assertionData.error || // TODO: Make conditions (test status) to mark a test resolved, configurable.
136138
assertionData.skipped ||
137139
_.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
140142
);
141143

142144
testState.pending = assertionData.pending = !shouldResolve;
@@ -149,23 +151,35 @@ module.exports = function (pm, testsState, onAssertion) {
149151
onAssertion(assertionData);
150152
},
151153

154+
155+
// Processes the provided options and updates the test state accordingly every time a test spec is encountered
152156
processOptions = function (_testId, assertionData, options) {
153157
const testState = testsState[_testId],
154158
shouldRun = testState.pending &&
155159
(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;
157165

158166
if (shouldRun) {
159-
testState.currRunCount++;
167+
testState.runCount++;
168+
}
160169

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);
162175

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);
169183
}
170184

171185
return shouldRun;

0 commit comments

Comments
 (0)