Skip to content

Commit 8f32960

Browse files
fix: scaffold correct config file (#19776)
* fix: scaffold correct config file * update setConfigFilePath * Update test * Update with code review * remove default supportFile * Update supportFile default value * Add test * Fix identation * Update with feedback * fix english pluralization Co-authored-by: Tim Griesser <tgriesser10@gmail.com>
1 parent d307abe commit 8f32960

File tree

26 files changed

+106
-51
lines changed

26 files changed

+106
-51
lines changed

cli/schema/cypress.schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
"string",
148148
"boolean"
149149
],
150-
"default": "cypress/support/e2e.js",
150+
"default": "cypress/support/e2e.{js,jsx,ts,tsx}",
151151
"description": "Path to file to load before test files load. This file is compiled and bundled. (Pass false to disable)"
152152
},
153153
"videosFolder": {

npm/angular/cypress.config.ts

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export default defineConfig({
77
'fileServerFolder': 'src',
88
'projectId': 'nf7zag',
99
'component': {
10-
'supportFile': 'cypress/support/component.ts',
1110
setupNodeEvents (on, config) {
1211
return require('./cypress/plugins')(on, config)
1312
},

npm/react/examples/find-webpack/cypress.config.ts

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export default defineConfig({
44
'video': true,
55
'projectId': 'jq5xpp',
66
'component': {
7-
'supportFile': 'cypress/support/component.ts',
87
devServer (cypressConfig) {
98
const findReactScriptsWebpackConfig = require('@cypress/react/plugins/react-scripts/findReactScriptsWebpackConfig')
109
const { startDevServer } = require('@cypress/webpack-dev-server')

npm/react/examples/react-scripts-typescript/cypress.config.js

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module.exports = defineConfig({
55
'viewportWidth': 500,
66
'viewportHeight': 800,
77
'component': {
8-
'supportFile': 'cypress/support/component.ts',
98
devServer: require('@cypress/react/plugins/react-scripts'),
109
},
1110
})

packages/config/__snapshots__/index_spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ exports['src/index .getDefaultValues returns list of public config keys 1'] = {
5858
"screenshotsFolder": "cypress/screenshots",
5959
"slowTestThreshold": 10000,
6060
"scrollBehavior": "top",
61-
"supportFile": "cypress/support/e2e.js",
61+
"supportFile": "cypress/support/e2e.{js,jsx,ts,tsx}",
6262
"supportFolder": false,
6363
"taskTimeout": 60000,
6464
"trashAssetsBeforeRuns": true,

packages/config/lib/options.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ const resolvedOptions: Array<ResolvedConfigOption> = [
314314
canUpdateDuringTestTime: true,
315315
}, {
316316
name: 'supportFile',
317-
defaultValue: (options: Record<string, any> = {}) => options.testingType === 'component' ? 'cypress/support/component.js' : 'cypress/support/e2e.js',
317+
defaultValue: (options: Record<string, any> = {}) => options.testingType === 'component' ? 'cypress/support/component.{js,jsx,ts,tsx}' : 'cypress/support/e2e.{js,jsx,ts,tsx}',
318318
validation: validate.isStringOrFalse,
319319
isFolder: true,
320320
canUpdateDuringTestTime: false,

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

+10-24
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import fs from 'fs'
66
import path from 'path'
77

88
import type { DataContext } from '..'
9-
import { getDefaultSpecPatterns } from '../util/config-options'
109

1110
interface WizardGetCodeComponent {
1211
chosenLanguage: CodeLanguage
@@ -144,6 +143,7 @@ export class WizardActions {
144143
}
145144

146145
private async scaffoldConfig (configCode: string): Promise<NexusGenObjects['ScaffoldedFile']> {
146+
this.ctx.lifecycleManager.setConfigFilePath(this.ctx.coreData.wizard.chosenLanguage)
147147
if (!fs.existsSync(this.ctx.lifecycleManager.configFilePath)) {
148148
return this.scaffoldFile(
149149
this.ctx.lifecycleManager.configFilePath,
@@ -196,9 +196,7 @@ export class WizardActions {
196196
codeBlocks.push(lang === 'ts' ? `import { defineConfig } from 'cypress'` : `const { defineConfig } = require('cypress')`)
197197
codeBlocks.push('')
198198
codeBlocks.push(lang === 'ts' ? `export default defineConfig({` : `module.exports = defineConfig({`)
199-
codeBlocks.push(` ${E2E_SCAFFOLD_BODY({
200-
lang,
201-
}).replace(/\n/g, '\n ')}`)
199+
codeBlocks.push(` ${E2E_SCAFFOLD_BODY.replace(/\n/g, '\n ')}`)
202200

203201
codeBlocks.push('})\n')
204202

@@ -212,7 +210,7 @@ export class WizardActions {
212210
codeBlocks.push(chosenLanguage.type === 'ts' ? `import { defineConfig } from 'cypress'` : `const { defineConfig } = require('cypress')`)
213211
codeBlocks.push('')
214212
codeBlocks.push(chosenLanguage.type === 'ts' ? `export default defineConfig({` : `module.exports = defineConfig({`)
215-
codeBlocks.push(`// Component testing, ${chosenLanguage.name}, ${chosenFramework.name}, ${chosenBundler.name}`)
213+
codeBlocks.push(` // Component testing, ${chosenLanguage.name}, ${chosenFramework.name}, ${chosenBundler.name}`)
216214

217215
codeBlocks.push(` ${COMPONENT_SCAFFOLD_BODY({
218216
lang: chosenLanguage.type,
@@ -319,23 +317,13 @@ export class WizardActions {
319317
}
320318
}
321319

322-
interface E2eScaffoldOpts {
323-
lang: CodeLanguageEnum
324-
}
325-
326-
const E2E_SCAFFOLD_BODY = (opts: E2eScaffoldOpts) => {
327-
return dedent`
320+
const E2E_SCAFFOLD_BODY = dedent`
328321
e2e: {
329-
supportFile: 'cypress/support/e2e.${opts.lang}',
330-
specPattern: '${getDefaultSpecPatterns().e2e}',
331-
viewportHeight: 660,
332-
viewportWidth: 1000,
333322
setupNodeEvents(on, config) {
334323
// implement node event listeners here
335324
},
336325
},
337-
`
338-
}
326+
`
339327

340328
interface ComponentScaffoldOpts {
341329
lang: CodeLanguageEnum
@@ -346,13 +334,11 @@ interface ComponentScaffoldOpts {
346334

347335
const COMPONENT_SCAFFOLD_BODY = (opts: ComponentScaffoldOpts) => {
348336
return dedent`
349-
component: {
350-
supportFile: 'cypress/support/component.${opts.lang}',
351-
specPattern: '${getDefaultSpecPatterns().component}',
352-
devServer: import('${opts.requirePath}'),
353-
devServerConfig: ${opts.configOptionsString}
354-
},
355-
`
337+
component: {
338+
devServer: import('${opts.requirePath}'),
339+
devServerConfig: ${opts.configOptionsString}
340+
},
341+
`
356342
}
357343

358344
const FIXTURE_DATA = {

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -1105,20 +1105,20 @@ export class ProjectLifecycleManager {
11051105

11061106
if (fs.existsSync(configFileTs)) {
11071107
metaState.hasValidConfigFile = true
1108-
this._configFilePath = configFileTs
1108+
this.setConfigFilePath('ts')
11091109
}
11101110

11111111
if (fs.existsSync(configFileJs)) {
11121112
metaState.hasValidConfigFile = true
11131113
if (this._configFilePath) {
11141114
metaState.hasMultipleConfigPaths = true
11151115
} else {
1116-
this._configFilePath = configFileJs
1116+
this.setConfigFilePath('js')
11171117
}
11181118
}
11191119

11201120
if (!this._configFilePath) {
1121-
this._configFilePath = metaState.hasTypescript ? configFileTs : configFileJs
1121+
this.setConfigFilePath(metaState.hasTypescript ? 'ts' : 'js')
11221122
}
11231123

11241124
if (metaState.hasLegacyCypressJson && !metaState.hasValidConfigFile) {
@@ -1130,6 +1130,10 @@ export class ProjectLifecycleManager {
11301130
return metaState
11311131
}
11321132

1133+
setConfigFilePath (lang: 'ts' | 'js') {
1134+
this._configFilePath = this._pathToFile(`cypress.config.${lang}`)
1135+
}
1136+
11331137
private _pathToFile (file: string) {
11341138
return path.isAbsolute(file) ? file : path.join(this.projectRoot, file)
11351139
}

packages/frontend-shared/cypress.config.ts

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export default defineConfig({
1717
'configFile': '../../mocha-reporter-config.json',
1818
},
1919
'component': {
20-
'supportFile': 'cypress/support/component.ts',
2120
devServer (cypressConfig, devServerConfig) {
2221
const { startDevServer } = require('@cypress/vite-dev-server')
2322

packages/frontend-shared/cypress/e2e/support/e2eProjectDirs.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export const e2eProjectDirs = [
3131
'max-listeners',
3232
'migration',
3333
'multiple-task-registrations',
34-
'multiples-config-files-with-json',
34+
'multiple-config-files-with-json',
35+
'multiple-support-files',
3536
'no-scaffolding',
3637
'no-server',
3738
'no-specs-found',

packages/launchpad/cypress/e2e/onboarding-flow.cy.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,39 @@ describe('Launchpad: Onboarding Flow', () => {
2424
cy.findByText('I\'ve installed them').click()
2525
cy.findByText('We added the following files to your project.')
2626
cy.findByText('Continue').click()
27-
cy.withCtx(async (ctx) => {
27+
cy.withCtx((ctx) => {
2828
return ctx.file.readFileInProject('cypress.config.js')
29-
}).then((str) => {
30-
cy.log(str)
3129
})
3230

3331
cy.findByText('Choose a Browser', { timeout: 10000 })
3432
})
3533

34+
it('can setup component testing with TS', () => {
35+
cy.visitLaunchpad()
36+
cy.get('[data-cy-testingType=component]').click()
37+
cy.get('[data-testid=select-framework]').click()
38+
cy.findByText('React.js').click()
39+
cy.get('[data-testid=select-framework]').should('contain', 'React.js')
40+
cy.get('[data-testid=select-bundler]')
41+
.findByText(cy.i18n.setupPage.projectSetup.bundlerPlaceholder)
42+
.click()
43+
44+
cy.findByText('Webpack').click()
45+
cy.get('[data-testid=select-bundler]').should('contain', 'Webpack')
46+
cy.reload()
47+
cy.get('[data-testid=select-framework]').should('contain', 'React.js')
48+
cy.get('[data-testid=select-bundler]').should('contain', 'Webpack')
49+
cy.findByText('TypeScript').click()
50+
cy.findByText('Next Step').click()
51+
cy.get('h1').should('contain', 'Dependencies')
52+
cy.findByText('I\'ve installed them').click()
53+
cy.findByText('We added the following files to your project.')
54+
cy.findByText('Continue').click()
55+
cy.withCtx((ctx) => {
56+
return ctx.file.readFileInProject('cypress.config.ts')
57+
})
58+
})
59+
3660
it('can setup e2e testing', () => {
3761
cy.visitLaunchpad()
3862
cy.get('[data-cy-testingType=e2e]').click()

packages/reporter/cypress.config.ts

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export default defineConfig({
1414
'openMode': 0,
1515
},
1616
'e2e': {
17-
'supportFile': 'cypress/support/e2e.ts',
1817
setupNodeEvents (on, config) {
1918
const express = require('express')
2019

packages/server/lib/config.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,29 @@ export const setNodeBinary = (obj, userNodePath, userNodeVersion) => {
348348
}
349349

350350
// async function
351-
export function setSupportFileAndFolder (obj, defaults) {
351+
export async function setSupportFileAndFolder (obj, defaults) {
352352
if (!obj.supportFile) {
353353
return Bluebird.resolve(obj)
354354
}
355355

356356
obj = _.clone(obj)
357357

358+
const ctx = getCtx()
359+
360+
const supportFilesByGlob = await ctx.file.getFilesByGlob(obj.projectRoot, obj.supportFile, { absolute: false })
361+
362+
if (supportFilesByGlob.length > 1) {
363+
return errors.throw('MULTIPLE_SUPPORT_FILES_FOUND', obj.supportFile, supportFilesByGlob.join(', '))
364+
}
365+
366+
if (supportFilesByGlob.length === 0) {
367+
const configFile = obj.configFile || defaults.configFile
368+
369+
return errors.throw('SUPPORT_FILE_NOT_FOUND', path.resolve(obj.projectRoot, obj.supportFile), configFile)
370+
}
371+
358372
// TODO move this logic to find support file into util/path_helpers
359-
const sf = obj.supportFile
373+
const sf = supportFilesByGlob[0]
360374

361375
debug(`setting support file ${sf}`)
362376
debug(`for project root ${obj.projectRoot}`)

packages/server/lib/errors.js

+9
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,15 @@ const getMsgByType = function (type, ...args) {
542542
https://on.cypress.io/renderer-process-crashed`
543543
case 'AUTOMATION_SERVER_DISCONNECTED':
544544
return 'The automation client disconnected. Cannot continue running tests.'
545+
546+
case 'MULTIPLE_SUPPORT_FILES_FOUND':
547+
return stripIndent`\
548+
There are multiple support files.
549+
550+
Your \`supportFile\` is set to \`${arg1}\`, and we found \`${arg2}\`.
551+
552+
Correct your supportFile config or merge the files into one.`
553+
545554
case 'SUPPORT_FILE_NOT_FOUND':
546555
return stripIndent`\
547556
The support file is missing or invalid.

packages/server/test/unit/config_spec.js

-1
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,6 @@ describe('lib/config', () => {
19971997

19981998
context('.setSupportFileAndFolder', () => {
19991999
const mockSupportDefaults = {
2000-
supportFile: 'cypress/support/e2e.ts',
20012000
supportFolder: false,
20022001
configFile: 'cypress.json',
20032002
}

system-tests/__snapshots__/config_spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ Cypress no longer supports 'cypress.json', please migrate to 'cypress.config.{ts
176176

177177
exports['e2e config throws error when cypress.json is found in project and cypress.config.{ts|js} exists as well 1'] = `
178178
There is both a \`cypress.config.js\` and a cypress.json file at the location below:
179-
/foo/bar/.projects/multiples-config-files-with-json
179+
/foo/bar/.projects/multiple-config-files-with-json
180180
181181
Cypress no longer supports 'cypress.json' config, please remove it from your project.
182182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
exports['e2e multiple support files passes 1'] = `
2+
There are multiple support files.
3+
4+
Your \`supportFile\` is set to \`/foo/bar/.projects/multiple-support-files/cypress/support/e2e.{js,jsx,ts,tsx}\`, and we found \`/foo/bar/.projects/multiple-support-files/cypress/support/e2e.js, /foo/bar/.projects/multiple-support-files/cypress/support/e2e.ts\`.
5+
6+
Correct your supportFile config or merge the files into one.
7+
8+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it('is true', () => {
2+
expect(true).to.be.true
3+
})

system-tests/projects/multiple-support-files/cypress/support/e2e.js

Whitespace-only changes.

system-tests/projects/multiple-support-files/cypress/support/e2e.ts

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
module.exports = {
2-
'e2e': {
3-
'supportFile': 'cypress/support/e2e.ts',
4-
},
5-
}
1+
module.exports = {}

system-tests/test/config_spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ describe('e2e config', () => {
103103
})
104104

105105
it('throws error when cypress.json is found in project and cypress.config.{ts|js} exists as well', function () {
106-
Fixtures.scaffoldProject('multiples-config-files-with-json')
107-
Fixtures.projectPath('multiples-config-files-with-json')
106+
Fixtures.scaffoldProject('multiple-config-files-with-json')
107+
Fixtures.projectPath('multiple-config-files-with-json')
108108

109109
return systemTests.exec(this, {
110-
project: 'multiples-config-files-with-json',
110+
project: 'multiple-config-files-with-json',
111111
expectedExitCode: 1,
112112
snapshot: true,
113113
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const systemTests = require('../lib/system-tests').default
2+
3+
describe('e2e multiple support files', () => {
4+
systemTests.setup()
5+
6+
it('passes', function () {
7+
return systemTests.exec(this, {
8+
project: 'multiple-support-files',
9+
sanitizeScreenshotDimensions: true,
10+
snapshot: true,
11+
expectedExitCode: 1,
12+
onStdout: systemTests.normalizeWebpackErrors,
13+
})
14+
})
15+
})

0 commit comments

Comments
 (0)