MCPcopy
hub / github.com/codeceptjs/CodeceptJS / enableAuthBeforeEachTest

Function enableAuthBeforeEachTest

lib/plugin/auth.js:364–403  ·  view source on GitHub ↗
(name)

Source from the content-addressed store, hash-verified

362 }
363
364 function enableAuthBeforeEachTest(name) {
365 const suite = store.currentSuite
366 if (!suite) return
367
368 output.debug(`enabling auth as ${name} for each test of suite ${suite.title}`)
369
370 // we are setting test opts so they can be picked up by Playwright if it starts browser for this test
371 suite.eachTest(test => {
372 // preload from store
373 if (store[`${name}_session`]) {
374 test.opts.cookies = store[`${name}_session`]
375 test.opts.user = name
376 return
377 }
378
379 if (!config.saveToFile) return
380 const cookieFile = path.join(store.outputDir, `${name}_session.json`)
381
382 if (!fileExists(cookieFile)) {
383 return
384 }
385
386 const context = fs.readFileSync(cookieFile).toString()
387 test.opts.cookies = JSON.parse(context)
388 test.opts.user = name
389 })
390
391 function runLoginFunctionForTest(test) {
392 if (!suite.tests.includes(test)) return
393 // let's call this function to ensure that authorization happened
394 // if no cookies, it will login and save them
395 loginFunction(name)
396 }
397
398 // we are in BeforeSuite hook
399 event.dispatcher.on(event.test.started, runLoginFunctionForTest)
400 event.dispatcher.on(event.suite.after, () => {
401 event.dispatcher.off(event.test.started, runLoginFunctionForTest)
402 })
403 }
404
405 // adding this to DI container
406 const support = {}

Callers 1

loginFunctionFunction · 0.85

Calls 3

fileExistsFunction · 0.90
debugMethod · 0.80
toStringMethod · 0.45

Tested by

no test coverage detected