Title: Throw an exception if one of the parameters is not declared in the CLI command using Cypress?
Tags: javascript;command-line-interface;cypress;package.json
Question: Actual results are:
I just got a run and nothing happens even after waiting for a half an hour.
Expected results are:
To throw a specific exception if a specific parameter is missing.
I don't have currently any error messages. If I try to run it with for example PAGE_TITLE equal to undefined nothing happens.
Tried scenarios:
I put all of the constants which are located in package.json, docker:test-data-creation:add-widgets in a constant array called requiredFields and afterwards call requiredFields from a validateParameters function which doesn't work for unknown reason.
Show some code:
This is where I call --spec from with specific parameters
"docker:test-data-creation:add-widgets": "XSOCK=/tmp/.X11-unix &amp;&amp; XAUTH=/tmp/.docker.xauth &amp;&amp; xauth nlist :0 | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge - &amp;&amp; chmod 755 $XAUTH &amp;&amp; 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'"
Part if the dynamically_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
This is where I call it from
context("Dynamically create widgets", () =&gt; {
validateParameters(requiredFields);
createWidgets(WIDGET_TYPE, TOTAL_WIDGETS_COUNT, MAX_WIDGETS_IN_PAGE, WIDGETS_WITH_ROWS);
});
validateParameters function -->
function validateParameters(requiredFields) {
requiredFields.forEach((field) =&gt; {
if (Cypress.env(field) !== undefined) {
return;
}
throw {message: `Field ${field} is required. Please provide it using --env ${field}. Please dcallahan@example.org for an example.`};
})
P.S. I am aware that this forEach couldn't work because I need a specific message if a specific parameter is missing, but I am trying to get to this message and afterwards to get to a specific message if a specific parameter is missing.

Here is the accepted answer: Problem is that in validateParameters function you have passed array of constants. Each constant will have value for example PAGE_TITLE = Cypress.env('PAGE_TITLE'); so it will be anything that is defined in .env. That could be string, number, undefined or something else.
To solve problem you need to check if passed field is not undefined.
function validateParameters(requiredFields) {
requiredFields.forEach((field) =&gt; {
if (field !== undefined) {
return;
}
throw {message: `Field ${field} is required. Please provide it using --env ${field}. Please dcallahan@example.org for an example.`};
})

Here is another answer: You are already setting the requiredFields variable above with Cypress.env('...') so you don't need to call it again.
The cypress docs state that Cypress.env('...') returns, the variable or undefined.
/**
* Returns specific environment variable or undefined
* @see https://on.cypress.io/env
* @example
*    // cypress.json
*    { "env": { "foo": "bar" } }
*    Cypress.env("foo") // =&gt; bar
*/
env(key: string): any
See more about Cypress env here: https://docs.cypress.io/guides/guides/environment-variables.html#Overriding-Configuration

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:03/06/20', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<javascript><command-line-interface><cypress><package.json>', 'Title': 'Throw an exception if one of the parameters is not declared in the CLI command using Cypress?', 'ViewCount': 124, 'id': 60563552, 'user_AboutMe': '<p>I am two times master, but I still have to learn and I am loving it :)</p>\n', 'user_AccountId': 2473487.0, 'user_DisplayName': 'FortuneSoldier', 'user_Reputation': 492.0, 'user_Views': 128.0, 'user_id': 2154696.0}}