question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New Cypress 10 global TypeScript type conflicts with Jest `expect`

See original GitHub issue

Current behavior

The following new global TypeScript type for expect in node_modules/cypress/types/cypress-expect.d.ts causes conflicts with Jest expect types, if a project has both Jest and Cypress:

https://github.com/cypress-io/cypress/blob/a0a64cc851c8232b94eb0e907f220e08260bda20/cli/types/cypress-expect.d.ts#L2

Errors:

Property 'toBe' does not exist on type 'Assertion'.
Screen Shot 2022-06-02 at 21 19 56

When looking through the docs, there is a guide here:

Cypress Types Conflict with Jest

However, this guide is based on the fact that Cypress types were not global before (but they are now).

Desired behavior

No response

Test code to reproduce

Jest test code (filename __tests__/colors.test.ts):

import { findWhiteContrastingColor } from '../colors';

test('findWhiteContrastingColor finds colors contrasting with white', () => {
  expect(findWhiteContrastingColor(['#ffffff', '#000000', '#e1e1e1'])).toBe(
    '#000000',
  );
});

Cypress Version

10.0.1

Other

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:53
  • Comments:36 (12 by maintainers)

github_iconTop GitHub Comments

181reactions
ZachJW34commented, Jun 7, 2022

@karlhorky thanks for posting, looks like you’re not the only one having issues with Cypress + Jest type collisions.

I was able to reproduce this with the repos linked @karlhorky @mrdrogdrog @wKovacs64. When I checked out the dep update to v10, all of the Jest tests started complaining.

We didn’t change our globals very much for v10. For all the cases I reproduced, it was fixed by adding ./cypress.config.ts to the tsconfig.exclude property. Since the cypress.config.ts is being included in the type checking, it is loading the cypress types which is polluting your jest tests.

20reactions
gustavodamaziocommented, Jul 8, 2022

./tsconfig.json, include types and exclude properties in config file.

{
  "compilerOptions": {
    ...
    "types": ["jest", "node", "@types/testing-library__jest-dom"]
  },
  "exclude": ["node_modules", "cypress", "./cypress.config.ts"]
}

cypress/tsconfig.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "noEmit": true,
    "types": ["cypress"]
  },
  "exclude": ["node_modules"]
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress causing type errors in jest assertions - Stack Overflow
I ran into this problem yesterday. It seems that what you are saying is correct, cypress and jest both declares types for expect...
Read more >
Cypress and Jest `expect` functions conflict in IDEA, tests ...
Cypress and Jest `expect` functions conflict in IDEA, tests execute fine on the command line. This problem has a bit of relevance to...
Read more >
TypeScript - Cypress Documentation
If you are using both Jest and Cypress in the same project, the TypeScript types registered globally by the two test runners can...
Read more >
How to Avoid Using Global Cypress Variables - Gleb Bahmutov
Both Cypress and Jest provide global variables like describe and expect for your tests to use. The global variables do not clash at...
Read more >
Cypress - TypeScript Deep Dive - Gitbook
It is best to keep the e2e tsconfig.json and node_modules in this special e2e folder to prevent global type definition conflicts.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found