cypress-io / cypress

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

Make it possible to import various Cypress types #27973

Open mgol opened 11 months ago

mgol commented 11 months ago

What would you like?

We have a lot of imports like:

import type { Interception as CypressInterceptor } from 'cypress/types/net-stubbing';

in the app.

This used to work fine until I tried to update TypeScript to a version that properly parses the exports field (5.2 but I think all >=5.0 might behave in a similar way) in package.json, which for Cypress contains sub-entries like:

    ".": {
      "types": "./types/index.d.ts",
      "import": "./index.mjs",
      "require": "./index.js"
    },

Since there's no entry starting with ./types, you can't import from this file like that.

That may be fine but importing directly from cypress doesn't work:

import type { Interception as CypressInterceptor } from 'cypress';

This happens for two reasons. First, types/cypress-npm-api.d.ts declares the cypress module and imports of cypress lead to it. This can be hacked around by importing from cypress/ instead:

import type { Interception as CypressInterceptor } from 'cypress';

but types/index.d.ts is not a module - it does not import or export anything directly, it just has a bunch of pragmas like:

/// <reference path="./net-stubbing.d.ts" />

It seems all the types are supposed to do is to type the built-in globals.

Why is this needed?

A lot of the types Cypress defines are useful for external consumers, e.g. to build custom interceptors, etc. It doesn't seem like there's a way to use the provided types with new TypeScript. Right now, it seems my choices are either hacking the cypress package to export what I need or to redefine all the types in the project - which may be quite daunting as they can get pretty complex.

Other

No response

mgol commented 11 months ago

To be clear, this still works with "moduleResolution": "node10" (or "node" which is just a deprecated alias of "node10" now) but if you try "node16" or any other modern setting, the exports field is consulted and the above problem appears.

borislit commented 6 months ago

Any news on this?

NathanKleekamp commented 5 months ago

Yeah, I'd also like to throw a vote for supporting this sooner rather than later. I would like to start using sub-path exports, but as soon as I turn on moduleResolution: bundler in an application that has Cypress e2e, I start getting Cannot find module 'cypress/types/net-stubbing' or its corresponding type declarations.

aniketjivtode123 commented 2 months ago

Any update or workaround on this ?

nowinskp commented 1 month ago

Any updates or plans for fixing this issue?