cypress: mocha type definitions conflict with @types/jest
Current behavior:
I’m running into an issue where the types defined for mocha, located at node_modules/cypress/types/mocha results in conflicts with types defined in @types/jest. Below is are the details of one of the various errors that the typescript compiler reports:
"resource": "/Users/hjm/Source/test/cypress-jest-types-issue/node_modules/@types/jest/index.d.ts",
"owner": "typescript",
"code": "2403",
"severity": 8,
"message": "Subsequent variable declarations must have the same type.
Variable 'beforeEach' must be of type 'HookFunction', but here has type 'Lifecycle'.",
"source": "ts",
"startLineNumber": 34,
"startColumn": 13,
"endLineNumber": 34,
"endColumn": 23,
"relatedInformation": [
{
"startLineNumber": 2680,
"startColumn": 13,
"endLineNumber": 2680,
"endColumn": 23,
"message": "'beforeEach' was also declared here.",
"resource": "/Users/hjm/Source/test/cypress-jest-types-issue/node_modules/cypress/types/mocha/index.d.ts"
}
]
}
Below is a full list of the errors that appear in my IDE (VS Code):

Desired behavior:
Types do not conflict.
Test code to reproduce
Here’s a small repo that demonstrates the issue. Errors can be seen by running npm run tsc.
Forked test-tiny repo and was able to reproduce there as well.
Versions
Core packages needed to see issue are:
“@types/jest”: “^25.1.4”, “cypress”: “^4.1.0”, “typescript”: “^3.8.3”
Current Workaround
Using approach described in this comment from #1319 .
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 30
- Comments: 25 (9 by maintainers)
There’s a PR open to fix this here: https://github.com/cypress-io/cypress/pull/7194
Also running into this with the latest versions of Cypress and Jest. I’ve tried every workaround I’ve found in all of these issues/threads and nothing works. We’re currently on slightly older versions of Cypress and we have to resort to a custom module and custom namespace with copy-pasted types just to get by. Upgrading to the latest Cypress packages breaks this horribly.
Having Cypress and Jest in the same repo seems like a pretty common situation to me, so I’m surprised and annoyed that this is still an issue.
I am also running into this issue with Cypress 4.4. Is there a reason this isn’t a peer dependency or is being redeclared and rewritten?
This has just started happening for me with the release of 4.3.0 (I’d skipped 4.2.0 due to #6752): https://travis-ci.com/github/textbook/starter-kit/jobs/309601851
@sainthkh, here’s the simplest possible Cypress setup that shows a difference between 4.2.0 and 4.3.0. Running this (in an empty subdirectory) works:
If you replace
4.2.0with4.3.0(or4.5.0, for that matter), the compilation fails.This setup does not even have any tests (either Jest or Cypress), it looks like simply installing
@types/jestand@types/mochaat the same time is enough to trigger this. It looks like Cypress 4.2.0 did not depend on@types/mocha, but instead bundled own types in./node_modules/cypress/types/mocha/index.d.ts… so at least code that didn’t require Cypress worked just fine.(This is the situation I currently have - my Cypress tests are JS instead of TS, but I cannot update to Cypress 4.3.0 since it breaks other, non-Cypress-related TypeScript code in the same repo…)
The cause is easy to understand.
mochaclaims the ownership of global variables:And
jestclaims the ownership of them, too:Result: clash and error.
But the problem is a bit hard to solve. Because types cannot be imported with a condition like “when @types/jest exist, import these, else, import those”. And there is no official solution. TypeScript team answer is:
I guess
patch-packagecan be a solution. I’m experimenting things.I also ran into this issue on Cypress 4.3.0.
I have a blunt workaround that worked for me, which was to patch the
@types/mochawith patch-package and comment out the conflicting/duplicated declarations:Patch contents
You put this patch into
patches/directory and run apostinstallscript to run patch-package (read their README).The code for this is done in cypress-io/cypress#7352, but has yet to be released. We’ll update this issue and reference the changelog when it’s released.
The code for this is done in cypress-io/cypress#7194, but has yet to be released. We’ll update this issue and reference the changelog when it’s released.
4.2.0 doesn’t have this issue, but it’s happening to me too on 4.3.0.