I have a multiple specs files in Cypress and each one imports a file named "helper.cy". helper.cy contains a Cypress.Commands.overwriteQuery overwriting the "contains" query.

When I try to run all the spec files together (there are 8), the contains overwrite calls itself 8 times. Presumably this is because the way that Cypress merges the spec files together, it ends up importing helper.cy 8 times.

Has anyone else experienced this and does anyone know of a workaround?

1

There are 1 best solutions below

0
Sandra de La Cruz On

Generally helper files are classed as "Support" files and are placed in (or imported into) the index or root file of the folder cypress/support. By default that file is cypress/support/e2e.js but you can configure it to be another file in cypress.config.js.

See this page of the documentation Writing and Organizing Tests

Support file

To include code before your test files, set the supportFile path.

This file runs before every single spec file.
We do this purely as a convenience mechanism so you don't have to import this file.