Skip to content

Commit ce95894

Browse files
committed
feedback
1 parent c9ffbd5 commit ce95894

File tree

1 file changed

+62
-56
lines changed
  • packages/driver/src/cypress

1 file changed

+62
-56
lines changed

packages/driver/src/cypress/log.ts

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -223,16 +223,16 @@ class Log {
223223
cy: any
224224
state: any
225225
config: any
226-
logManager: LogManager
226+
fireChangeEvent: ((log) => (void | undefined))
227227
obj: any
228228

229229
private attributes: Record<string, any> = {}
230230

231-
constructor (cy, state, config, logManager, obj) {
231+
constructor (cy, state, config, fireChangeEvent, obj) {
232232
this.cy = cy
233233
this.state = state
234234
this.config = config
235-
this.logManager = logManager
235+
this.fireChangeEvent = fireChangeEvent
236236
this.obj = defaults(state, config, obj)
237237

238238
extendEvents(this)
@@ -321,7 +321,7 @@ class Log {
321321
this.setElAttrs()
322322
}
323323

324-
this.logManager.fireChangeEvent(this)
324+
this.fireChangeEvent(this)
325325

326326
return this
327327
}
@@ -513,6 +513,10 @@ class Log {
513513
class LogManager {
514514
logs: Record<string, any> = {}
515515

516+
constructor () {
517+
this.fireChangeEvent = this.fireChangeEvent.bind(this)
518+
}
519+
516520
trigger (log, event) {
517521
// bail if we never fired our initial log event
518522
if (!log._hasInitiallyLogged) {
@@ -560,76 +564,78 @@ class LogManager {
560564

561565
return debounceFn()
562566
}
563-
}
564567

565-
export function create (Cypress, cy, state, config) {
566-
counter = 0
567-
const logManager = new LogManager()
568-
569-
const logFn = function (options: any = {}) {
570-
if (!_.isObject(options)) {
571-
$errUtils.throwErrByPath('log.invalid_argument', { args: { arg: options } })
572-
}
568+
createLogFn (cy, state, config) {
569+
return (options: any = {}) => {
570+
if (!_.isObject(options)) {
571+
$errUtils.throwErrByPath('log.invalid_argument', { args: { arg: options } })
572+
}
573573

574-
const log = new Log(cy, state, config, logManager, options)
574+
const log = new Log(cy, state, config, this.fireChangeEvent, options)
575575

576-
log.set(options)
576+
log.set(options)
577577

578-
// if snapshot was passed
579-
// in, go ahead and snapshot
580-
if (log.get('snapshot')) {
581-
log.snapshot()
582-
}
578+
// if snapshot was passed
579+
// in, go ahead and snapshot
580+
if (log.get('snapshot')) {
581+
log.snapshot()
582+
}
583583

584-
// if end was passed in
585-
// go ahead and end
586-
if (log.get('end')) {
587-
log.end()
588-
}
584+
// if end was passed in
585+
// go ahead and end
586+
if (log.get('end')) {
587+
log.end()
588+
}
589589

590-
if (log.get('error')) {
591-
log.error(log.get('error'))
592-
}
590+
if (log.get('error')) {
591+
log.error(log.get('error'))
592+
}
593593

594-
log.wrapConsoleProps()
594+
log.wrapConsoleProps()
595595

596-
const onBeforeLog = state('onBeforeLog')
596+
const onBeforeLog = state('onBeforeLog')
597597

598-
// dont trigger log if this function
599-
// explicitly returns false
600-
if (_.isFunction(onBeforeLog)) {
601-
if (onBeforeLog.call(cy, log) === false) {
602-
return
598+
// dont trigger log if this function
599+
// explicitly returns false
600+
if (_.isFunction(onBeforeLog)) {
601+
if (onBeforeLog.call(cy, log) === false) {
602+
return
603+
}
603604
}
604-
}
605605

606-
// set the log on the command
607-
const current = state('current')
606+
// set the log on the command
607+
const current = state('current')
608608

609-
if (current) {
610-
current.log(log)
611-
}
609+
if (current) {
610+
current.log(log)
611+
}
612612

613-
logManager.addToLogs(log)
613+
this.addToLogs(log)
614614

615-
if (options.sessionInfo) {
616-
Cypress.emit('session:add', log.toJSON())
617-
}
615+
if (options.sessionInfo) {
616+
Cypress.emit('session:add', log.toJSON())
617+
}
618618

619-
if (options.emitOnly) {
620-
return
621-
}
619+
if (options.emitOnly) {
620+
return
621+
}
622622

623-
logManager.triggerLog(log)
623+
this.triggerLog(log)
624624

625-
// if not current state then the log is being run
626-
// with no command reference, so just end the log
627-
if (!current) {
628-
log.end()
629-
}
625+
// if not current state then the log is being run
626+
// with no command reference, so just end the log
627+
if (!current) {
628+
log.end()
629+
}
630630

631-
return log
631+
return log
632+
}
632633
}
634+
}
635+
636+
export function create (Cypress, cy, state, config) {
637+
counter = 0
638+
const logManager = new LogManager()
633639

634-
return logFn
640+
return logManager.createLogFn(cy, state, config)
635641
}

0 commit comments

Comments
 (0)