3

I have been trying to run multiple npm commands to run some of my cypress tests in a sequence. after some research what I found was to use && in between npm commands.

package.json file, inside scripts I defined a test as,

"scripts":{
"test":"npm run cypresscommand_1 && npm run cypresscommand_2"
}

When I execute this using npm run test for some reason, npm run cypresscommand_1 got executed but npm run cypresscommand_2 didn't get executed. With further research and going through some tutorials, later I tried two modifications

modification 1: insted of && I used &

"scripts":{
"test":"npm run cypresscommand_1 & npm run cypresscommand_2"
}

modification 2: insted of && I used ||

"scripts":{
"test":"npm run cypresscommand_1 || npm run cypresscommand_2"
}

surprisingly both gave me the expected results which mean both executed npm run cypresscommand_1 and then npm run cypresscommand_2

What I want to know is,

  1. In the new versions of npm did they replace with && with & and ||
  2. Is the meaning of & is equal to and
  3. Is the meaning of || is equal to and
  4. Is there any difference between & and ||

Although the code is working perfectly, I want to make sure if I'm using the correct syntax or not. Can someone help?

Thank you.

Hrudayanath
  • 474
  • 4
  • 18
Muditha Perera
  • 1,216
  • 8
  • 24
  • Your question implies that you want `npm run cypresscommand_2` to run regardless of whether `run cypresscommand_1` completes successfully or fails. Is that correct? and if so do you want that same logic to apply across platforms, i.e. when running `npm run test` on either Windows and/or _*nix_ (Linux. macOS, etc) operating systems? – RobC Jul 10 '20 at 09:45
  • Yes. Actually I did this setup for cross browser testing. So, I want tests to run with firefox even chrome fails. In any platform. – Muditha Perera Jul 10 '20 at 13:43
  • 1
    Then you need to be mindful that using the semicolon (`;`), as suggested in the answer that you accepted, does not behave the same in any platform because on Windows NPM utilizes `cmd.exe` as the default shell to execute npm scripts. See my answer [here](https://stackoverflow.com/questions/62343768/how-to-continue-running-scripts-defined-in-package-json-in-case-one-fails/62348702#62348702) explaining that the `&` operator in `cmd` is analogous to a semicolon `;` in `sh` on _*nix_. – RobC Jul 10 '20 at 14:06
  • Thank you for the information. I have used '&' to use solve my issue. In your answer, you have said && is replaced with &. Does it mean && is not going to work or && have a different meaning now? – Muditha Perera Jul 12 '20 at 16:51
  • The `&&` operator essentially acts the same in both `cmd.exe` _(Windows)_ and `sh` _(*nix)_. It's logic is as follows: **A)** If the task/command on the left side of `&&` operator fails for whatever reason, (i.e. it exits with non-zero code/status), then the task/command on the right side of the `&&` operator _does not_ get executed. **B)** If the task/command on the left side of `&&` operator completes successfully (i.e. it exits with a zero code/status) then the task/command on the right side of the `&&` operator _does_ get executed. – RobC Jul 13 '20 at 08:26

1 Answers1

9

It has nothing to do with npm, these are interpreted as bash commands on Linux based systems,

& - means it will run as background job,

&& - exit code from each command and uses it as an operand in a chained && operation.

| - is a pipe operator where the output of one command is passed on to the following command,

|| - the OR logical operator, and make Bash continue processing chained commands if only one of a pair completes.

For your case you can try using semicolon ';' which runs the commands one after other even if it fails.

"scripts":{ "test":"npm run cypresscommand_1 ; npm run cypresscommand_2" }

Hrudayanath
  • 474
  • 4
  • 18
  • Thank you very much for the explanation, I've been googling all over to find what's going on. If you have a link to refer can you add that in a comment? – Muditha Perera Jul 09 '20 at 17:30
  • 1
    check this https://www.linux.com/training-tutorials/logical-ampersand-bash/ – Hrudayanath Jul 09 '20 at 17:31
  • you can try few bash commands with above operators you would get more clarity like ls & ls, ls && ls, ls | wc-l , ls || cd, cd || ls. – Hrudayanath Jul 09 '20 at 17:34
  • I would definitely do it. If not even though the code is working fine I have no clue why it's running correctly or not. Thank you once again. – Muditha Perera Jul 09 '20 at 17:40
  • 3
    _"these are interpreted as bash commands"_ is not entirely correct. It depends on the shell used to run npm scripts. By default NPM runs npm scripts via `sh` on _*nix_, and `cmd` on Windows. Both `&&`, `||`(operators), and `|` (pipe), are same in both shells (`sh` and `cmd`). However, [`&`](http://www.embeddedframeworks.com/cmds/nt/syntax-redirection.html) operator differs in `cmd` and `sh`. The `&` in `cmd` is analogous to `;` in `sh`. See example [here](https://stackoverflow.com/questions/62343768/how-to-continue-running-scripts-defined-in-package-json-in-case-one-fails/62348702#62348702). – RobC Jul 10 '20 at 08:50
  • True @RobC it varies on windows and linux based os. – Hrudayanath Jul 10 '20 at 15:47