MCPcopy Index your code
hub / github.com/codeceptjs/CodeceptJS / tryTo

Function tryTo

lib/effects.js:278–317  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

276 * @throws Will handle errors internally, logging them and returning `false` as the result.
277 */
278async 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
319export { hopeThat, retryTo, tryTo, within }
320

Callers 4

effects_test.jsFile · 0.90

Calls 5

callbackFunction · 0.85
debugMethod · 0.80
addMethod · 0.45
startMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected