cypress: Possible type definition conflict on @types/chai
The package.json uses save-exact=true. I didn’t really notice this when I added type for Cypress-wrapped types, but it can causes issues if a type definition defined global variables (you can’t duplicate global declarations). The issue I’m seeing is in @types/chai.
It looks like Cypress depends on chai@^3.5.0 (Chai is at 4 now - looks like nested was added). A project could also use chai at a different version - normally not a problem as both npm and yarn will embed on conflict detection. But TypeScript won’t handle this conflict as gracefully for type definitions. I guess there are a few options to fix this.
- dependency version could be any
* - dependency version could be some tested versions:
^3.5.0 || ^4.0.0 - If using
yarn, you can just override using a resolution: https://yarnpkg.com/lang/en/docs/selective-version-resolutions/ - Have the app remove
@types/chaias a dependency
Either dependency version string change should prevent yarn or npm from detecting a conflict and flatten the dependency.
Personally I think handling 3.5+ is okay. I can open an issue later. There is a workaround (I’m using yarn, so it will allow resolution overrides).
- Operating System: N/A
- Cypress Version: 1.2
- Browser Version: N/A
Is this a Feature or Bug?
Bug
How to reproduce:
If your application uses TypeScript and you depend on @types/chai in any version other than 4.0.8
Additional Info (images, stack traces, etc)
[CYP] ERROR in ~/{project}/node_modules/cypress/node_modules/@types/chai/index.d.ts
[CYP] (1619,5): error TS2300: Duplicate identifier 'export='.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 10
- Comments: 31 (15 by maintainers)
Commits related to this issue
- Enabled skipLibCheck in tsconfig.json to circumvent Jest type errors Some related info: https://github.com/cypress-io/cypress/issues/1087#issuecomment-353370449 — committed to jpoppe/openapi-client by jpoppe 6 years ago
- Add workaround for bug https://github.com/cypress-io/cypress/issues/1087 — committed to waldur/waldur-homeport by AmbientLighter 6 years ago
- fix(types): work around conflicting cypress and jest types cypress ships mocha, which declares e.g. `describe` on a top level, as does jest. unfortunately their types are incompatible, forcing us to ... — committed to dcos/dcos-ui by pierrebeitz 5 years ago
- fix(types): add skipLibCheck to work around conflicting cypress and jest types we now trust .d.ts-files blindly, mostly because the mocha shipped by cypress conflicts with jest otherwise. an alternat... — committed to dcos/dcos-ui by pierrebeitz 5 years ago
- fix(types): add skipLibCheck bc conflicting cypress- and jest-types we now trust .d.ts-files blindly, mostly because the mocha shipped by cypress conflicts with jest otherwise. an alternative approac... — committed to dcos/dcos-ui by pierrebeitz 5 years ago
- fix(types): add skipLibCheck bc conflicting cypress- and jest-types we now trust .d.ts-files blindly, mostly because the mocha shipped by cypress conflicts with jest otherwise. an alternative approac... — committed to dcos/dcos-ui by pierrebeitz 5 years ago
- fix(types): add skipLibCheck bc conflicting cypress- and jest-types we now trust .d.ts-files blindly, mostly because the mocha shipped by cypress conflicts with jest otherwise. an alternative approac... — committed to dcos/dcos-ui by pierrebeitz 5 years ago
- fix(types): add skipLibCheck bc conflicting cypress- and jest-types we now trust .d.ts-files blindly, mostly because the mocha shipped by cypress conflicts with jest otherwise. an alternative approac... — committed to dcos/dcos-ui by pierrebeitz 5 years ago
- add stripe stuff, including passing jest tests and a successful deployment of a firebase cloud function. Had to add skipLibCheck : true to main tsconfig.ts file for success per https://github.com/cypr... — committed to Atticus29/dataJitsu by Atticus29 5 years ago
Confirmed, it was skipLibCheck I added to get around Jest “Set” problem. Ok, so the recommended solution is ultimately
in the root
tsconfig.jsonfile. Not ideal, but it is hard or impossible to make globals work without clashing.why not make
@types/mochaa peer dependency or optional dependency? I have cypress and jest, and a third tool that has nothing to do with either but is using typescript and it’s picking up on the fact that both are installed.None of the solutions which @Vheissu described were working for me, including their last one. The thing that finally got my project working was adding the include tag:
Not sure why none of the other solutions didn’t work for my project, but just thought I would put this here in case somebody else is having this issue as well.
I just got bitten by this…
I have Jest for unit tests written in TypeScript, and I just added in Cypress but have my Cypress tests kept as the default
.jsmy Cypress tests work beautifully, however my Jest tests are now throwing errors likeProperty 'toBeCalled' does not exist on type 'Assertion'.Property 'toEqual' does not exist on type 'Assertion'. Did you mean 'equal'?and a few others.I have tried everything listed in this issue as well as other issues and repos:
tsconfig.jsonfilesskipLibCheckto true (it was already true)typesin my roottsconfig.jsonto be an empty array/// <reference types="Jest" />to the top of my Jest unit testsincludepaths in my roottsconfig.jsonfileThis is a serious issue and it should be reopened @bahmutov
I was able to fix this by doing the following:
In the root you need to create an empty file called
noop.d.tsmentioned in the path above.Super not ideal, but it got me out of a bind short of not using Cypress at all.
Yet another fix, in case someone finds it useful.
I had confict issues with global types. In my case, I was using Angular (v8), which uses
jasmineandkarmaby default, and I added Cypress (v3.6.1).The issue I had was that after installing Cypress, suddenly my Angular specs started complaining because the
expect()type was coming fromchaiinstead ofjasmine.My (hacky) fix was adding this to the main
tsconfig.jsonfile.I did that because you can only
excludewhat has beenincluded.The issue specifically reappeared after latest update to 4.4.1, is there any change that could have affected this? Will try to produce a repro later. Edit: adding
cypressto excluded folders of my globaltsconfigdid the job. But I am not sure why the issue appeared suddenly, there might be an underlying root cause.I have done some experimentation. Here’s what works:
./tsconfig.json./cypress/tsconfig.jsonCypress seems to handle TypeScript just fine without any extra configuration, so the
cypress/tsconfig.jsonis just to keep the editor happy.We are trying to move all
@typesdefinitions into our bundle, so this work is done in https://github.com/cypress-io/cypress/pull/3425It’s sad that this has no universal fix. Tried all the solutions and none worked
This PR will likely fix these issues without needing the workaround https://github.com/cypress-io/cypress/pull/3425
I think the issue is deeper than just conflicts in
chai. The conflict can happen for any type definition declared on global (jest,mocha,chai,jquery, etc). The issue is Cypress is basically its own environment. The good news is Cypress is meant to run in its own subdirectory of a project (it could be in its own repo, but I like it in the source project).I think the solution for TypeScript users should be to have their own
tsconfig.jsonwhere you can set up a more limited environment. I’m experimenting with that right now. It would be good to put these best practices in the TypeScript example with some explanation as to why. If people use that example as a starting point, it will reduce the likelihood that someone will run into this issue.Proposal: The idea is you have a
tsconfig.jsonin thecypressfolder. Thistsconfig.jsonextends from thetsconfig.jsonin the base of the project. It has abaseUrlthat references the project base path (most likely../node_modules). It has atypeRootsthat picks../node_modules/cypress/typesbefore../node_modules/@typesto ensure Cypress gets resolved first. Maybeincludemanually references../node_modules/cypress/typesas well.I’ll try this out and post back.
Why is this issue closed? Doesn’t feel like it’s solved, at all.
Hi, can we make this a bit more alive? I see this as big issue and moving mocha to peer dependency as the best option - i just spent non-trivial time now to define the types in app
tsconfig, this can introduce issues in the future as project (and number of people) grows.skipLibCheckis really brutal option -> if you have bigger projects, this really becomes handy.@kirjai You are right that separating
typesputs some burden on developers. The upside istypesonly needs to be specified for files with global declarations. Also a way around this when using bundlers is to simplyimport 'jest'in a spec helper (that will grab the type definitions regardless oftypessetting intsconfig.json- easier to maintain possibly?)From https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types
I don’t think there is an ideal solution. Cypress uses globals for convenience (no
importstatements are required) and projects can also have globals for various reasons (legacy or otherwise). The possibility of global conflicts is the reason why modules was introduced. I think documentation can note possible solutions with tradeoffs. Disabling lib checks can have issues as well:I think skipping lib checks is the easiest solution with the least overhead. It also solves issues like
chai-enzymeandchai-jqueryhave conflicting overrides forchai(https://github.com/DefinitelyTyped/DefinitelyTyped/pull/22173). I hesitate to recommend something that disables type checks, but I understand this use-case. I tend to fix the root issue, but that can take a lot more time and effort.Adding
"types": ["@types/jest", "cypress"]in this specific order fixed the problem for me.@Vheissu You did a lot of things that should work. Be sure to restart your TS language service after each change as it is sometimes not picked up. In VS Code it is “Restart TS” something in the command palette.
This is a hard problem and I’m glad the Cypress team is working to fix the issue and reduce the developer setup burden.