Running a Subset of Cypress Tests using Grep and Test Tags
Combining test grepping using Cypress Module API and test tagging to conveniently filter and run just a subset of tests.
Test automation is rarely static because periodically it needs to be maintained. Chances are you need to add additional tests to increase coverage or tests to cover specific user flow. Or maybe you need to separate tests for the web application, tests for the CMS of your application, and tests for enabler — which is not quite a test, but more of a process automation as you might be doing repetitive tasks, such as downloading report periodically. Another good example of test enabler (or RPA) is where you would do some kind of website scrapping and automating the process using GitHub.
So, here is my journey of finding the perfect recipe of filtering and running a subset of Cypress tests (that is specific to folders and specific tests).
The Beginning
A common developer or beginner like me would certainly use Google to search for the available solutions of filtering Cypress tests. Depending on the search term, you should certainly come across these great results:
- How I Filtered Tests with Cypress.io by Jonathan Thompson.
- Using Tags to Filter Your Cypress Tests by Marie Drake.
- How to Apply Tags to Your Cypress Tests Like Smoke, E2E by TestersDock.
The first and second article uses custom function TestFilters
, while the third article uses cypress-select-tests
, a plugin written by Gleb Bahmutov.
Unfortunately none of the suggested solutions are applicable enough for my case. Using custom function may be good if you don’t mind wrapping the describe
or it
inside TestFilters
, and don’t mind writing it all over the specs file. Using cypress-select-tests
also not applicable since the tags are placed inside the test title or filename, not to mention that the plugin itself is deprecated.
Test Grepping to The Rescue
There is a really good article by Filip Hric about test grepping, which I based my solution on. It’s simple enough and easy to understand and followed, and I think it’s helpful enough for most cases. Basically you supply the test folders to be run.
But, wait! I also need to filter out specific tests inside a spec file, for example: how to just run all the positive tests in a folder, combined with some other positive tests in different folders?
Enter cypress-grep
Reading and digging about cypress-select-tests
from TestersDock solution, actually will lead to cypress-grep. A super handy plugin from Cypress guru, Gleb. You can run specific tests using part of the test title and by using explicit tags, which is perfectly suites my need.
Final Solution
So, how do I combine test grepping solution with cypress-grep? Don’t be confused by the name anyway, since they contain the same word “grep”.
- Follow solution from Filip Hric’s test grepping. Create a new file in root folder
cypress.ts
, and modifypackage.json
to incorporate changes
- Install cypress-grep by executing
npm install --save-dev cypress-grep
. - Add require module inside support file. Note that you should really using
require
instead ofimport
, otherwise the plugin won’t work.
- The readme says that you can optionally load and register from plugin file. I’m adding it anyway.
- Add
cypress-grep
to types in the tsconfig file.
- Finally, add additional parameter for
grepTags
(andgrep
if you need it) with typestring
and default of''
.
An example of the test case would look like.
That’s all from me. I hope my journey of finding the perfect recipe of filtering Cypress tests may come useful to you. If you have any suggestion for improvement, please let me know. Thanks for reading!