'Cypress causes: Property 'each' does not exist on type 'TestFunction'

We have introduced cypress 9.3.1 in our project for e2e test. Now we face the problem that our existing jest test don't compile in the CI.

The following error occures for all parameterized tests:

Property 'each' does not exist on type 'TestFunction'.
       it.each<TestCase>([

Question: How to fix it?


What we tried and did't work:

  • Adding import { it } from '@jest/globals' to every test. We where able to fix a similar problem (Property 'toBeTruthy' does not exist on type 'Assertion') by adding import { expect } from '@jest/globals' to every test. See: https://stackoverflow.com/a/65153905

  • Adding a project wide exclusion for cypress globals, by adding "exclude": ["cypress/global.d.ts"] to the tsconfig.spec.json



Solution 1:[1]

We are using the tsconfig.json as described here (see @JRJurman answer) but that doesn't worked in our project.

Now we are using the cypress-each plugin, developed by one of the cypress contributors.

npm i -D cypress-each

usage:

import 'cypress-each'

// create a separate test for each selector
const selectors = ['header', 'footer', '.new-todo']
it.each(selectors)('element %s is visible', (selector) => {
  cy.visit('/')
  cy.get(selector).should('be.visible')
})

Solution 2:[2]

Just ran into this problem as well, it turned out a conflicting type definition from mocha was included in our tsconfig.json. Make sure that you aren't loading Mocha's type definitions.

Edit: what we ended up needing to do was creating a separate tsconfig.json for our cypress tests, as described here: https://docs.cypress.io/guides/tooling/typescript-support#Configure-tsconfig-json

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chriss
Solution 2