question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Import Cypress functions, no more globals

See original GitHub issue

Current 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:open
  • Created 5 years ago
  • Reactions:28
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
janoshcommented, Jan 9, 2021

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.

3reactions
xiaoxiangmoecommented, Jan 27, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found