Cypress : how to group tests like "Smoke", "Sanity" and "regression"

I am using this plugin https://github.com/cypress-io/cypress-grep to tag tests as smoke, stable, etc. You can check out its homepage for usage and use cases. I recommend to go with this option since it's simple.

Or if you prefer, you can create your own plugin to filter tests based on the test case name since Cypress also provides hooks (borrowed from Mocha).

before(() => {
  // root-level hook
  // runs once before all tests
})

beforeEach(() => {
  // root-level hook
  // runs before every test block
})

afterEach(() => {
  // runs after each test block
})

after(() => {
  // runs once all tests are done
})

describe('Hooks', () => {
  before(() => {
    // runs once before all tests in the block
  })

  beforeEach(() => {
    // runs before each test in the block
  })

  afterEach(() => {
    // runs after each test in the block
  })

  after(() => {
    // runs once after all tests in the block
  })
})

Since we want this implementation applied for all specs, the right place to put it in is /cypress/support/index.js

beforeEach(function() {
    // get tags passed from command
    const tags = Cypress.env('tagName').split(",");

    // exit if no tag or filter defined - we have nothing to do here
    if ( !tags ) return;

    // get current test's title (which also contains tag/s)
    const testName = Cypress.mocha.getRunner().suite.ctx.currentTest.title

    // check if current test contains at least 1 targetted tag
    for (let i = 0; i < tags.length; i++){
        if ( testName.includes(tags[i]) ) return;
    }

    // skip current test run if test doesn't contain targetted tag/s
    this.skip();
})

In your spec files, you can add tag/s for your tests

describe('this is a test for tests selection', () => {
    it('test #1 [@smoke, @functional, @e2e]', () => {
        console.log("this is a test #1")
    })

    it('test #2[@e2e]', () => {
        console.log("this is a test #2")
    })
})

Lastly, in your command, you specify which tag/s to be used for filtering your tests

cypress_tagName="@functional, @smoke" npm run cy:open