Home > Blockchain >  Passing parameters to a bash script from npm
Passing parameters to a bash script from npm

Time:10-22

I have created the following batch that is supposed to run multiple jest tests:

#!/bin/bash
echo 'The test will run 10 times per loop. The number of loops are 20.'
echo 'Total number of instances that will be run: 200 instances'
echo 'Starting in 5 seconds...'
sleep 5
for i in {1..20}; do ${1} & ${1} & ${1} & ${1} & ${1} & ${1} & ${1} & ${1} & ${1} & ${1} || (echo 'Failed after $i attempts' && break); done

The ideia is that once I pass the test as a param, it will run like this: npm run test SomeTest.test.ts & npm run test SomeTest.test.ts & npm run test SomeTest.test.ts.With this I will be running the same test at the same time, without having to wait for it to be completed.

And I have added it to my package.json like this:

"scripts": {
    "stress-test": "./scripts/bash.sh"
}

The idea is to run like this because I want to run only one test from my .test file that has multiple tests npm run test MyJestTest.test.ts -- -t 'some specific test'. And this command works like a charm when used on the terminal without the bash. But if I try to use it with the bash, it won't work.

npm run stress-test -- "npm run test NelnetService.test.ts -- -t 'loan transactions gets loan transactions data'"

For some reason it won't accept the -t parameters that I'm adding. The main idea is to execute a specific test multiple times concurrently through a loop.

Can anyone help me?

CodePudding user response:

npm run test SomeTest.test.ts && npm run test SomeTest.test.ts && npm run test SomeTest.test.ts . With this I will be running the same test at the same time, without having to wait for it to be completed.

You have misunderstood the meaning of "&&".

Those components will not run simultaneously.

Only after successful completion of the first segment, the shell will be instructed to execute the second segment, and only after successful completion of that second segment, the shell will be instructed to execute the third segment. The "triplet" sequence will stop at the first failed condition.

To ensure simultaneous run of all instances, regardless of return-code value, you need to run something like

npm run test SomeTest.test.ts &
npm run test SomeTest.test.ts &
npm run test SomeTest.test.ts &
wait

The will ensure that you only proceed with the next step after all those background processes have completed.

CodePudding user response:

And this command works like a charm when used on the terminal without the bash. But if I try to use it with the bash, it won't work.

You need to check which value is reported by ${SHELL} (add an echo of that value in your script to probe for that value) for each of terminal prompt and in your script. You may believe you are (or are not) using bash for each, but you need to ensure that the desired shell is explicitly specified for the same, or different, as you want it to be.

I am always aware of the distinction, because my terminal shell is bash, but my scripts are always coded with #!/bin/sh which is not fully bash subset; there are conflicts or differences of behaviour for which you need to be aware and code for.

I know for a fact that how shells treat what follows the "--" is different. You need to verify that the shell that is active at the time the command is interpreted is treating the follow-on strings the way you expect them to be.

Another example of shell differences that comes to mind is the need for "-e" on "echo" statements for bash to get proper expansion of newline escape character and others. I prefer using the traditional/legacy/archaic/better "sh" for scripting for that reason.

  • Related