3

I am struggling to add a second module.export cypress/plugin/index.js

My Current cypress/plugin/index.js file look like this

/// <reference types="cypress" />
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

/**
 * @type {Cypress.PluginConfig}
 */
// eslint-disable-next-line no-unused-vars

const { on } = require('events');
const fs = require('fs-extra');
const path = require('path');

function getConfigurationByFile(file) {
  const pathToConfigFile = path.resolve('config', `${file}.json`);

  return fs.readJson(pathToConfigFile);
}

module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config
  const file = config.env.configFile || 'qa';

  return getConfigurationByFile(file);
};

I want to add the following to cypress/plugin/index.js:

require('cypress-grep/src/plugin')(config)
// make sure to return the config object
// as it might have been modified by the plugin
return config
chigolfer
  • 117
  • 3
  • 7

2 Answers2

2

I believe that you can pass in the config from your function to your require and then return that new config.

module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config
  const file = config.env.configFile || 'qa';
  let newConfig = getConfigurationByFile(file);
  
  require('cypress-grep/src/plugin')(newConfig);
  
  return newConfig;
};

Since your getConfigurationByFile() function returns a JSON Object like the original config, and the cypress-grep plugin takes in a JSON Object, you can probably just add the resolved JSON from getConfigurationByFile instead of the standard one provided by config.

agoff
  • 5,818
  • 1
  • 7
  • 20
  • I tried the code above and no luck. Unfortunately it doesn't load the 'cypress-grep' module from the plugin file – chigolfer Dec 21 '21 at 01:50
  • I don't really have any other advice -- maybe add some logging in between getting the configuration file and trying the `cypress-grep` plugin, and then after. I'm not really familiar with that plugin, but the documentation looks like it just takes in a `config` object. – agoff Dec 21 '21 at 15:11
  • That's almost it - but use `return fs.readJsonSync(pathToConfigFile)` instead, otherwise it's a Promise, which Cypress resolves when returned from the task but is no good for `cypress-grep` parameter. – Fody Jan 14 '22 at 02:57
  • In the `getConfigurationByFile` function? – agoff Jan 14 '22 at 14:16
0

If you have your plugins set up correctly, then it may be the environment variables passed via command line. Here is an example repo of using cypress-grep and configuration files.

jjhelguero
  • 2,281
  • 5
  • 13
  • Hm, I tested the example from the repo and the `grepFilterSpecs` is not working. It will still loop through all the specs instead of skipping them. – Sašo Krajnc Feb 23 '22 at 06:50
  • I checked now. I ran `npm run cy:run:dev` and only the tests tagged `@dev` ran. The same, respectively, is for `@qa` and `@prod`. – jjhelguero Feb 23 '22 at 17:33
  • 1
    Hey, this is correct. Only the tests tagged @dev will run, but the rest specs won't be skipped from the runner and will be included in the loop. For example, if you have 100 specs and you need to run only one tagged spec test, including `grepFilterSpecs=true` won't skip them, instead, the runner will go through all the specs until it reaches the tagged spec to run tests on it. This is time-consuming. If you try to run the `grepFilterSpecs=true` without using the env config files, it will work as expected, it will run only the selected spec that includes the tag and will skip the rest specs. – Sašo Krajnc Feb 23 '22 at 17:50
  • Ah, I see what you mean. I'll make sure to note that in the repo. – jjhelguero Feb 23 '22 at 19:04