Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use the "excludeSpecPattern" in Cypress together with a plugin to exclude from the cypress run one specific test file

I’m using the Cypress version 10.3.0 and I’m trying to exclude a specific test file when I do cypress run.

The general purpose of this is due to the fact I want to be able to run all tests when I open CY UI and exclude the all.cy.js from running when I do cypress run to avoid running twice the same tests.

I tried to follow the documentation about excludeSpecPattern which should allow me not to run the test and I implemented this as follow

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

defineConfig({
  e2e: {
    chromeWebSecurity: false,
    viewportWidth: 1280,
    viewportHeight: 800,
    screenshotsFolder: 'artifacts',
    video: false,
    reporter: 'junit',
    reporterOptions: {
      mochaFile: 'results/test-results-[hash].xml',
    },
    retries: 1,
    defaultCommandTimeout: 60000,

    setupNodeEvents(on, config) {
      if (config.isTextTerminal) {
         // skip the all.cy.js spec in "cypress run" mode
         return {
              excludeSpecPattern: ['cypress/e2e/all.cy.js'],
          }
      }
      return require('./cypress/plugins/index.js')(on, config);
    },
  },

I tried to follow a guide here Link to the guide used

However, the above doesn’t work as the test immediately fails in 1 sec without even run practically

I don’t know how can I do it in my current setup

the plugin called file

const fs = require('fs');
const path = require('path');
const gmail = require('gmail-tester');

function getConfigurationFromFile(file, type) {
  const pathToConfigFile = path.resolve('cypress', 'config', type, `${file}.json`);
  return JSON.parse(fs.readFileSync(pathToConfigFile));
}

// See https://docs.cypress.io/guides/guides/continuous-integration.html#In-Docker
function disableDevShmUsage(browser = {}, launchOptions) {
  if (browser.family === 'chromium' && browser.name !== 'electron') {
    launchOptions.args.push('--disable-dev-shm-usage');
  }
  return launchOptions;
}

/**
 * This function is called when a project is opened or re-opened (e.g. due to the project's config changing)
 *
 * @type {Cypress.PluginConfig}
 * @param on - Used to hook into various events Cypress emits (e.g. `on('before:browser:launch', callback)`)
 * @param config - The resolved Cypress config
 */
module.exports = (on, config) => {
  on('before:browser:launch', disableDevShmUsage);

  on('task', {
    'gmail:check': async (args) => {
      const { from, to, subject } = args;
      let fiveMinutesAgo = new Date();
      // Will rollover to yy:55 if minutes is xx:00
      fiveMinutesAgo.setMinutes(fiveMinutesAgo.getMinutes() - 5);

      const email = await gmail.check_inbox(
        path.resolve(__dirname, 'credentials.json'), // credentials.json is inside plugins/ directory.
        path.resolve(__dirname, 'gmail_token.json'), // gmail_token.json is inside plugins/ directory.
        {
          subject,
          from,
          to,
          after: fiveMinutesAgo,
          include_body: true,
          wait_time_sec: 6, // Poll interval (in seconds)
          max_wait_time_sec: 60, // Maximum poll interval (in seconds). If reached, return null, indicating the completion of the task().
        },
      );

      return email ?? [];
    },
  });

  const env = config.env.ENVIRONMENT || 'development';
  const envConfig = getConfigurationFromFile(env, 'env');

  const scope = config.env.SCOPE || 'full';
  const scopeConfig = getConfigurationFromFile(scope, 'scope');

  return Object.assign(config, envConfig, scopeConfig);
};


I thought maybe I need to include that inside the plugin but still, I have no idea whatever I tried so far with that if statement fails the tests

>Solution :

You would need to merge the result of /cypress/plugins/index.js with your new value.

If not adding the new value, just return the legacyConfig.

defineConfig({
  e2e: {
    chromeWebSecurity: false,
    viewportWidth: 1280,
    viewportHeight: 800,
    screenshotsFolder: 'artifacts',
    video: false,
    reporter: 'junit',
    reporterOptions: {
      mochaFile: 'results/test-results-[hash].xml',
    },
    retries: 1,
    defaultCommandTimeout: 60000,

    setupNodeEvents(on, config) {

      const legacyConfig = require('./cypress/plugins/index.js')(on, config);

      if (config.isTextTerminal) {
         // skip the all.cy.js spec in "cypress run" mode
         return {
           ...legacyConfig,
           excludeSpecPattern: ['cypress/e2e/all.cy.js'],
         }
      }
      return legacyConfig;
    },
  },
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading