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



我正在努力添加第二个模块。导出cypry/plugin/index.js

我当前的柏树/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);
};

我想将以下内容添加到cypr/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对象,因此您可能只需添加getConfigurationByFile中解析的JSON,而不是config提供的标准JSON。

如果插件设置正确,那么它可能是通过命令行传递的环境变量。下面是一个使用cypry-grep和配置文件的示例repo。

相关内容

最新更新