Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use JSX/TSX in generated spec filenames #25318

Merged
merged 3 commits into from
Jan 6, 2023

Conversation

mike-plummer
Copy link
Contributor

User facing changelog

  • Utilize a JSX/TSX file extension when generating a new empty spec file if project contains at least one file with those extensions. This applies only to component testing and is skipped if the project specPattern has been configured to exclude files with those extensions.

Additional details

Original writeup was specific to React which, while likely the largest use case for JSX, is not the only setup that can use JSX. It's also not necessary to use a JSX extension if using React (or other frameworks). For these reasons, I think the only reliable way to determine if we should use a JSX extension for the default spec filename is to check whether there are other existing files using that extension and it would not violate the project's configured specPattern to use it (so we don't generate "disappearing" spec files)

Steps to test

  1. Scaffold a CT project using any framework, keep default specPattern
  2. Open Cypress, use "Create new empty spec" button. Verify suggested filename uses cy.js (not JSX)
  3. Close Cypress, add a JSX file within the project (can be empty)
  4. Reopen Cypress, use "Create new empty spec" button. Verify suggested filename now uses cy.jsx (CHANGED BEHAVIOR)
  5. Close Cypress, add a typescript dependency to the project and rename cypress.config.js to cypress.config.ts
  6. Reopen Cypress, use "Create new empty spec" button. Verify suggested filename now uses cy.tsx (CHANGED BEHAVIOR)
  7. Close Cypress, update the CT specPattern to **/*.cy.js
  8. Reopen Cypress, use "New spec" button. Verify suggested filename now uses cy.ts (not TSX)

How has the user experience changed?

PR Tasks

  • Have tests been added/updated?
  • Has the original issue (or this PR, if no issue exists) been tagged with a release in ZenHub? (user-facing changes only)
  • [na] Has a PR for user-facing changes been opened in cypress-documentation?
  • [na] Have API changes been updated in the type definitions?

@cypress
Copy link

cypress bot commented Dec 30, 2022



Test summary

26393 0 1179 0Flakiness 33


Run details

Project cypress
Status Passed
Commit d5881b7
Started Dec 30, 2022 8:18 PM
Ended Dec 30, 2022 8:35 PM
Duration 16:35 💡
OS Linux Debian -
Browser Multiple

View run in Cypress Dashboard ➡️


Flakiness

create-from-component.cy.ts Flakiness
1 ... > runs generated spec
commands/net_stubbing.cy.ts Flakiness
1 network stubbing > intercepting request > can delay and throttle a StaticResponse
2 network stubbing > intercepting request > can delay and throttle a StaticResponse
3 network stubbing > intercepting request > can delay and throttle a StaticResponse
4 ... > stops waiting when an fetch request is canceled
This comment includes only the first 5 flaky tests. See all 33 flaky tests in the Cypress Dashboard.

This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

// If generating a component test then check whether there are JSX/TSX files present in the project.
// If project uses JSX then user likely wants to use JSX for their tests as well.
// JSX can be used (or not used) with a variety of frameworks depending on user preference/config, so
// the only reliable way to determine is whether there are files with JSX extension present
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately it would be nice to put the extension preference for generated tests in the component testing config or something so there is the ability to override this if needed 🤔. But I think this is a good rubric, especially for new people to get what they probably expect.

@mike-plummer mike-plummer requested a review from a team January 3, 2023 14:35
@marktnoonan
Copy link
Contributor

marktnoonan commented Jan 3, 2023

Testing more, but in a Vue project that use jsx for tests, I get a .js file extension:

Screen Shot 2023-01-03 at 11 12 06 AM

It's this project: https://github.com/marktnoonan/cy-validators-example/tree/what-component

@marktnoonan
Copy link
Contributor

marktnoonan commented Jan 3, 2023

Works well in the React project I tried and in one of our other Vue example projects. Interestingly in the project that's not working as expected, the defaultSpecFileName is generated correctly and works fine for new specs that aren't coming from "Create from Component".

Screen Shot 2023-01-03 at 12 34 18 PM

Screen Shot 2023-01-03 at 12 34 28 PM

Copy link
Contributor

@marktnoonan marktnoonan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took me a few reads of everything to understand that the scope of this doesn't include "Create from Component" - so the Vue problems I reported are actually unrelated, I made a new issue here to track: #25346

@mike-plummer mike-plummer requested a review from a team January 3, 2023 20:05
// the only reliable way to determine is whether there are files with JSX extension present
if (this.ctx.coreData.currentTestingType === 'component') {
debug('Checking for jsx/tsx files to determine file extension for default spec filename')
const projectJsxFiles = await this.ctx.file.getFilesByGlob(this.ctx.currentProject ?? '', '**/*.[jt]sx')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this check not be framework based? If it's react -> {fileExtensionToUse}x else ${fileExtensionToUse}?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to use JSX with React (albeit 99.9% of people do), and you can use JSX with non-React frameworks (like we do with our Vue tests). Doing a "is there already a JSX/TSX file present" seemed like the most reliable option, but I'll defer to the team

Copy link
Contributor

@ZachJW34 ZachJW34 Jan 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to think through some edge-cases, like a user having some jsx file for storybook but not wanting to have that pollute their generated Cypress tests if they don't plan on using jsx to write their CT tests. I think the proper solution is to have a more deterministic output like what framework they have and then, like @marktnoonan pointed out, having a user config override.

In the end this is just a suggestion for a filename and the user can change it if they want. I'm cool with this if this gets us closer, especially for react devs as that is our majority.

@mike-plummer mike-plummer merged commit acd14a8 into develop Jan 6, 2023
@mike-plummer mike-plummer deleted the mikep/24495-use-jsx-in-generated-filename branch January 6, 2023 16:34
tgriesser added a commit that referenced this pull request Jan 18, 2023
* develop: (45 commits)
  fix: re-enable CYPRESS_INTERNAL_VITE_DEV development (#25364)
  fix: add skip domain injection description (#25463)
  fix: revert CSP header and script-src addition (#25445)
  chore: Update v8 snapshot cache (#25401)
  feat: Do not strip CSP headers from HTTPResponse (#24760)
  fix: keep spaces in formatted output in test runner (#24687)
  fix: Restrict dependency versions to known supported ranges (#25380)
  chore: Update v8 snapshot cache (#25370)
  feat: experimental skip domain injection (#25307)
  chore: support vite v4 for component testing (#25365)
  feat: Use JSX/TSX in generated spec filenames (#25318)
  docs(angular): Properties that are spied upon have to be defined within `componentProperties` instead of on root level. (#25359)
  chore: remove lint-changed from scripts/docs (#25308)
  chore: bump to 12.3.0 [skip ci] (#25355)
  fix: make NODE_ENV "production" for prod builds of launchpad (#25320)
  fix: .contains() should only return one element at all times (#25250)
  feat: add currentRetry to Cypress API (#25297)
  chore: release @cypress/webpack-dev-server-v3.2.2
  chore: release create-cypress-tests-v2.0.1
  fix: change wording for spec creation (#25271)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scaffolds TS file for TSX project
3 participants