gpt4 book ai didi

javascript - 如果使用 Cypress 的 CLI 命令中未声明参数之一,会引发异常吗?

转载 作者:行者123 更新时间:2023-11-28 03:06:30 24 4
gpt4 key购买 nike

实际结果是:我刚刚运行,等了半个小时也没有任何反应。 Actual result

预期结果是:如果缺少特定参数,则抛出特定异常。

我目前没有任何错误消息。如果我尝试使用例如 PAGE_TITLE 等于 undefined 来运行它,则不会发生任何情况。

尝试过的场景:我将位于 package.json、docker:test-data-creation:add-widgets 中的所有常量放入名为 requiredFields 的常量数组中,然后从 validateParameters 函数调用 requiredFields,该函数因未知原因而不起作用。

显示一些代码:这是我使用特定参数调用 --spec 的地方

    "docker:test-data-creation:add-widgets": "XSOCK=/tmp/.X11-unix && XAUTH=/tmp/.docker.xauth && xauth nlist :0 | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge - && chmod 755 $XAUTH && docker run --rm -it -v $XSOCK:$XSOCK -v $XAUTH:$XAUTH -e XAUTHORITY=/tmp/.docker.xauth -e DISPLAY=$DISPLAY --network=sity_static-network -v $PWD:/test -w /test cypress/included:3.8.1 --browser chrome --env baseUrl=http://symfony.local,PAGE_TITLE='Slider Swiper Widget Test',MAX_WIDGETS_IN_PAGE=50,TOTAL_WIDGETS_COUNT=250,WIDGET_TYPE='SliderSwiper',WIDGETS_WITH_ROWS=50 --spec 'cypress/integration/creation/dynamically_add_widgets_spec.js'" 

如果dynamic_add_widgets_spec.js部分

   * Name of the page where you like to save this widgets
*/
const PAGE_TITLE = Cypress.env('PAGE_TITLE');

/**
* Maximum widgets in the page
*/
const MAX_WIDGETS_IN_PAGE = Cypress.env('MAX_WIDGETS_IN_PAGE');

/**
* Number of total widgets to be added to the page
*/
const TOTAL_WIDGETS_COUNT = Cypress.env('TOTAL_WIDGETS_COUNT');

/**
* Just change the type of the widget to select your needed widget
*/
const WIDGET_TYPE = Cypress.env('WIDGET_TYPE');

/**
* This variable is needed because we want to build some widgets with rows and some widgets don't need rows. The number below is how many widgets one need with rows.
*/
const WIDGETS_WITH_ROWS = Cypress.env('WIDGETS_WITH_ROWS');

/**
* This is a variable that is used to count needed widgets with rows. This variable should always stay 0.
*/
let ROWS_COUNTER = 0;

/**
* This constant is used to determine if all required fields are declared in the cli command
*/
const requiredFields = [
PAGE_TITLE,
MAX_WIDGETS_IN_PAGE,
TOTAL_WIDGETS_COUNT,
WIDGET_TYPE,
WIDGETS_WITH_ROWS
];

这是我调用它的地方

context("Dynamically create widgets", () => {
validateParameters(requiredFields);
createWidgets(WIDGET_TYPE, TOTAL_WIDGETS_COUNT, MAX_WIDGETS_IN_PAGE, WIDGETS_WITH_ROWS);
});

validateParameters 函数 -->

  function validateParameters(requiredFields) {
requiredFields.forEach((field) => {
if (Cypress.env(field) !== undefined) {
return;
}
throw {message: `Field ${field} is required. Please provide it using --env ${field}. Please look at README.md for an example.`};
})
}

附注我知道这个 forEach 无法工作,因为如果缺少特定参数,我需要一条特定消息,但我试图获取此消息,然后在缺少特定参数时获取特定消息。

最佳答案

问题是在 validateParameters 函数中您传递了常量数组。每个常量都有值,例如 PAGE_TITLE = Cypress.env('PAGE_TITLE');所以它将是 .env 中定义的任何内容。这可能是字符串、数字、未定义或其他内容。

要解决问题,您需要检查传递的字段是否未定义。

function validateParameters(requiredFields) {
requiredFields.forEach((field) => {
if (field !== undefined) {
return;
}
throw {message: `Field ${field} is required. Please provide it using --env ${field}. Please look at README.md for an example.`};
})
}

关于javascript - 如果使用 Cypress 的 CLI 命令中未声明参数之一,会引发异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60563552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com