Home > Software design >  How to use the "excludeSpecPattern" in Cypress together with a plugin to exclude from the
How to use the "excludeSpecPattern" in Cypress together with a plugin to exclude from the

Time:08-06

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

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

CodePudding user response:

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;
    },
  },
  • Related