Skip to content

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

@rewento

Description

@rewento

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    E2EIssue related to end-to-end testingtype: featureNew feature that does not currently exist

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions