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

Allow plugin authors to extend the Cypress config types #22127

Open
Lakitna opened this issue Jun 6, 2022 · 3 comments
Open

Allow plugin authors to extend the Cypress config types #22127

Lakitna opened this issue Jun 6, 2022 · 3 comments
Labels
type: enhancement Requested enhancement of existing feature

Comments

@Lakitna
Copy link
Contributor

Lakitna commented Jun 6, 2022

What would you like?

The ability for extension authors to extend the types used in the defineConfig function when configuring Cypress via cypress.config.ts (or, to a lesser extent, cypress.config.js).

Why is this needed?

I am the author of cypress-commands which adds a few new commands and extends some others.

One of the extended commands also adds a new property to the config file. In this case, the extended cy.request() uses the requestBaseUrl config property. Details on the command: https://github.com/Lakitna/cypress-commands/blob/develop/docs/request.md

I have tried to extend it using the same approach as with adding commands to the types. It does not work as-is, the type validation keeps throwing errors.

Other

To make things seamless for users, I want to refrain from using/recommending workarounds like:

  • Directly export the config without using defineConfig. This skips the type checks.
  • Use cypress.config.js instead of .ts. This skips the type checks.
@mjhenkes mjhenkes added the type: enhancement Requested enhancement of existing feature label Jun 6, 2022
@cypress-bot cypress-bot bot added the stage: investigating Someone from Cypress is looking into this label Jun 6, 2022
@ZachJW34
Copy link
Contributor

ZachJW34 commented Jun 6, 2022

I tried to extend the return type of defineConfig (ConfigOptions) and was unable to do so. We define it as type ConfigOptions, but if it was declared as an interface you should be able to export .d.ts file that would merge the interfaces like so:

// my-package/types/index.d.ts
declare global {
  namespace Cypress {
    interface ConfigOptions {
      requestBaseUrl?: string;
    }
  }
}

As a workaround, you could export your own defineConfig function that adds your desired values:

interface MyConfigOptions extends Cypress.ConfigOptions {
  requestBaseUrl: string;
}

const myDefineConfig = (config: MyConfigOptions): MyConfigOptions =>
  config;

// usage
export default myDefineConfig({ ... });

@cypress-bot cypress-bot bot added stage: routed to e2e-core and removed stage: investigating Someone from Cypress is looking into this labels Jun 7, 2022
@Lakitna
Copy link
Contributor Author

Lakitna commented Jun 8, 2022

That's a better workaround than the ones I had up to now, but is still limited in that only 1 plugin can change the config types. In any case, progress :)

Sounds like it would be a relatively easy fix on the Cypress side to make Cypress.ConfigOptions into an interface, right? Is anyone willing to share an approximate ETA?

@flotwig
Copy link
Contributor

flotwig commented Aug 22, 2022

Hey @Lakitna, we don't currently have this prioritized but we would absolutely accept a PR that updates the types to work like you've described.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement Requested enhancement of existing feature
Projects
None yet
Development

No branches or pull requests

7 participants