* A CodeceptJS utility function to attempt a step or callback without failing the test. * If the step fails, the test continues execution without interruption, and the result is logged. * * @async * @function tryTo * @param {Function} callback - The function to execute, which may succeed or fai
(callback)
| 276 | * @throws Will handle errors internally, logging them and returning `false` as the result. |
| 277 | */ |
| 278 | async function tryTo(callback) { |
| 279 | if (store.dryRun) return |
| 280 | const sessionName = 'tryTo' |
| 281 | |
| 282 | let result = false |
| 283 | let isAutoRetriesEnabled = store.autoRetries |
| 284 | return recorder.add( |
| 285 | sessionName, |
| 286 | () => { |
| 287 | recorder.session.start(sessionName) |
| 288 | isAutoRetriesEnabled = store.autoRetries |
| 289 | if (isAutoRetriesEnabled) output.debug('Auto retries disabled inside tryTo effect') |
| 290 | store.autoRetries = false |
| 291 | callback() |
| 292 | recorder.add(() => { |
| 293 | result = true |
| 294 | recorder.session.restore(sessionName) |
| 295 | return result |
| 296 | }) |
| 297 | recorder.session.catch(err => { |
| 298 | result = false |
| 299 | const msg = err.inspect ? err.inspect() : err.toString() |
| 300 | output.debug(`Unsuccessful try > ${msg}`) |
| 301 | recorder.session.restore(sessionName) |
| 302 | return result |
| 303 | }) |
| 304 | return recorder.add( |
| 305 | 'result', |
| 306 | () => { |
| 307 | store.autoRetries = isAutoRetriesEnabled |
| 308 | return result |
| 309 | }, |
| 310 | true, |
| 311 | false, |
| 312 | ) |
| 313 | }, |
| 314 | false, |
| 315 | false, |
| 316 | ) |
| 317 | } |
| 318 | |
| 319 | export { hopeThat, retryTo, tryTo, within } |
| 320 |
no test coverage detected