最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I get the Cypress API to run `.feature` files as well as `spec` files? - Stack Overflow

programmeradmin1浏览0评论

I've got a mixture of Cucumber .feature files and normal Cypress .spec.js files.

Normally, they run together fine as I have included:

specPattern: ["cypress/e2e/**/*.spec.js", "cypress/e2e/*.feature"],

in my cypress.config.js file.

However, I want to use Cypress's Module API, eg:

const mySpecs = [
    `homePage.spec.js`,
    `aboutPage.spec.js`
];

cypress.run({
    browser: 'chrome',
    spec: mySpecs,
});

The above works, but not if I also have a .feature file.

const mySpecs = [
    `homePage.spec.js`,
    `aboutPage.spec.js`,
    `contactPage.feature`
];

I've tied modifying the above, eg:

cypress.run({
    browser: 'chrome',
    spec: mySpecs,
    config: {
        specPattern: ["cypress/e2e/**/*.spec.js", "cypress/e2e/*.feature"],
    }
});

and

cypress.run({
    browser: 'chrome',
    specPattern: ["cypress/e2e/**/*.spec.js", "cypress/e2e/*.feature"],
    spec: mySpecs
});

Does anyone know if this is possible?

I've got a mixture of Cucumber .feature files and normal Cypress .spec.js files.

Normally, they run together fine as I have included:

specPattern: ["cypress/e2e/**/*.spec.js", "cypress/e2e/*.feature"],

in my cypress.config.js file.

However, I want to use Cypress's Module API, eg:

const mySpecs = [
    `homePage.spec.js`,
    `aboutPage.spec.js`
];

cypress.run({
    browser: 'chrome',
    spec: mySpecs,
});

The above works, but not if I also have a .feature file.

const mySpecs = [
    `homePage.spec.js`,
    `aboutPage.spec.js`,
    `contactPage.feature`
];

I've tied modifying the above, eg:

cypress.run({
    browser: 'chrome',
    spec: mySpecs,
    config: {
        specPattern: ["cypress/e2e/**/*.spec.js", "cypress/e2e/*.feature"],
    }
});

and

cypress.run({
    browser: 'chrome',
    specPattern: ["cypress/e2e/**/*.spec.js", "cypress/e2e/*.feature"],
    spec: mySpecs
});

Does anyone know if this is possible?

Share Improve this question edited Mar 28 at 13:03 Leta Waldergrave 8964 silver badges13 bronze badges asked Mar 28 at 2:51 MeltingDogMeltingDog 15.6k52 gold badges178 silver badges322 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

I just ran a clean system including typescript and badeball/cypress-cucumber-preprocessor.

I added Badeball's example duckduckgo.feature plus it's step file in the folder cypress/e2e/features.

This is the cypress.config.ts, pretty much as per Example setup except the specPattern setting included feature and non-feature file patterns (only used .js spec patterns not .ts but you should be able to specify those as well).

import { defineConfig } from "cypress";
import createBundler from "@bahmutov/cypress-esbuild-preprocessor";
import { addCucumberPreprocessorPlugin } from "@badeball/cypress-cucumber-preprocessor";
import { createEsbuildPlugin } from "@badeball/cypress-cucumber-preprocessor/esbuild";

export default defineConfig({
  e2e: {
    specPattern: ['**/*.e2e.js', '**/*.spec.js', '**/*.feature'],
    async setupNodeEvents(on, config) {
      await addCucumberPreprocessorPlugin(on, config);
      on('file:preprocessor', createBundler({
          plugins: [createEsbuildPlugin(config)],
        })
      )
      return config
    },
  },
})

In the Module API script to run the tests, I used similar to your script but I specified the full path from the project root, i.e included cypress/e2e.

const cypress = require('cypress')

const mySpecs = [
  `./cypress/e2e/home-page-test.spec.js`,
  `./cypress/e2e/about-page-test.spec.js`,
  `./cypress/e2e/features/duckduckgo.feature`
];

cypress
  .run({
    browser: 'chrome',
    spec: mySpecs,
  })

For me the full path to specs and feature was required, otherwise Cypress reported it did not match any specs (or features).

My understanding is the Module API runner uses cypress.config.ts as the base config, then adds anything you put in the script as an override, similar to the way the Cypress CLI lets you override browser, spec, etc.

So if you have specified specPattern in cypress.config.ts and all the specs and features can be run via the GUI (npm cypress open), then the Module API script just needs to specify the specs and features (and in this case browser, presuming you don't want the default Electron browser).

发布评论

评论列表(0)

  1. 暂无评论