Pick specs that spend the most time or execute the most Cypress commands on the page.
If your project has a lot of E2E specs, picking the specs to run first is a challenge. You pick specs based on the test ids in the changed source files or using my test tags plugin. But what if you do not know the test ids in the changes? Or the test tags do not map neatly onto your pull request?
What if all you know is the URL of the page most likely to need testing? Which specs test that URL the most?
The [email protected] plugin I am using in the blog post has 2 features that make picking the first specs to run a breeze. Let's take a Swag Store course example app. It have multiple e-commerce store pages and lots of specs. One of the most important pages is the "/inventory" page.
Since this is the first page after the login screen a lot of specs "touch" the "/inventory" page. But which specs really test it? We can think of the testing effort each spec spends on the page. We can either measure the total duration the spec's tests spend on the page or the number of Cypress commands (like cy.get, cy.click, cy.then, etc) executed while on the page. The plugin stores these numbers with each test:
It makes sense that the top 4 specs that run the most Cypress commands on the "/inventory" page all come from the "cypress/e2e/inventory" folder! We probably want to limit ourselves to the specs that have at least 40 Cypress commands against the "/inventory" page. We can use the --cutoff 40 parameter to filter the table:
The cypress-visited-urls plugin also measures the time Cypress spends on each test. It might not be the most appropriate measurement, since it depends on the app's speed and the test design. This test spends 10 seconds on the page, but does not really test it.
1 2
cy.visit('/about') cy.wait(10_000)
The second test spends less time on the About page, but at least it verifies some elements:
Notice the first spec cypress/e2e/checkout/glitch-user.cy.ts - it is not even in the top 7 specs if measured by the number of commands; it's just a spec that waits a lot for the app to react to a "glitch" user! This is why I like the "commands count" metric when deciding which test covers the given URL.