Import Cypress functions, no more globals
See original GitHub issueCurrent behavior:
Cypress uses global variables: describe, it, etc. This is implicit and makes it unclear where they are coming from. Linters also don’t like it and for good reason. On top of that, I really dislike the BDD style and would much prefer TDD (naming these suite and test), but I can’t do that since Cypress dictates the name for me and I would never want test as a global variable (too generic and easy to conflict with other pieces of code). There are numerous downsides to global variables and the entire web platform is moving away from them.
Desired behavior:
I can import what I need from Cypress. Something like:
const { describe, it } = require('cypress');
Versions
All versions.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:28
- Comments:17 (4 by maintainers)
Top Results From Across the Web
How to Avoid Using Global Cypress Variables - Gleb Bahmutov
Avoid clashing global types between Cypress and Jest by using local-cypress library. Global variables are the worst thing in programming, but ...
Read more >javascript - Cypress.io: Is is possible to set global variables in ...
This enables you to import functions into each spec file, thus making them way more organized and obvious than using globals.
Read more >Variables and Aliases - Cypress Documentation
The first and most important concept you should recognize is... Return Values. You cannot assign or work with the return values of any...
Read more >Environment Variables | Cypress Documentation
When your tests are running, you can use the Cypress.env function to access the values of your environment variables. Option #1: configuration file....
Read more >Configuration - Cypress Documentation
For more info on upgrading configuration to Cypress 10, see the migration guide. ... This file is used to store any configuration specific...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’ve only recently started using Cypress and so far the onboarding experience has been great. Props to you!
However, regarding this particular question, I think Cypress still has room for improvement. Without meaning to sound condescending, who the heck thought using globals for testing was a good idea in the first place?
Saying other popular frameworks do it this way is no legitimate argument in my opinion. You’re still following suit on a bad practice that has proven time and again in software development to lead to confusion, unnecessary tooling and setup steps, and in the end “magical” seeming code. And in return what are the benefits?
With ESM, JavaScript now has such a nice and widely adopted standard for imports and exports. Let’s all #UseThePlatform.
Any update about this feature?
It will help us a lot for monorepo users. See: https://github.com/cypress-io/cypress/issues/1319#issuecomment-517489190