Skip to content

Commit ef9df8d

Browse files
authored
chore: simplify error/warning management in data context (#23034)
* chore: remove unused prop * remove duplicate error and warning abstraction * remove incorrect warning clobber * do not clear warning on set project * remove concept of currentProjectData * collapse errors and warnings into diagnostics key * remove TODO [ci skip] * simplify code * remove unused code
1 parent 05479bf commit ef9df8d

File tree

9 files changed

+26
-130
lines changed

9 files changed

+26
-130
lines changed

packages/config/src/options.ts

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ interface ResolvedConfigOption {
3939
* Can be mutated with Cypress.config() or test-specific configuration overrides
4040
*/
4141
canUpdateDuringTestTime?: boolean
42-
specificTestingType?: TestingType
4342
requireRestartOnChange?: 'server' | 'browser'
4443
}
4544

packages/data-context/src/DataContext.ts

+5-28
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,11 @@ export class DataContext {
157157
}
158158

159159
get baseError () {
160-
return this.coreData.currentProjectData?.testingTypeData?.activeAppData?.error
161-
?? this.coreData.currentProjectData?.testingTypeData?.error
162-
?? this.coreData.currentProjectData?.error
163-
?? this.coreData.baseError
164-
?? null
160+
return this.coreData.diagnostics.error
165161
}
166162

167163
get warnings () {
168-
return [
169-
...this.coreData.currentProjectData?.testingTypeData?.activeAppData?.warnings ?? [],
170-
...this.coreData.currentProjectData?.testingTypeData?.warnings ?? [],
171-
...this.coreData.currentProjectData?.warnings ?? [],
172-
...this.coreData.warnings ?? [],
173-
]
164+
return this.coreData.diagnostics.warnings
174165
}
175166

176167
@cached
@@ -386,14 +377,8 @@ export class DataContext {
386377
}
387378

388379
this.update((d) => {
389-
if (d.currentProjectData?.testingTypeData?.activeAppData) {
390-
d.currentProjectData.testingTypeData.activeAppData.error = err
391-
} else if (d.currentProjectData?.testingTypeData) {
392-
d.currentProjectData.testingTypeData.error = err
393-
} else if (d.currentProjectData) {
394-
d.currentProjectData.error = err
395-
} else {
396-
d.baseError = err
380+
if (d.diagnostics) {
381+
d.diagnostics.error = err
397382
}
398383
})
399384

@@ -413,15 +398,7 @@ export class DataContext {
413398
}
414399

415400
this.update((d) => {
416-
if (d.currentProjectData?.testingTypeData?.activeAppData) {
417-
d.currentProjectData.testingTypeData.activeAppData.warnings.push(warning)
418-
} else if (d.currentProjectData?.testingTypeData) {
419-
d.currentProjectData.testingTypeData.warnings.push(warning)
420-
} else if (d.currentProjectData) {
421-
d.currentProjectData.warnings.push(warning)
422-
} else {
423-
d.warnings.push(warning)
424-
}
401+
d.diagnostics.warnings.push(warning)
425402
})
426403

427404
this.emitter.errorWarningChange()

packages/data-context/src/actions/BrowserActions.ts

-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ export class BrowserActions {
3030

3131
this.ctx.update((d) => {
3232
d.activeBrowser = browser
33-
if (d.currentProjectData?.testingTypeData) {
34-
d.currentProjectData.testingTypeData.activeAppData = { error: null, warnings: [] }
35-
}
3633
})
3734

3835
this.ctx._apis.projectApi.insertProjectPreferencesToCache(this.ctx.lifecycleManager.projectTitle, {

packages/data-context/src/actions/ErrorActions.ts

+5-41
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,8 @@ export class ErrorActions {
99
*/
1010
clearError (id: string) {
1111
this.ctx.update((d) => {
12-
if (d.currentProjectData?.testingTypeData?.activeAppData?.error?.id === id) {
13-
d.currentProjectData.testingTypeData.activeAppData.error = null
14-
}
15-
16-
if (d.currentProjectData?.testingTypeData?.error?.id === id) {
17-
d.currentProjectData.testingTypeData.error = null
18-
}
19-
20-
if (d.currentProjectData?.error?.id === id) {
21-
d.currentProjectData.error = null
22-
}
23-
24-
if (d.baseError?.id === id) {
25-
d.baseError = null
12+
if (d.diagnostics.error?.id === id) {
13+
d.diagnostics.error = null
2614
}
2715
})
2816
}
@@ -33,34 +21,10 @@ export class ErrorActions {
3321
*/
3422
clearWarning (id: string) {
3523
this.ctx.update((d) => {
36-
const warningsIndex = d.warnings.findIndex((v) => v.id === id)
37-
38-
if (warningsIndex != null && warningsIndex !== -1) {
39-
d.warnings.splice(warningsIndex, 1)
40-
41-
return
42-
}
43-
44-
const projectWarningsIndex = d.currentProjectData?.warnings.findIndex((v) => v.id === id)
45-
46-
if (projectWarningsIndex != null && projectWarningsIndex !== -1) {
47-
d.currentProjectData?.warnings.splice(projectWarningsIndex, 1)
48-
49-
return
50-
}
51-
52-
const testingTypeWarningsIndex = d.currentProjectData?.testingTypeData?.warnings.findIndex((v) => v.id === id)
53-
54-
if (testingTypeWarningsIndex != null && testingTypeWarningsIndex !== -1) {
55-
d.currentProjectData?.testingTypeData?.warnings.splice(testingTypeWarningsIndex, 1)
56-
57-
return
58-
}
59-
60-
const appWarningsIndex = d.currentProjectData?.testingTypeData?.activeAppData?.warnings.findIndex((v) => v.id === id)
24+
const warningsIndex = d.diagnostics.warnings.findIndex((v) => v.id === id)
6125

62-
if (appWarningsIndex != null && appWarningsIndex !== -1) {
63-
d.currentProjectData?.testingTypeData?.activeAppData?.warnings.splice(appWarningsIndex, 1)
26+
if (warningsIndex !== -1) {
27+
d.diagnostics.warnings.splice(warningsIndex, 1)
6428
}
6529
})
6630
}

packages/data-context/src/actions/ProjectActions.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,13 @@ export class ProjectActions {
8383

8484
async clearCurrentProject () {
8585
this.ctx.update((d) => {
86-
d.baseError = null
8786
d.activeBrowser = null
8887
d.currentProject = null
89-
d.currentProjectData = null
88+
d.diagnostics = {
89+
error: null,
90+
warnings: [],
91+
}
92+
9093
d.currentTestingType = null
9194
d.forceReconfigureProject = null
9295
d.scaffoldedFiles = null

packages/data-context/src/data/ProjectLifecycleManager.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { EventRegistrar } from './EventRegistrar'
2323
import { getServerPluginHandlers, resetPluginHandlers } from '../util/pluginHandlers'
2424
import { detectLanguage } from '@packages/scaffold-config'
2525
import { validateNeedToRestartOnChange } from '@packages/config'
26-
import { makeTestingTypeData } from './coreDataShape'
2726

2827
export interface SetupFullConfigOptions {
2928
projectName: string
@@ -408,7 +407,7 @@ export class ProjectLifecycleManager {
408407
})
409408
}
410409

411-
s.currentProjectData = { error: null, warnings: [], testingTypeData: null }
410+
s.diagnostics = { error: null, warnings: [] }
412411
s.packageManager = packageManagerUsed
413412
})
414413

@@ -495,8 +494,11 @@ export class ProjectLifecycleManager {
495494
d.currentTestingType = testingType
496495
d.wizard.chosenBundler = null
497496
d.wizard.chosenFramework = null
498-
if (d.currentProjectData) {
499-
d.currentProjectData.testingTypeData = makeTestingTypeData(testingType)
497+
if (testingType) {
498+
d.diagnostics = {
499+
error: null,
500+
warnings: [],
501+
}
500502
}
501503
})
502504

@@ -516,9 +518,6 @@ export class ProjectLifecycleManager {
516518
d.currentTestingType = testingType
517519
d.wizard.chosenBundler = null
518520
d.wizard.chosenFramework = null
519-
if (d.currentProjectData) {
520-
d.currentProjectData.testingTypeData = makeTestingTypeData(testingType)
521-
}
522521
})
523522

524523
if (this._currentTestingType === testingType) {

packages/data-context/src/data/coreDataShape.ts

+3-46
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,11 @@ export interface ForceReconfigureProjectDataShape {
107107
component?: boolean | null
108108
}
109109

110-
export interface ActiveAppData {
110+
interface Diagnostics {
111111
error: ErrorWrapperSource | null
112112
warnings: ErrorWrapperSource[]
113113
}
114114

115-
export interface CurrentTestingTypeData {
116-
error: ErrorWrapperSource | null
117-
warnings: ErrorWrapperSource[]
118-
activeAppData: ActiveAppData | null
119-
}
120-
121-
export interface CurrentProjectData {
122-
error: ErrorWrapperSource | null
123-
warnings: ErrorWrapperSource[]
124-
testingTypeData: CurrentTestingTypeData | null
125-
}
126-
127115
export interface CoreDataShape {
128116
cliBrowser: string | null
129117
cliTestingType: string | null
@@ -139,25 +127,20 @@ export interface CoreDataShape {
139127
gqlSocketServer?: Maybe<SocketIONamespace>
140128
}
141129
hasInitializedMode: 'run' | 'open' | null
142-
baseError: ErrorWrapperSource | null
143130
dashboardGraphQLError: ErrorWrapperSource | null
144131
dev: DevStateShape
145132
localSettings: LocalSettingsDataShape
146133
app: AppDataShape
147134
currentProject: string | null
148135
currentProjectGitInfo: GitDataSource | null
149136
currentTestingType: TestingType | null
150-
151-
// TODO: Move everything under this container, to make it simpler to reset the data when switching
152-
currentProjectData: CurrentProjectData | null
153-
137+
diagnostics: Diagnostics
154138
wizard: WizardDataShape
155139
migration: MigrationDataShape
156140
user: AuthenticatedUserShape | null
157141
electron: ElectronShape
158142
authState: AuthStateShape
159143
scaffoldedFiles: NexusGenObjects['ScaffoldedFile'][] | null
160-
warnings: ErrorWrapperSource[]
161144
packageManager: typeof PACKAGE_MANAGERS[number]
162145
forceReconfigureProject: ForceReconfigureProjectDataShape | null
163146
versionData: {
@@ -176,7 +159,6 @@ export function makeCoreData (modeOptions: Partial<AllModeOptions> = {}): CoreDa
176159
cliTestingType: modeOptions.testingType ?? null,
177160
machineBrowsers: null,
178161
hasInitializedMode: null,
179-
baseError: null,
180162
dashboardGraphQLError: null,
181163
dev: {
182164
refreshState: null,
@@ -198,7 +180,7 @@ export function makeCoreData (modeOptions: Partial<AllModeOptions> = {}): CoreDa
198180
browserOpened: false,
199181
},
200182
currentProject: modeOptions.projectRoot ?? null,
201-
currentProjectData: makeCurrentProjectData(modeOptions.projectRoot, modeOptions.testingType),
183+
diagnostics: { error: null, warnings: [] },
202184
currentProjectGitInfo: null,
203185
currentTestingType: modeOptions.testingType ?? null,
204186
wizard: {
@@ -225,7 +207,6 @@ export function makeCoreData (modeOptions: Partial<AllModeOptions> = {}): CoreDa
225207
shouldAddCustomE2ESpecPattern: false,
226208
},
227209
},
228-
warnings: [],
229210
activeBrowser: null,
230211
user: null,
231212
electron: {
@@ -238,27 +219,3 @@ export function makeCoreData (modeOptions: Partial<AllModeOptions> = {}): CoreDa
238219
versionData: null,
239220
}
240221
}
241-
242-
export function makeCurrentProjectData (projectRoot: Maybe<string>, testingType: Maybe<TestingType>): CurrentProjectData | null {
243-
if (projectRoot) {
244-
return {
245-
error: null,
246-
warnings: [],
247-
testingTypeData: makeTestingTypeData(testingType),
248-
}
249-
}
250-
251-
return null
252-
}
253-
254-
export function makeTestingTypeData (testingType: Maybe<TestingType>): CurrentTestingTypeData | null {
255-
if (testingType) {
256-
return {
257-
error: null,
258-
warnings: [],
259-
activeAppData: null,
260-
}
261-
}
262-
263-
return null
264-
}

packages/data-context/src/sources/BrowserDataSource.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class BrowserDataSource {
4848
}).catch((e) => {
4949
this.ctx.update((coreData) => {
5050
coreData.machineBrowsers = null
51-
coreData.baseError = e
51+
coreData.diagnostics.error = e
5252
})
5353

5454
throw e

packages/graphql/src/plugins/nexusMutationErrorPlugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const mutationErrorPlugin = plugin({
1515
return (source, args, ctx: DataContext, info, next) => {
1616
return plugin.completeValue(next(source, args, ctx, info), (v) => v, (err) => {
1717
ctx.update((d) => {
18-
d.baseError = {
18+
d.diagnostics.error = {
1919
id: _.uniqueId('Error'),
2020
cypressError: err.isCypressErr
2121
? err

0 commit comments

Comments
 (0)