cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.03k stars 3.19k forks source link

Allow plugin authors to extend the Cypress config types #22127

Open Lakitna opened 2 years ago

Lakitna commented 2 years ago

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:

ZachJW34 commented 2 years ago

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({ ... });
Lakitna commented 2 years ago

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 commented 2 years ago

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.