Search code examples
javascriptnode.jsautomated-testscucumbercucumberjs

Run cucumber by tag or run all tests


I am having hard time with running cucumber. I want to run either all scenarios if tags are not passed as CLI argument or certain scenarios according to passed tags as CLI argument. Here is my config file:

module.exports = {
  default: {
    tags: '@run or ""',
    formatOptions: {
      snippetInterface: 'async-await'
    },
    paths: [
      'src/features/'
    ],
    dryRun: false,
    require: [
      'src/step_definitions/**/*.ts',
      'hooks.ts'
    ],
    requireModule: [
      'ts-node/register'
    ],
    format: [
      'json:test-results/json-report.json',
      'progress'
    ],
    parallel: process.env.CI ? 10 : 1
  }
}

here is the script to run tests:

"test": "cucumber-js --config=configs/cucumber.cjs || true"

If I pass @run tag scenarios with that tag run or no tests run. If I remove tag property from the config no matter what tag I pass all tests are being run. For example, I have this scenario:

@api
Scenario: Name of the scenario
// Steps

config.js:

module.exports = {
  default: {
    formatOptions: {
      snippetInterface: 'async-await'
    },
    paths: [
      'src/features/'
    ],
    dryRun: false,
    require: [
      'src/step_definitions/**/*.ts',
      'hooks.ts'
    ],
    requireModule: [
      'ts-node/register'
    ],
    format: [
      'json:test-results/json-report.json',
      'progress'
    ],
    parallel: process.env.CI ? 10 : 1
  }
}

Command in terminal:

npm run test --tags="@api"

This command is running all tests. How can I make it to run tests with specific tag if that tag is passed CLI argument, or run all tests?

I'd appreciate your help.


Solution

  • After playing around the code I found the solution:
    updated config:

    module.exports = {
      default: {
    // removed tags property
        formatOptions: {
          snippetInterface: 'async-await'
        },
        paths: [
          'src/features/'
        ],
        dryRun: false,
        require: [
          'src/step_definitions/**/*.ts',
          'hooks.ts'
        ],
        requireModule: [
          'ts-node/register'
        ],
        format: [
          'json:test-results/json-report.json',
          'progress'
        ],
        parallel: process.env.CI ? 10 : 1
      }
    }
    

    package.json:
    "scripts": {
      "test": ""cucumber-js --config=configs/cucumber.cjs"
    }
    

    test run command:

    npm run test -- --tags "@TAG_NAME" || true