fix: Throw error when custom config file does not return expected value (#22927)

* fix: Throw error when custom config file does not return expected value

* Improve test to avoid false positive

* Code review changes

* remove old code

* remove unused yarn.lock

Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com>
This commit is contained in:
rockindahizzy
2022-07-27 19:19:39 -05:00
committed by GitHub
parent 6adba462ea
commit 72078ef3d2
5 changed files with 56 additions and 1 deletions

View File

@@ -233,7 +233,7 @@ export class ProjectLifecycleManager {
const devServerOptions = await this.ctx._apis.projectApi.getDevServer().start({ specs: this.ctx.project.specs, config: finalConfig })
if (!devServerOptions?.port) {
this.ctx.onError(getError('CONFIG_FILE_DEV_SERVER_INVALID_RETURN', devServerOptions))
throw getError('CONFIG_FILE_DEV_SERVER_INVALID_RETURN', devServerOptions)
}
finalConfig.baseUrl = `http://localhost:${devServerOptions?.port}`

View File

@@ -294,4 +294,38 @@ describe('setupNodeEvents', () => {
cy.get('h1').should('contain', 'Choose a Browser')
cy.get('[data-cy="alert"]').should('contain', 'Warning: Cannot Connect Base Url Warning')
})
it('handles a devServer function returning wrong structure', () => {
cy.scaffoldProject('dev-server-invalid')
// sets the current project to enable writeFileInProject
cy.openProject('dev-server-invalid')
cy.visitLaunchpad()
cy.get('[data-cy-testingtype=component]').click()
cy.get('body')
.should('contain.text', cy.i18n.launchpadErrors.generic.configErrorTitle)
.and('contain.text', 'The returned value of the devServer function is not valid.')
cy.get('[data-cy="collapsible-header"]')
.should('have.attr', 'aria-expanded', 'true')
.contains(cy.i18n.launchpadErrors.generic.stackTraceLabel)
cy.log('Fix error and validate it reloads configuration')
cy.withCtx(async (ctx) => {
await ctx.actions.file.writeFileInProject('cypress.config.js',
`module.exports = {
devServer: () => ({ port: '3000' })
}`)
})
cy.findByRole('button', { name: 'Try again' }).click()
cy.get('body')
.should('not.contain.text', cy.i18n.launchpadErrors.generic.configErrorTitle)
.should('contain.text', 'Welcome to Cypress!')
})
})

View File

@@ -0,0 +1,8 @@
module.exports = {
retries: null,
component: {
supportFile: false,
devServer (cypressConfig) {},
indexHtmlFile: 'cypress/component/support/component-index.html',
},
}

View File

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Components App</title>
</head>
<body>
<div data-cy-root></div>
</body>
</html>

View File

@@ -0,0 +1 @@
export default {}