Skip to content

Commit 4658c8c

Browse files
feat: (cy.prompt) introduce the concept of log collapse state being open/closed by default (#32262)
* feat: (cy.prompt) introduce the concept of logs' collapse state being open/closed by default * update tests * rename * PR comments
1 parent dcc8e24 commit 4658c8c

File tree

10 files changed

+217
-5
lines changed

10 files changed

+217
-5
lines changed

packages/driver/src/cy/commands/sessions/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export default function (Commands, Cypress, cy) {
151151
displayName: statusMap.stepName(step),
152152
message: '',
153153
type: 'system',
154+
defaultCollapsedState: 'closed',
154155
}, (setupLogGroup) => {
155156
return cy.then({ timeout: INTERNAL_COMMAND_TIMEOUT }, async () => {
156157
// Catch when a cypress command fails in the setup function to correctly update log status
@@ -213,6 +214,7 @@ export default function (Commands, Cypress, cy) {
213214
displayName: 'Restore saved session',
214215
message: '',
215216
type: 'system',
217+
defaultCollapsedState: 'closed',
216218
consoleProps: () => {
217219
return {
218220
Step: 'Restore saved session',
@@ -238,6 +240,7 @@ export default function (Commands, Cypress, cy) {
238240
displayName: 'Validate session',
239241
message: '',
240242
type: 'system',
243+
defaultCollapsedState: 'closed',
241244
consoleProps: () => {
242245
return {
243246
Step: 'Validate Session',
@@ -495,6 +498,7 @@ export default function (Commands, Cypress, cy) {
495498
let _log
496499
const groupDetails = {
497500
message: `${session.id.length > 50 ? `${session.id.substring(0, 47)}...` : session.id}`,
501+
defaultCollapsedState: 'closed' as const,
498502
}
499503

500504
return logGroup(Cypress, groupDetails, (log) => {

packages/driver/src/cy/logGroup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default (Cypress, userOptions: Cypress.LogGroup.Config, fn: Cypress.LogGr
1010
instrument: 'command',
1111
groupStart: true,
1212
hidden: userOptions.log === false,
13+
defaultCollapsedState: userOptions.defaultCollapsedState || 'open',
1314
}
1415

1516
const log = Cypress.log(options)

packages/driver/src/cypress/log.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type { StateFunc } from './state'
1414
const groupsOrTableRe = /^(groups|table)$/
1515
const parentOrChildRe = /parent|child|system/
1616
const SNAPSHOT_PROPS = 'id snapshots $el url coords highlightAttr scrollBy viewportWidth viewportHeight'.split(' ')
17-
const DISPLAY_PROPS = 'id alias aliasType callCount displayName end err event functionName groupLevel hookId instrument isStubbed group hidden message method name numElements numResponses referencesAlias renderProps sessionInfo state testId timeout type url visible wallClockStartedAt testCurrentRetry'.split(' ')
17+
const DISPLAY_PROPS = 'id alias aliasType callCount defaultCollapsedState displayName end err event functionName groupLevel hookId instrument isStubbed group hidden message method name numElements numResponses referencesAlias renderProps sessionInfo state testId timeout type url visible wallClockStartedAt testCurrentRetry'.split(' ')
1818
const PROTOCOL_PROPS = DISPLAY_PROPS.concat(['snapshots', 'createdAtTimestamp', 'updatedAtTimestamp', 'scrollBy', 'coords', 'highlightAttr'])
1919
const BLACKLIST_PROPS = 'snapshots'.split(' ')
2020

@@ -196,6 +196,7 @@ const defaults = function (state: StateFunc, config, obj) {
196196
id: `log-${window.location.origin}-${counter}`,
197197
chainerId,
198198
state: 'pending',
199+
defaultCollapsedState: 'open',
199200
hidden: false,
200201
instrument: 'command',
201202
url: state('url'),

packages/driver/types/cy/logGroup.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ declare namespace Cypress {
2626
// parent - log generated by Command
2727
// child - log generated by Chained Command
2828
type?: Cypress.InternalLogConfig['type']
29+
// the default collapsed state of the group (i.e. if the group is open or closed by default)
30+
defaultCollapsedState?: DefaultLogCollapsedState
2931
}
3032
}
3133
}

packages/driver/types/cypress/log.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ declare namespace Cypress {
5656
message?: string
5757
}
5858

59+
type DefaultLogCollapsedState = 'closed' | 'open'
60+
5961
interface InternalLogConfig {
6062
alias?: string
6163
aliasType?: 'agent' | 'route' | 'primitive' | 'dom' | undefined
@@ -75,6 +77,8 @@ declare namespace Cypress {
7577
y: number
7678
}
7779
count?: number
80+
// the default collapsed state of the log (i.e. if the log is open or closed by default)
81+
defaultCollapsedState?: DefaultLogCollapsedState
7882
// the name override for display purposes only
7983
displayName?: string
8084
// the JQuery element for the command. This will highlight the command

packages/reporter/cypress/e2e/commands.cy.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ describe('commands', { viewportHeight: 1000 }, () => {
713713
it('closed when nested logs that pass', () => {
714714
const nestedGroupId = addCommand(runner, {
715715
name: 'session',
716+
defaultCollapsedState: 'closed',
716717
state: 'passed',
717718
type: 'child',
718719
})
@@ -727,6 +728,7 @@ describe('commands', { viewportHeight: 1000 }, () => {
727728

728729
const nestedSessionGroupId = addCommand(runner, {
729730
name: 'session',
731+
defaultCollapsedState: 'closed',
730732
displayName: 'validate',
731733
type: 'child',
732734
groupLevel: 2,
@@ -769,6 +771,7 @@ describe('commands', { viewportHeight: 1000 }, () => {
769771
it('closed when nested logs has failures but last log is successful', () => {
770772
const nestedGroupId = addCommand(runner, {
771773
name: 'session',
774+
defaultCollapsedState: 'closed',
772775
state: 'passed',
773776
type: 'child',
774777
})
@@ -783,6 +786,7 @@ describe('commands', { viewportHeight: 1000 }, () => {
783786

784787
const nestedSessionGroupId = addCommand(runner, {
785788
name: 'session',
789+
defaultCollapsedState: 'closed',
786790
displayName: 'validate',
787791
type: 'child',
788792
state: 'failed',
@@ -829,6 +833,7 @@ describe('commands', { viewportHeight: 1000 }, () => {
829833
it('open when last log has failed', () => {
830834
const nestedGroupId = addCommand(runner, {
831835
name: 'session',
836+
defaultCollapsedState: 'closed',
832837
state: 'passed',
833838
type: 'child',
834839
})
@@ -843,6 +848,7 @@ describe('commands', { viewportHeight: 1000 }, () => {
843848

844849
const nestedSessionGroupId = addCommand(runner, {
845850
name: 'session',
851+
defaultCollapsedState: 'closed',
846852
displayName: 'validate',
847853
state: 'failed',
848854
type: 'system',
@@ -997,6 +1003,7 @@ describe('commands', { viewportHeight: 1000 }, () => {
9971003
cy.fixture('command_error').then((_commandErr) => {
9981004
const groupId = addCommand(runner, {
9991005
name: 'session',
1006+
defaultCollapsedState: 'closed',
10001007
message: 'mock restore',
10011008
state: 'passed',
10021009
type: 'system',
@@ -1031,6 +1038,7 @@ describe('commands', { viewportHeight: 1000 }, () => {
10311038
cy.fixture('command_error').then((_commandErr) => {
10321039
const groupId = addCommand(runner, {
10331040
name: 'session',
1041+
defaultCollapsedState: 'closed',
10341042
message: 'mock restore',
10351043
state: 'passed',
10361044
type: 'system',

packages/reporter/cypress/support/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export const addCommand = (runner: EventEmitter, log: Partial<CommandModel>) =>
4646
type: 'parent',
4747
url: 'http://example.com',
4848
hasConsoleProps: true,
49+
defaultCollapsedState: 'open',
4950
}
5051

5152
const commandLog = Object.assign(defaultLog, log)

packages/reporter/src/commands/command-model.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ export default class Command extends Instrument {
9191
return this._isOpen || (this._isOpen === null
9292
&& (
9393
this.err?.isRecovered ||
94-
(this.name === 'session' && this.state === 'failed') ||
94+
(this.defaultCollapsedState === 'closed' && this.state === 'failed') ||
9595
// command has nested commands
96-
(this.name !== 'session' && this.hasChildren && !this.event && this.type !== 'system') ||
96+
(this.defaultCollapsedState !== 'closed' && this.hasChildren && !this.event && this.type !== 'system') ||
9797
// command has nested commands with children
98-
(this.name !== 'session' && _.some(this.children, (v) => v.hasChildren)) ||
98+
(this.defaultCollapsedState !== 'closed' && _.some(this.children, (v) => v.hasChildren)) ||
9999
// last nested command is open
100-
(this.name !== 'session' && _.last(this.children)?.isOpen) ||
100+
(this.defaultCollapsedState !== 'closed' && _.last(this.children)?.isOpen) ||
101101
// show slow command when test is running
102102
(_.some(this.children, (v) => v.isLongRunning) && _.last(this.children)?.state === 'pending') ||
103103
// at last nested command failed

packages/reporter/src/instruments/instrument-model.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export interface AliasObject {
99

1010
export type Alias = string | Array<string> | null | AliasObject | Array<AliasObject>
1111

12+
export type DefaultCollapsedState = 'closed' | 'open'
13+
1214
export interface InstrumentProps {
1315
id: number
1416
alias?: Alias
@@ -23,6 +25,7 @@ export interface InstrumentProps {
2325
referencesAlias?: Alias
2426
instrument?: Instrument
2527
testId: string
28+
defaultCollapsedState?: DefaultCollapsedState
2629
}
2730

2831
export default class Log {
@@ -36,6 +39,7 @@ export default class Log {
3639
state: string
3740
referencesAlias?: Alias
3841
testId: string
42+
defaultCollapsedState: DefaultCollapsedState
3943

4044
constructor (props: InstrumentProps) {
4145
makeObservable(this, {
@@ -48,6 +52,7 @@ export default class Log {
4852
type: observable,
4953
state: observable,
5054
referencesAlias: observable.ref,
55+
defaultCollapsedState: observable,
5156
})
5257

5358
this.id = props.id
@@ -60,6 +65,7 @@ export default class Log {
6065
this.state = props.state
6166
this.referencesAlias = props.referencesAlias
6267
this.testId = props.testId
68+
this.defaultCollapsedState = props.defaultCollapsedState || 'open'
6369
}
6470

6571
update (props: InstrumentProps) {
@@ -71,5 +77,6 @@ export default class Log {
7177
this.type = props.type
7278
this.state = props.state
7379
this.referencesAlias = props.referencesAlias
80+
this.defaultCollapsedState = props.defaultCollapsedState || 'open'
7481
}
7582
}

0 commit comments

Comments
 (0)