添加到 cypress/plugins/index.js 文件上的 module.exports Cypress

2024-02-28

我正在努力添加第二个 module.export cypress/plugin/index.js

我当前的 cypress/plugin/index.js 文件如下所示

/// <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);
};

我想将以下内容添加到 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

我相信您可以将配置从您的函数传递到您的require然后返回新的配置。

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;
};

自从你的getConfigurationByFile()函数返回一个像原始对象一样的 JSON 对象config,以及cypress-grep插件接受一个 JSON 对象,您可能只需添加解析后的 JSONgetConfigurationByFile而不是提供的标准config.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

添加到 cypress/plugins/index.js 文件上的 module.exports Cypress 的相关文章

随机推荐