Cypress Add multiple function/event on plugins/index.js on file preprocessor

How can I add multiple events on file:preprocessor?
I’m struggling to have the webpack plugin and cypress-select-tests plugin on file:prep

I tried below but it will just process the last one.

const cypressTypeScriptPreprocessor = require('./cy-ts-preprocessor');
const selectTestsWithGrep = require('cypress-select-tests/grep')
module.exports = (on, config) => {

on('file:preprocessor', cypressTypeScriptPreprocessor);
on('file:preprocessor', selectTestsWithGrep(config));

Also, did like below and still to no avail.

const cypressTypeScriptPreprocessor = require('./cy-ts-preprocessor');
const selectTestsWithGrep = require('cypress-select-tests/grep')
module.exports = (on, config) => {
on('file:preprocessor', { cypressTypeScriptPreprocessor, , selectTestsWithGrep(config)});

Any inputs are much appreciated.
Thanks.

19 thoughts on “Cypress Add multiple function/event on plugins/index.js on file preprocessor”

  1. The event handler hook is essentially of the pattern on(eventName, callbackFunction).

    You want one handler with two functions being called, so my guess is something along the lines of

    module.exports = (on, config) => {
    
      on('file:preprocessor', () => {
        cypressTypeScriptPreprocessor();
        selectTestsWithGrep(config);
      });
    

    I think you need to pass the file through to both preprocessors and return it to the hook. The result of the preprocessor is a promise, so handle 1st PP (cypressTypeScriptPreprocessor) result with a .then().

    Also return the config.

    module.exports = (on, config) => {
    
      on('file:preprocessor', (file) => {
        return cypressTypeScriptPreprocessor(file)
          .then(file => {                              //1st PP returns a promise
            return selectTestsWithGrep(config)(file);  //2nd PP should return file to hook
          });
      });
    
      return config;
    }
    

    Shout out if there’s any problems, I can set up a system and help debug.


    For reference Support for multiple preprocessors #5832 still open.

    It has an example of the extended syntax with explicit file parameter and returning the file

    on('file:preprocessor', file => {
      return file.filePath.includes('.feature')
        ? cucumber()(file)
        : wp({ webpackOptions: webpackConfig })(file)
    })
    
    Reply

Leave a Comment