Gitlab-ci runner hangs after cypress tests - gitlab-ci

I am using gitlab-ci to tests a react application with cypress.
The test seems to pass but it hangs after executing cypress run command.
Thus, the test fails because of the timeout.
My service is the following
cypress:
image: cypress/base:10
script:
- serve -s build -l 3000 & yarn wait-on http://localhost:3000
- yarn cypress:run
And in my package.json
{
...
"scripts": {
"cypress:run": "cypress run --spec 'cypress/integration/**/*spec.js' --record false --config video=false"
},
...
}
This is the end of gitlab-ci runner's log:
✔ All specs passed! 01:01 11 11 - - -
Done in 73.82s.
ERROR: Job failed: execution took longer than 20m0s seconds

this issue occurs when a background task is running in the runner
To fix this I put in an or condition on the cypress:run step and kill the process if the result is not a success
there is another kill statement added in the step below also in case -parallel is used and multiple steps are running
Something like this
script:
# start the server in the background
- npx serve -s build -p 3001 &
# run Cypress tests in parallel
- yarn cypress:run || (ps -ef | grep [s]erve| awk '{print $2}' | xargs kill -9 )
- (ps -ef | grep [s]erve| awk '{print $2}' | xargs kill -9 ) || exit 0

I don't know if this can help someone, but it is worth to try updating node.
I had the same problem when using cypress 11.1.0 and node:16.17.1-slim docker image. I do not experience hang ups with node:16.18.1-slim anymore.
P.S. Along with node update I've updated Chrome from 106 to 107, so can't be sure what actually made the trick, just wanted to share with possible solution.

Related

Continue running scripts even after exit code 1

I'm trying to run Cypress test in Gitlab. Below is the sample script. After executing 'npm run Cypress', if there is any test case fail, it exits with 'exit code 1' and next two commands won't run.
Is there a way I can execute next two commands. Next two commands, generates consolidated Jnuit and HTML report.
script:
- cd ./cypress
- npm ci
- npm run Cypress
- npm run mochawesome
- npm run junit:merge
I have tried below mentioned solution but no luck.
script:
- cd ./cypress
- npm ci
- npm run Cypress || exit 0
- npm run mochawesome
- npm run junit:merge
script:
- cd ./cypress
- npm ci
- npm run Cypress
after_script:
- npm run mochawesome
- npm run junit:merge
output Image:
One way would be instead of mentioning the exit code, which seems to be dynamic you can directly echo something after the || operator.
npm run Cypress || echo \"The previous command has some errors..Continuing\"
Using the after_script approach actually should work fine as you can see from this minimal example:
# .gitlab-ci.yml
test:
image: alpine
script:
- echo "Hello after_script!" > test.txt
- exit 1
after_script:
- cat test.txt
Output:
$ echo "Hello after_script!" > test.txt
$ exit 1
Running after_script
Running after script...
$ cat test.txt
Hello after_script!
Cleaning up file based variables
ERROR: Job failed: exit code 1
Also, you can consider using set +e and set -e to disable/enable exit on error.

Mocha tests take 20 seconds to get started in docker?

I'm testing frontend-only-code with mocha, docker-compose and node. The following is run in docker using docker-compose run.
NODE_ENV=test env NODE_PATH=$NODE_PATH:$PWD/src mocha $(find . -name 'test_*.js') --compilers js:babel-core/register -r jsdom-global/register --require babel-polyfill
It takes about 15-20 seconds until the first (!) test starts running.
I thought the docker container would boot fast because after 2 or 3 seconds my shell command (see above) is shown on the console. (Which I assumed, would mean the container is up). But after the command is shown it takes 15 - 20 seconds to run the first test.
What's wrong?
Edit: I'm on a Macbook Pro with macOS

How to break Travis CI build if Appium/Mocha tests fail?

I have a Travis CI project which builds an iOS app then starts Appium and runs tests with Appium/Mocha.
The problem is that even though the Mocha tests fail and throw exception, the shell script which runs them via Gulp still exits with 0 and the build is deemed passing.
How can I make the build break/fail when the Mocha tests fail?
Here is how I managed to make this work:
Instead of running the Mocha tests via Gulp, run them directly from the shell script
Save the output to mocha.log besides displaying on stdout
./node_modules/.bin/mocha --reporter spec "appium/hybrid/*uat.js" 2>&1 | tee mocha.log
Check mocha.log for the string " failing" and exit with 1 if found
.
if grep -q " failing" mocha.log; then
exit 1
fi
The exit 1 will make the Travis build fail.

How do create a shell script to automate running watchcompile and browser-sync

I am trying to do something like this to automate watchcompile and browser-sync but it's not working.
#!/bin/sh
nvm use 0.10
watchcompile
browser-sync start --server --files "index.html, css/*.css, js/*.js"
This is to be run in the project directory.
Running the above gives me the following:
./watch.sh: line 2: nvm: command not found
./watch.sh: line 3: watchcompile: command not found
./watch.sh: line 4: browser-sync: command not found
watchcompile and browser-sync should be separate processes.
Any help will be greatly appreciated.
This is how I solved it:
I added this to ~/.bash_profile. This adds the nvm directory to PATH:
export PATH="~/.nvm/v0.10.33/bin/:$PATH"
I then added these lines to watch.sh:
#!/bin/bash
watchcompile &
ps -eaf | grep watchcompile
cd htdocs
browser-sync start --server --files "index.html, css/*.css, js/*.js" &
ps -eaf | grep browser-sync
The & at the end of line 2 and 5 makes the command run in the background, and the two
ps -eaf | grep command
lines are just there to give me the process ids of the background processes so that I may later kill them.
I am sure there is a better way to do this, but at least this works.

nodejs, run test automatically when files change

Is there a way to automatically run tests, when a file in the app is changed? In rails there is a gem called guard. How can one achieve the same in nodejs?
Not sure if this would work for tests, but Nodemon (https://github.com/remy/nodemon) looks like what you want.
Install Jasmine and run
jasmine-node <dir> --autotest
Try this
touch /tmp/nt; while [ true ]; do if [ find . -newer /tmp/nt -type f
| grep -v app/cache | wc -l -gt 0 ]; then phpunit; touch /tmp/nt; fi;
sleep 5; done
I'm using it to autostart phpunit. Replace phpunit with the command to run tests
replace sleep 5 with sleep 1 if you wish to check every second (depends on the size of your files)