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

In TypeScript, allow referencing Cypress and Chai globals as properties of the global object. #27539

Closed
rewento opened this issue Aug 11, 2023 · 1 comment · Fixed by #27540
Closed
Labels
E2E Issue related to end-to-end testing type: feature New feature that does not currently exist

Comments

@rewento
Copy link
Contributor

rewento commented Aug 11, 2023

What would you like?

I'd like to be able to reference these Cypress and Chai globals as properties of the global object (which they are) in TypeScript without the TypeScript compiler reporting an error.

  • Cypress
  • cy
  • assert
  • expect

The current type declarations prohibit this by incorrectly declaring that the objects in question are held in local variables of the global scope rather than in properties of the global object.

For example, the TypeScript compiler currently accepts these statements:

const browserVersion = Cypress.browser.version;
cy.mount(AppComponent);
assert("foo" !== "bar", "foo is not bar");
expect(true).not.to.be.false;

But, misguided by inaccurate type declarations, the TypeScript compiler issues an error for each of these statements:

const browserVersion = window.Cypress.browser.version;
window.cy.mount(AppComponent);
window.assert("foo" !== "bar", "foo is not bar");
window.expect(true).not.to.be.false;
const browserVersion = globalThis.Cypress.browser.version;
globalThis.cy.mount(AppComponent);
globalThis.assert("foo" !== "bar", "foo is not bar");
globalThis.expect(true).not.to.be.false;

Why is this needed?

Resolving this issue entails swapping the use of const for var in type declarations in cli/types/cypress-expect.d.ts and cli/types/cypress-global-vars.d.ts.

This small change would accommodate a coding style convention that demands that values in properties of the global object be accessed via the globalThis property of that object to enhance clarity regarding the origin of the values as globals as opposed to values defined in and imported from modules.

The change would have no impact on the TypeScript compiler's acceptance of code that uses Cypress globals without that convention, which is the majority of such code.

Other

No response

@nagash77 nagash77 added type: feature New feature that does not currently exist E2E Issue related to end-to-end testing labels Aug 14, 2023
@jennifer-shehane
Copy link
Member

Released in Cypress 13.0.0.

@cypress-io cypress-io locked as resolved and limited conversation to collaborators Aug 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
E2E Issue related to end-to-end testing type: feature New feature that does not currently exist
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants