Skip to content

Commit acd14a8

Browse files
authored
feat: Use JSX/TSX in generated spec filenames (#25318)
1 parent 4baad8e commit acd14a8

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

packages/app/cypress/e2e/specs.cy.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ describe('App: Specs', () => {
575575

576576
it('shows success modal when template spec is created', () => {
577577
cy.get('@CreateEmptySpecDialog').within(() => {
578-
cy.findByLabelText('Enter a relative path...').invoke('val').should('eq', getPathForPlatform('cypress/component/ComponentName.cy.ts'))
578+
cy.findByLabelText('Enter a relative path...').invoke('val').should('eq', getPathForPlatform('cypress/component/ComponentName.cy.tsx'))
579579

580580
cy.findByLabelText('Enter a relative path...').clear().type('cypress/my-empty-spec.cy.js')
581581

@@ -603,7 +603,7 @@ describe('App: Specs', () => {
603603

604604
it('navigates to spec runner when selected', () => {
605605
cy.get('@CreateEmptySpecDialog').within(() => {
606-
cy.findByLabelText('Enter a relative path...').invoke('val').should('eq', getPathForPlatform('cypress/component/ComponentName.cy.ts'))
606+
cy.findByLabelText('Enter a relative path...').invoke('val').should('eq', getPathForPlatform('cypress/component/ComponentName.cy.tsx'))
607607

608608
cy.findByLabelText('Enter a relative path...').clear().type('cypress/my-empty-spec.cy.js')
609609

@@ -623,7 +623,7 @@ describe('App: Specs', () => {
623623

624624
it('displays alert with docs link on new spec', () => {
625625
cy.get('@CreateEmptySpecDialog').within(() => {
626-
cy.findByLabelText('Enter a relative path...').invoke('val').should('eq', getPathForPlatform('cypress/component/ComponentName.cy.ts'))
626+
cy.findByLabelText('Enter a relative path...').invoke('val').should('eq', getPathForPlatform('cypress/component/ComponentName.cy.tsx'))
627627

628628
cy.findByLabelText('Enter a relative path...').clear().type('cypress/my-empty-spec.cy.js')
629629

@@ -756,7 +756,7 @@ describe('App: Specs', () => {
756756
cy.findByRole('dialog', {
757757
name: 'Enter the path for your new spec',
758758
}).within(() => {
759-
cy.findByLabelText('Enter a relative path...').invoke('val').should('eq', getPathForPlatform('src/specs-folder/ComponentName.js'))
759+
cy.findByLabelText('Enter a relative path...').invoke('val').should('eq', getPathForPlatform('src/specs-folder/ComponentName.jsx'))
760760

761761
cy.findByRole('button', { name: 'Create spec' }).click()
762762
})

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,42 @@ export class ProjectDataSource {
419419

420420
async defaultSpecFileName (): Promise<string> {
421421
const { specPattern = [] } = await this.ctx.project.specPatterns()
422+
let fileExtensionToUse: FileExtension = this.ctx.lifecycleManager.fileExtensionToUse
423+
424+
// If generating a component test then check whether there are JSX/TSX files present in the project.
425+
// If project uses JSX then user likely wants to use JSX for their tests as well.
426+
// JSX can be used (or not used) with a variety of frameworks depending on user preference/config, so
427+
// the only reliable way to determine is whether there are files with JSX extension present
428+
if (this.ctx.coreData.currentTestingType === 'component') {
429+
debug('Checking for jsx/tsx files to determine file extension for default spec filename')
430+
const projectJsxFiles = await this.ctx.file.getFilesByGlob(this.ctx.currentProject ?? '', '**/*.[jt]sx')
431+
432+
if (projectJsxFiles.length > 0) {
433+
debug('At least one jsx/tsx file found in project, utilizing for default spec filename')
434+
const generatedSpecFileName = await getDefaultSpecFileName({
435+
currentProject: this.ctx.currentProject,
436+
testingType: this.ctx.coreData.currentTestingType,
437+
fileExtensionToUse: `${fileExtensionToUse}x`,
438+
specs: this.specs,
439+
specPattern,
440+
})
441+
442+
// There is the possibility that a specPattern has been configured to exclude spec files using jsx/tsx extensions
443+
// In this case, fallback to default logic which will generate js/ts filename
444+
if (await this.matchesSpecPattern(generatedSpecFileName)) {
445+
return generatedSpecFileName
446+
}
447+
448+
debug('jsx/tsx extension would violate configured specPattern, utilizing default spec filename')
449+
} else {
450+
debug('No jsx/tsx files found, utilizing default spec filename')
451+
}
452+
}
422453

423454
return getDefaultSpecFileName({
424455
currentProject: this.ctx.currentProject,
425456
testingType: this.ctx.coreData.currentTestingType,
426-
fileExtensionToUse: this.ctx.lifecycleManager.fileExtensionToUse,
457+
fileExtensionToUse,
427458
specs: this.specs,
428459
specPattern,
429460
})

packages/data-context/test/unit/sources/ProjectDataSource.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,5 +818,29 @@ describe('ProjectDataSource', () => {
818818

819819
expect(defaultSpecFileName).to.equal('cypress/component-tests/foo/ComponentName.spec.js')
820820
})
821+
822+
describe('jsx/tsx handling', () => {
823+
beforeEach(async () => {
824+
ctx.coreData.currentTestingType = 'component'
825+
await ctx.actions.file.writeFileInProject(path.join('src', 'components', 'App.jsx'), '// foo')
826+
})
827+
828+
it('yields correct jsx extension if there are jsx files and specPattern allows', async () => {
829+
sinon.stub(ctx.project, 'specPatterns').resolves({ specPattern: [defaultSpecPattern.component] })
830+
831+
const defaultSpecFileName = await ctx.project.defaultSpecFileName()
832+
833+
expect(defaultSpecFileName).to.equal('cypress/component/ComponentName.cy.jsx', defaultSpecFileName)
834+
})
835+
836+
it('yields non-jsx extension if there are jsx files but specPattern disallows', async () => {
837+
sinon.stub(ctx.project, 'specPatterns').resolves({ specPattern: ['cypress/component/*.cy.js'] })
838+
839+
const defaultSpecFileName = await ctx.project.defaultSpecFileName()
840+
841+
// specPattern does not allow for jsx, so generated spec name should not use jsx extension
842+
expect(defaultSpecFileName).to.equal('cypress/component/ComponentName.cy.js', defaultSpecFileName)
843+
})
844+
})
821845
})
822846
})

0 commit comments

Comments
 (0)