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

[typescript] Cannot find name 'JQuery' #96

Closed
a-b-r-o-w-n opened this issue Nov 14, 2019 · 13 comments
Closed

[typescript] Cannot find name 'JQuery' #96

a-b-r-o-w-n opened this issue Nov 14, 2019 · 13 comments

Comments

@a-b-r-o-w-n
Copy link

  • cypress-testing-library version: 5.0.2
  • node version: 12.13.0
  • npm (or yarn) version: (yarn) 1.19.1

What you did:
Installed @testing-library/cypress in monorepo root (yarn add -W -D @testing-library/cypress).

What happened:
Unrelated build failures in packages/ resulting in

../../../node_modules/@types/testing-library__cypress/index.d.ts:445:77 - error TS2304: Cannot find name 'JQuery'.

Reproduction repository:
https://github.com/a-b-r-o-w-n/testing-library-cypress-bug
https://github.com/a-b-r-o-w-n/testing-library-cypress-bug/runs/303541569

@gabceb
Copy link

gabceb commented Nov 27, 2019

I just experienced the same thing when bumping to the latest version (5.0.2)

@kentcdodds
Copy link
Member

Sorry, I don't have the bandwidth to look at this right now. Anyone is welcome to dig a little bit and suggest a solution.

@simjes
Copy link
Contributor

simjes commented Dec 13, 2019

We've had a discussion about this over in the DefinitelyTyped repo DefinitelyTyped/DefinitelyTyped#40099

@a-b-r-o-w-n
Copy link
Author

@simjes that thread does not really offer a solid workaround. In my case I had to add the compiler option --skipLibCheck. Not ideal, but gets me past this error.

I wonder if this would be a problem if this package included its own typings instead of hosting the typings on the definitel-typed project? The problem seems to stem from its usage of the global namespace and the typescript compiler's greediness when loading @types/.

Just a thought. I assume there is some historical context for why the types are not co-located with this project.

@NicholasBoll
Copy link
Contributor

@a-b-r-o-w-n Do you know if there is a node_modules/@types/cypress in your project? If there is, bad things will happen. There also shouldn't be a node_modules/@types/jquery. If these are present and you don't have either dependencies listed in your package.json, then your IDE (VSCode seems to be the worst offender here) has preemptively installed these types for you.

Cypress had a lot of issues with global/namespace conflicts and chose to not directly require any but instead copy/paste type definitions nested in Cypress type definitions. In most cases, this solved namespace collisions assuming the cypress folder explicitly states that only cypress types should be included in the project.

Type definitions were originally a part of this project, but the source code is not in Typescript so the type definitions were moved to DefinitelyTyped: #68

@types/testing-library__cypress updated to no longer require a specific version of cypress: DefinitelyTyped/DefinitelyTyped#41917

Let us know if you are still running into issues.

@JochenDiekenbrock
Copy link

I see the same error with @testing-library/cypress version 5.2.1.
There is neither node_modules/@types/cypress nor node_modules/@types/jquery in the monorepo

@NicholasBoll
Copy link
Contributor

@a-b-r-o-w-n I updated the reproduction repo a bit by changing the tsconfig.json

File in full:

{
  "compilerOptions": {
    "outDir": "./lib/",
    "module": "commonjs",
    "lib": ["es2015", "dom"],
    "target": "es5",
    "moduleResolution": "node",
    "strict": true,
    "types": ["cypress"],
  },
  "exclude": [
    "node_modules"
  ],
}

Changes:

diff --git a/packages/client/tsconfig.json b/packages/client/tsconfig.json
index dda9816..8bbbb78 100644
--- a/packages/client/tsconfig.json
+++ b/packages/client/tsconfig.json
@@ -2,11 +2,12 @@
   "compilerOptions": {
     "outDir": "./lib/",
     "module": "commonjs",
+    "lib": ["es2015", "dom"],
     "target": "es5",
     "moduleResolution": "node",
     "strict": true,
+    "types": ["cypress"],
   },
-  "include": ["./src/**/*"],
   "exclude": [
     "node_modules"
   ],

I added "types": ["cypress"]. That normally would go into a cypress/ directory since cypress would be the only global you'd want in that directory (to prevent issues with jest or jasmine)

I also added the es2015 lib. That works for Chrome and Firefox which Cypress supports without a runtime transpilation. Also Cypress needs the dom library.

I dropped the include. It was interfering. By default it will include all .ts, .tsx and .d.ts files. You could change it to src/**/* it would also fix the issue.

@JochenDiekenbrock Maybe this could help you as well?

@JochenDiekenbrock
Copy link

Thank you, @NicholasBoll
The error message in our case is shown when building apps and libs in a (Lerna) monorepo that do not use Cypress at all. There is only one app with Cypress e2e-tests in the monorepo.

Adding "types": ["cypress"] to the apps that do not use Cypress fixes the compile error, but the generated artifacts are different and I'm not sure that they are built correctly. Anyways, having the add the types entry to tsconfigs of other libs/apps does not sound correct.

@NicholasBoll
Copy link
Contributor

@JochenDiekenbrock I'm not sure how your monorepo is set up. The project I work on has a base tsconfig that all packages inherit from that has "types": "jest" since all packages use jest.

Only the cypress folder has a tsconfig with "types": "cypress" in it. The Cypress folder is not in any packages

@gnapse
Copy link
Member

gnapse commented Mar 19, 2020

I hit this issue in a repo that does not have any monorepo/lerna setup. Tried various things from this discussion here and also from the one linked to above in DT, and nothing helped. The only thing that helped was to explicitly install @types/jquery as a dev dependency.

Here's the tsconfig files if that helps:

root tsconfig

{
    "compilerOptions": {
        "noEmit": true,
        "allowJs": true,
        "checkJs": true,
        "target": "es6",
        "moduleResolution": "Node",
        "resolveJsonModule": true,
        "declaration": true,
        "noImplicitReturns": true,
        "jsx": "react",
        "allowSyntheticDefaultImports": true,
        "baseUrl": "."
    },
    "include": ["./src/**/*"],
    "exclude": ["node_modules", "built", "cypress"]
}

cypress/tsconfig.json

{
    "compilerOptions": {
        "strict": true,
        "baseUrl": "../node_modules",
        "target": "es5",
        "lib": ["es5", "dom"],
        "types": ["cypress"]
    },
    "exclude": []
}

@raphaelsaunier
Copy link

I ran into the same issue in a monorepo that uses Yarn Workspaces and where Cypress wasn't installed at the root and where no root tsconfig.json was present.

I got rid of the warning with the same workaround that @gnapse proposed, by adding @types/jquery to the "devDependencies" of the package that is using Cypress.

@kentcdodds
Copy link
Member

This is probably resolved now that the type defs are built-in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants