【问题标题】:Adding to module.exports on cypress/plugins/index.js file Cypress添加到 cypress/plugins/index.js 文件上的 module.exports 赛普拉斯
【发布时间】:2021-12-20 22:00:56
【问题描述】:

我正在努力添加第二个 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

【问题讨论】:

    标签: cypress


    【解决方案1】:

    我相信您可以将配置从您的函数传递到您的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() 函数返回一个类似于原始config 的JSON 对象,而cypress-grep 插件接受一个JSON 对象,您可能只需添加来自getConfigurationByFile 的解析JSON,而不是提供的标准JSON config.

    【讨论】:

    • 我尝试了上面的代码,但没有运气。不幸的是,它没有从插件文件中加载“cypress-grep”模块
    • 我真的没有任何其他建议——也许在获取配置文件和尝试cypress-grep 插件之间添加一些登录,然后再添加。我对那个插件不是很熟悉,但是文档看起来只是包含一个config 对象。
    • 差不多了 - 但改用return fs.readJsonSync(pathToConfigFile),否则它是一个Promise,赛普拉斯在从任务返回时解析它,但对cypress-grep参数没有好处。
    • getConfigurationByFile函数中?
    【解决方案2】:

    如果您的插件设置正确,则可能是通过命令行传递的环境变量。 Here 是使用 cypress-grep 和配置文件的示例 repo。

    【讨论】:

    • 嗯,我测试了 repo 中的示例,grepFilterSpecs 不起作用。它仍然会循环遍历所有规范,而不是跳过它们。
    • 我现在检查了。我跑了npm run cy:run:dev,只有标记为@dev的测试跑了。 @qa@prod 分别是相同的。
    • 嘿,这是正确的。只有标记为 @dev 的测试会运行,但不会从运行器中跳过其余规范,并将包含在循环中。例如,如果您有 100 个规范并且您只需要运行一个标记规范测试,包括 grepFilterSpecs=true 不会跳过它们,相反,运行器将遍历所有规范,直到它到达标记规范以对其运行测试.这很耗时。如果您尝试在不使用 env 配置文件的情况下运行 grepFilterSpecs=true,它将按预期工作,它将仅运行包含标签的选定规范,并将跳过其余规范。
    • 啊,我明白你的意思了。我会确保在 repo 中注明。
    猜你喜欢
    • 1970-01-01
    • 2020-12-23
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 2021-04-26
    • 2023-02-14
    相关资源
    最近更新 更多