Skip to content

Commit 1e61923

Browse files
fix: restart Cypress server and browser on baseUrl change (#22154)
* fix: restart server on baseUrl change * Rework how baseUrl works * Refactor how we determine if we should ping the base url * Fix test * Update packages/launchpad/cypress/e2e/choose-a-browser.cy.ts * Update packages/launchpad/cypress/e2e/choose-a-browser.cy.ts Co-authored-by: Matt Henkes <[email protected]>
1 parent 203758f commit 1e61923

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

packages/app/cypress/e2e/cypress-in-cypress.cy.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,26 @@ describe('Cypress in Cypress', { viewportWidth: 1500, defaultCommandTimeout: 100
305305
expect(ctx.actions.project.initializeActiveProject).to.be.called
306306
})
307307
})
308+
309+
it('restarts server if baseUrl is updated in the config file', () => {
310+
startAtSpecsPage('e2e')
311+
cy.get('[data-cy="spec-item"]')
312+
313+
cy.withCtx((ctx, o) => {
314+
ctx.coreData.app.browserStatus = 'open'
315+
o.sinon.stub(ctx.actions.project, 'initializeActiveProject')
316+
317+
let config = ctx.actions.file.readFileInProject('cypress.config.js')
318+
319+
config = config.replace(` e2e: {`, ` e2e: {\n baseUrl: 'https://example.cypress.io',\n`)
320+
ctx.actions.file.writeFileInProject('cypress.config.js', config)
321+
})
322+
323+
cy.get('[data-cy="loading-spinner"]').should('be.visible')
324+
cy.contains('[role="alert"]', 'Loading')
325+
326+
cy.withRetryableCtx((ctx) => {
327+
expect(ctx.actions.project.initializeActiveProject).to.be.called
328+
})
329+
})
308330
})

packages/config/src/browser.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ export const validateNeedToRestartOnChange = (cachedConfig: any, updatedConfig:
188188
const restartOnChange = {
189189
browser: false,
190190
server: false,
191-
pingBaseUrl: false,
192191
}
193192

194193
if (!cachedConfig || !updatedConfig) {

packages/config/src/options.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ interface ResolvedConfigOption {
4040
*/
4141
canUpdateDuringTestTime?: boolean
4242
specificTestingType?: TestingType
43-
requireRestartOnChange?: 'server' | 'browser' | 'pingBaseUrl'
43+
requireRestartOnChange?: 'server' | 'browser'
4444
}
4545

4646
interface RuntimeConfigOption {
@@ -52,7 +52,7 @@ interface RuntimeConfigOption {
5252
* Can be mutated with Cypress.config() or test-specific configuration overrides
5353
*/
5454
canUpdateDuringTestTime?: boolean
55-
requireRestartOnChange?: 'server' | 'browser' | 'pingBaseUrl'
55+
requireRestartOnChange?: 'server' | 'browser'
5656
}
5757

5858
export interface BreakingOption {
@@ -135,7 +135,7 @@ const resolvedOptions: Array<ResolvedConfigOption> = [
135135
defaultValue: null,
136136
validation: validate.isFullyQualifiedUrl,
137137
canUpdateDuringTestTime: true,
138-
requireRestartOnChange: 'pingBaseUrl',
138+
requireRestartOnChange: 'server',
139139
}, {
140140
name: 'blockHosts',
141141
defaultValue: null,

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,6 @@ export class ProjectLifecycleManager {
236236
})
237237
}
238238

239-
const restartOnChange = validateNeedToRestartOnChange(this._cachedFullConfig, finalConfig)
240-
241239
if (this._currentTestingType === 'component') {
242240
const devServerOptions = await this.ctx._apis.projectApi.getDevServer().start({ specs: this.ctx.project.specs, config: finalConfig })
243241

@@ -246,14 +244,12 @@ export class ProjectLifecycleManager {
246244
}
247245

248246
finalConfig.baseUrl = `http://localhost:${devServerOptions?.port}`
249-
250-
// Devserver can pick a random port, this solve the edge case where closing
251-
// and spawning the devserver can result in a different baseUrl
252-
if (this._cachedFullConfig && this._cachedFullConfig.baseUrl !== finalConfig.baseUrl) {
253-
restartOnChange.server = true
254-
}
255247
}
256248

249+
const pingBaseUrl = this._cachedFullConfig && this._cachedFullConfig.baseUrl !== finalConfig.baseUrl
250+
251+
const restartOnChange = validateNeedToRestartOnChange(this._cachedFullConfig, finalConfig)
252+
257253
this._cachedFullConfig = finalConfig
258254

259255
// This happens automatically with openProjectCreate in run mode
@@ -271,7 +267,7 @@ export class ProjectLifecycleManager {
271267
await this.ctx.actions.browser.relaunchBrowser()
272268
}
273269

274-
if (restartOnChange.pingBaseUrl) {
270+
if (pingBaseUrl) {
275271
this.ctx.actions.project.pingBaseUrl().catch(this.onLoadError)
276272
}
277273
}

packages/launchpad/cypress/e2e/choose-a-browser.cy.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ describe('Choose a Browser Page', () => {
190190
cy.get('h1').should('contain', 'Choose a Browser')
191191

192192
cy.withCtx((ctx, o) => {
193-
o.sinon.spy(ctx.actions.project, 'launchProject')
193+
o.sinon.stub(ctx.actions.project, 'launchProject')
194194
})
195195

196196
cy.intercept('mutation-OpenBrowser_LaunchProject', cy.stub().as('launchProject'))
@@ -233,7 +233,7 @@ describe('Choose a Browser Page', () => {
233233
cy.intercept('mutation-OpenBrowser_FocusActiveBrowserWindow').as('focusBrowser')
234234

235235
cy.withCtx((ctx, o) => {
236-
o.sinon.spy(ctx.actions.browser, 'focusActiveBrowserWindow')
236+
o.sinon.stub(ctx.actions.browser, 'focusActiveBrowserWindow')
237237
})
238238

239239
cy.get('@focusButton').click()
@@ -276,6 +276,10 @@ describe('Choose a Browser Page', () => {
276276

277277
cy.findByRole('radio', { name: 'Chrome v1', checked: true }).as('chromeItem')
278278

279+
cy.withCtx((ctx, o) => {
280+
o.sinon.stub(ctx.actions.project, 'launchProject')
281+
})
282+
279283
cy.contains('button', 'Start E2E Testing in Chrome').should('be.visible').click()
280284

281285
cy.withCtx((ctx) => {

0 commit comments

Comments
 (0)