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

diagnostics - Cypress.env is not a function when referring to env object as const #1120

Closed
3 tasks done
jessica-jorgenson opened this issue Nov 14, 2023 · 8 comments
Closed
3 tasks done

Comments

@jessica-jorgenson
Copy link

jessica-jorgenson commented Nov 14, 2023

Current behavior

Upon running npx cypress-cucumber-diagnostics, I am met with the error:

TypeError: Cypress.env is not a function
    at cypress/support/appOne/pages.ts (C:\Users\JessicaJorgenson\Repos\CypressExample\.output-7bedd1.cjs:15748:22)
    at __init (C:\Users\JessicaJorgenson\Repos\CypressExample\.output-7bedd1.cjs:10:58)
    at cypress/support/appOne/step-definitions/preconditions.steps.ts (C:\Users\JessicaJorgenson\Repos\CypressExample\.output-7bedd1.cjs:15762:7)
    at __init (C:\Users\JessicaJorgenson\Repos\CypressExample\.output-7bedd1.cjs:10:58)
    at C:\Users\JessicaJorgenson\Repos\CypressExample\.output-7bedd1.cjs:15770:3
    at Object.<anonymous> (C:\Users\JessicaJorgenson\Repos\CypressExample\.output-7bedd1.cjs:15771:3)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
Error: Failed to evaluate step definitions, with errors shown above...
    at C:\Users\JessicaJorgenson\Repos\CypressExample\node_modules\@badeball\cypress-cucumber-preprocessor\dist\diagnostics\diagnose.js:110:27
    at withRegistry (C:\Users\JessicaJorgenson\Repos\CypressExample\node_modules\@badeball\cypress-cucumber-preprocessor\dist\registry.js:188:5)
    at diagnose (C:\Users\JessicaJorgenson\Repos\CypressExample\node_modules\@badeball\cypress-cucumber-preprocessor\dist\diagnostics\diagnose.js:103:52)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async execute (C:\Users\JessicaJorgenson\Repos\CypressExample\node_modules\@badeball\cypress-cucumber-preprocessor\dist\diagnostics\index.js:189:20)

When I have a constant equal to a Cypress.env value.
See here: https://github.com/jessica-jorgenson/cucumber-preprocessor-error/blob/main/cypress/support/appOne/pages.ts

When referring to the env like so: return Cypress.env("urls").main.replace("bing", "google"), the error doesn't occur.

Desired behavior

Cypress behaves as usual, so this shouldn't raise an error on cypress-cucumber-preprocessor...riiight?

Test code to reproduce

Repo: https://github.com/jessica-jorgenson/cucumber-preprocessor-error

Versions

  • Cypress version: ^13.3.0
  • Preprocessor version: ^19.0.1
  • Node version: 18.18.0

Checklist

@jessica-jorgenson jessica-jorgenson changed the title diagnostics - Cypress.env is not a function when referring to env object outside of step definitions diagnostics - Cypress.env is not a function when referring to env object as const Nov 14, 2023
@badeball
Copy link
Owner

Cypress behaves as usual, so this shouldn't raise an error on cypress-cucumber-preprocessor...riiight?

That'd be nice, but it's not quite the case. The steps are evaluated in a non-cypress environment and these variables aren't available. This is a result of careful consideration between evaluation and static analysis. For now, you need to move invocation of Cypress function inside methods and away from global scope. I will update the documentation to further explain this, in time.

@jessica-jorgenson jessica-jorgenson closed this as not planned Won't fix, can't repro, duplicate, stale Nov 15, 2023
@jessica-jorgenson
Copy link
Author

Thank you!

badeball added a commit that referenced this issue Nov 16, 2023
badeball added a commit that referenced this issue Nov 16, 2023
@badeball
Copy link
Owner

I found that I previously actually did make attempts at mocking Cypress global during evaluation. Thus, I've improved that behavior slightly with v19.1.1 and your original code should now hopefully run fine.

@jessica-jorgenson
Copy link
Author

Oh thank you for working on this! It's definitely a step in the right direction, but it seems it's still having a hard time with the new update. Is it something else I'm doing wrong?

TypeError: Cannot read properties of undefined (reading 'main')
    at cypress/support/appOne/pages.ts (/Users/jessicajorgenson/Documents/GitHub/cucumber-preprocessor-error/.output-9565d5.cjs:15748:33)
    at __init (/Users/jessicajorgenson/Documents/GitHub/cucumber-preprocessor-error/.output-9565d5.cjs:10:58)
    at cypress/support/appOne/step-definitions/preconditions.steps.ts (/Users/jessicajorgenson/Documents/GitHub/cucumber-preprocessor-error/.output-9565d5.cjs:15762:7)
    at __init (/Users/jessicajorgenson/Documents/GitHub/cucumber-preprocessor-error/.output-9565d5.cjs:10:58)
    at /Users/jessicajorgenson/Documents/GitHub/cucumber-preprocessor-error/.output-9565d5.cjs:15770:3
    at Object.<anonymous> (/Users/jessicajorgenson/Documents/GitHub/cucumber-preprocessor-error/.output-9565d5.cjs:15771:3)
    at Module._compile (node:internal/modules/cjs/loader:1267:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1321:10)
    at Module.load (node:internal/modules/cjs/loader:1125:32)
    at Module._load (node:internal/modules/cjs/loader:965:12)
Error: Failed to evaluate step definitions, with errors shown above...
    at /Users/jessicajorgenson/Documents/GitHub/cucumber-preprocessor-error/node_modules/@badeball/cypress-cucumber-preprocessor/dist/diagnostics/diagnose.js:117:27
    at withRegistry (/Users/jessicajorgenson/Documents/GitHub/cucumber-preprocessor-error/node_modules/@badeball/cypress-cucumber-preprocessor/dist/registry.js:188:5)
    at diagnose (/Users/jessicajorgenson/Documents/GitHub/cucumber-preprocessor-error/node_modules/@badeball/cypress-cucumber-preprocessor/dist/diagnostics/diagnose.js:111:52)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async execute (/Users/jessicajorgenson/Documents/GitHub/cucumber-preprocessor-error/node_modules/@badeball/cypress-cucumber-preprocessor/dist/diagnostics/index.js:189:20)

@badeball
Copy link
Owner

Ah, the return value of mocked functions is just undefined, which means that accessing the value as an object isn't going to work. Doing something about this is out of scope for this project as of now.

@mrkjdy
Copy link

mrkjdy commented Nov 21, 2023

I also have this problem. It would be awesome if this was fixed! Is it possible to run the diagnostics script within a Cypress environment so that it doesn't need to be mocked? Maybe the diagnostics code could be ran as a Cypress test?

I'd really like to use this tool without needing to change my code.

@badeball
Copy link
Owner

Maybe the diagnostics code could be ran as a Cypress test?

This is not something I'm considering right now. I am however open to introducing static analysis with fallback, ref. #1129.

badeball added a commit that referenced this issue Sep 24, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

This is related to #1120 [1].

This closed #1129 [2].

[1] #1120
[2] #1129
badeball added a commit that referenced this issue Sep 24, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

This is related to #1120 [1].

This closes #1129 [2].

[1] #1120
[2] #1129
badeball added a commit that referenced this issue Sep 24, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

This is related to #1120 [1].

This closes #1129 [2].

[1] #1120
[2] #1129
badeball added a commit that referenced this issue Sep 24, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

This is related to #1120 [1].

This closes #1129 [2].

[1] #1120
[2] #1129
badeball added a commit that referenced this issue Sep 24, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

This is related to #1120 [1].

This closes #1129 [2].

[1] #1120
[2] #1129
badeball added a commit that referenced this issue Sep 24, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

This is related to #1120 [1].

This closes #1129 [2].

[1] #1120
[2] #1129
badeball added a commit that referenced this issue Sep 24, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

This is related to #1120 [1].

This closes #1129 [2].

[1] #1120
[2] #1129
badeball added a commit that referenced this issue Sep 24, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

This is related to #1120 [1].

This closes #1129 [2].

[1] #1120
[2] #1129
badeball added a commit that referenced this issue Sep 24, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

This is related to #1120 [1].

This closes #1129 [2].

[1] #1120
[2] #1129
badeball added a commit that referenced this issue Sep 24, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

This is related to #1120 [1].

This closes #1129 [2].

[1] #1120
[2] #1129
badeball added a commit that referenced this issue Sep 25, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

Having a postinstall not run upon installation turned out to be less
trivial than I had hoped for, however SO came to the rescue [1].

This is related to #1120 [2].

This closes #1129 [3].

[1] https://stackoverflow.com/q/54212147/4098802
[2] #1120
[3] #1129
badeball added a commit that referenced this issue Sep 25, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

Having a postinstall not run upon installation turned out to be less
trivial than I had hoped for, however SO came to the rescue [1].

This is related to #1120 [2].

This closes #1129 [3].

[1] https://stackoverflow.com/q/54212147/4098802
[2] #1120
[3] #1129
badeball added a commit that referenced this issue Sep 26, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

Having a postinstall not run upon installation turned out to be less
trivial than I had hoped for, however SO came to the rescue [1].

This is related to #1120 [2].

This closes #1129 [3].

[1] https://stackoverflow.com/q/54212147/4098802
[2] #1120
[3] #1129
badeball added a commit that referenced this issue Sep 26, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

Having a postinstall not run upon installation turned out to be less
trivial than I had hoped for, however SO came to the rescue [1].

This is related to #1120 [2].

This closes #1129 [3].

[1] https://stackoverflow.com/q/54212147/4098802
[2] #1120
[3] #1129
badeball added a commit that referenced this issue Sep 26, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

Having a postinstall not run upon installation turned out to be less
trivial than I had hoped for, however SO came to the rescue [1].

This is related to #1120 [2].

This closes #1129 [3].

[1] https://stackoverflow.com/q/54212147/4098802
[2] #1120
[3] #1129
badeball added a commit that referenced this issue Sep 28, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

Having a postinstall not run upon installation turned out to be less
trivial than I had hoped for, however SO came to the rescue [1].

This is related to #1120 [2].

This closes #1129 [3].

[1] https://stackoverflow.com/q/54212147/4098802
[2] #1120
[3] #1129
badeball added a commit that referenced this issue Sep 28, 2024
This is "more proper" in the sense that it's still executing tests an
actual Cypress environments, while still being reasonably quick.

Having a postinstall not run upon installation turned out to be less
trivial than I had hoped for, however SO came to the rescue [1].

This is related to #1120 [2].

This closes #1129 [3].

[1] https://stackoverflow.com/q/54212147/4098802
[2] #1120
[3] #1129
@badeball
Copy link
Owner

Dry run has been completely overhauled with v21.0.0 and changed in regards to the original issue.

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

No branches or pull requests

3 participants