DEV Community

Cover image for A Simple Approach to Using Tags in Cypress
Paul Astorga for Cypress

Posted on

4

A Simple Approach to Using Tags in Cypress

When starting with Cypress, it’s common to focus on writing a lot of test cases to enhance coverage and improve metrics. While this enthusiasm is commendable, it can lead to challenges in terms of maintainability down the line.

As the number of test cases increases, you may notice that your pipeline takes significantly longer to execute. So, how can we address this issue? The answer lies in utilizing tags!

With Cypress, we can categorize our test cases into different suites, typically separating them into smoke and regression suites. There are various methods to implement these tags, but in this blog, I will demonstrate a straightforward approach that I regularly apply.

While there are several packages available to assist with this functionality, I will focus on my preferred package: @cypress/grep. The documentation for this package outlines multiple ways to utilize tags, so let's summarize the process with a simple step-by-step guide.

  • I usually install this package as a dev dependency so let’s run this code:
# using NPM
npm i -D @cypress/grep
# using Yarn
yarn add -D @cypress/grep
Enter fullscreen mode Exit fullscreen mode
  • Let’s register this package in our support file
// cypress/support/e2e.js
// load and register the grep feature using "require" function
// https://github.com/cypress-io/cypress/tree/develop/npm/grep
const registerCypressGrep = require('@cypress/grep')
registerCypressGrep()
Enter fullscreen mode Exit fullscreen mode

Note: If you are using ts instead of js, the documentation provides the examples for other configurations.

  • Let’s add our package in the configuration file
// cypress.config.js
{
  e2e: {
    setupNodeEvents(on, config) {
      require('@cypress/grep/src/plugin')(config);
      return config;
    },
  }
}
Enter fullscreen mode Exit fullscreen mode
  • Let’s enable the grepFilterSpecs in out configuration file as well
{
  "env": {
    "grepFilterSpecs": true
  }
}
Enter fullscreen mode Exit fullscreen mode

Note: If you don’t want to enable this parameter in the configuration file, you can enable it via command.

  • Let’s add our tags to our test cases, so this will depends on which tags you want to add, personally I use smoke and regression tags in my tests, but you can use different ones.
it('verify log in', { tags: ['@smoke', '@regression'] }, () => {
  cy.visit(“/login”)
  cy.get(“#username”).type(“user”)
  cy.get(“#password”).type(“password”)
  cy.get(“#submit-button”).click()
})
Enter fullscreen mode Exit fullscreen mode

Note: The tags as an object includes an array so you can add as many tags as you want.

  • Let’s run our test cases
npx cypress -run –env grepTags=@smoke
Enter fullscreen mode Exit fullscreen mode

Note: If you need to run more than one tag just use the + sign. Ex:

--env grepTags=@smoke+@fast
Enter fullscreen mode Exit fullscreen mode
  • Additionally if you want to update your yaml to run only the smoke you can use something like this:
name: Cypress Tests

on:
  push:
    branches:
    - main
  pull_request:
    branches:
    - main

jobs:
  cypress-run:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
        uses: actions/checkout@v3

    - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
        node-version: '16'

    - name: Install dependencies
        run: npm install

    - name: Run Cypress tests with grep
        run: npx cypress run --env grepTags=@smoke
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay