Gitlab CI job succeeds before mocha tests are run - node.js

I found that my Gitlab jobs are succeeding and ending before the actual test is run.
First, I installing the required dependencies for my app, then I call the mocha commands, but the job succeeds before there is any output from them.
My .gitlab-ci.yml:
image: node:lts-alpine
stages:
- test
test:
stage: test
services:
- mongo:latest
script:
- cd server/
- apk add --update git
- apk --no-cache add g++ gcc libgcc libstdc++ linux-headers make python
- npm install --quiet node-gyp -g
- npm install
- npm rebuild bcrypt --build-from-source
- npm i mocha -g
- mocha ./src/tests/unit/. --timeout 10000 --exit
- mocha ./src/tests/integration/. --timeout 10000 --exit
cache:
key: "$CI_PROJECT_ID"
paths:
- server/node_modules/
And the last couple of lines from the runners output from the console:
...
make: Leaving directory '/builds/myapp/myapp/server/node_modules/bcrypt/build'
bcrypt#3.0.2 /builds/myapp/myapp/server/node_modules/bcrypt
$ npm i mocha -g
/usr/local/bin/mocha -> /usr/local/lib/node_modules/mocha/bin/mocha
/usr/local/bin/_mocha -> /usr/local/lib/node_modules/mocha/bin/_mocha
+ mocha#6.1.4
added 115 packages from 509 contributors in 5.54s
$ mocha ./src/tests/unit/. --timeout 10000 --exit
$ mocha ./src/tests/integration/. --timeout 10000 --exit
Creating cache 8738844...
server/node_modules/: found 19633 matching files
Uploading cache.zip to https://storage.googleapis.com/gitlab-com-runners-cache/project/XXXX/XXXX
Created cache
Job succeeded
my folder structure:
- root
- client/
- server/
public/
src/
tests/
unit/
someUnitTest.js
integration/
someIntegrationTest.js
package.json
...
Why is it not waiting for the commands to start/finish? Locally, they work of course. I also tried using npm run test as an alias for the 2 mocha commands, but it results in the same.

I have found the problem:
The mocha commands are not returning anything because they fail before they start. They have a require('app.js') at the top which starts the server and so on, and due to a missing .env file, this and the mocha tests failed silently.
So properly including a .env file solved the problem.

Related

ng test --no-watch --watch=false never exiting in gitlab CI

As the title says, I simply want to run ng test from within gitlabCI.. but all the flags I've seen mentioned don't affect anything.. the routine just sits and sits. are these flags enough or do I need to setup jest.config.js ? I see mention of karma.conf.d in other tutorials but I do not have that file at all in the project
package.json
"scripts": {
"affected:apps": "nx affected:apps",
...
"test:ci": "ng test --watch=false --no-watch",
.gitlab-ci.yml
run-ng-test-ci:
tags:
- juju
stage: test
image: registry.gitlab.com/jrgemcp-public/gitlab-cicd-docker/build-mahrio-docker:latest
script:
- node --version
- npm --version
- npm ci --cache .npm --prefer-offline --ignore-scripts --quiet
- npm run test:ci
$ node --version
v14.20.0
$ npm --version
6.14.17
EDIT: Seems it's perhaps a Jest issue
https://forum.gitlab.com/t/runner-hangs-sometimes-after-all-jest-tests-are-completed/38853

Permission denied when running npm test via mocha and nyc on Travis-CI

I'm setting up my server side for NodeJS application which I'm integrating with Travis-CI. I'm using Mocha and NYC to help with my tests. However, while these tests run fine locally, I'm failing the tests when on Travis-CI.
Locally everything works fine with no errors.
Travis-CI output error:
> server#1.0.0 test /server
> nyc --reporter=lcov npm run run-test "--coverage"
sh: nyc: Permission denied
npm ERR! Test failed. See above for more details.
The command "docker run repname/server npm test -- --coverage" exited with 1.
That's my .travis.yml file:
sudo: required
services:
- docker
before_install:
- docker build -t repname/server -f Dockerfile.dev .
script:
- docker run repname/server npm test -- --coverage
I even tried some of other similar problems find on stack:
Thats what I tried with no luck:
For NYC:
before_script: chmod 0555 ./node_modules/.bin/nyc
before_script: chmod 0777 ./node_modules/.bin/nyc
For Mocha:
before_script: chmod 0555 ./node_modules/.bin/mocha
before_script: chmod 0555 ./node_modules/.bin/mocha
I solved it. The problem was that I wasn't add the right path in .gitignore file. So when I push to master on github, it actually pushed the node_modules folder, which was not supposed to do.
Keep that in mind when you try to build on Travis CI or any CI, don't put your module folder in your main repo.

This job is stuck because the project doesn't have any runners online assigned to it. Go to Runners page

i'm really new with this, when I started the project, I put this code the .gitlab-ci.yml, and work fine, but today I tried to merge and the pipeline never end, always still in pending.
any idea?
one week ago work
file: .gitlab-ci.yml
image: node:10
before_script:
- apt update
- apt-get install -y nodejs
- npm install -y npm#6.11.0
- nodejs -v
- npm -v
- ls -la -F
- yarn
# CI/CD Pipeline
stages:
- lint
lint:
stage: 'lint'
script:
- yarn
- yarn lint
# Setup Node.js cache for the runners
cache:
paths:
- node_modules/

Installing NPM package from gitlab doesn't download all repo files only on CI

I'm using GitLab as my npm package repo. The project consuming this package is using an ssh url and targeting a specific tag. The entry in the consuming projects package.json looks like: "my-package": "git+ssh://git#gitlab.com:company/repo.git#tag"
Now to clarify, this works just fine on my dev machine. I can wipe my node_modules and package-lock.json file, clear my npm cache, and do an npm i and my-package gets successfully built and installed.
The package.json for the my-package project has a prepare script used to build the package on install. It looks like this:
"scripts": {
"prepare": "npm run build",
"clean": "npx rimraf lib",
"build": "npm run clean && tsc && npx gulp"
},
my-package is in a private repo, and so to solve the initial permission issues, I had to create ssh keys, I added the public key to the my-package GitLab repo under the Deploy Keys section, as per these instructions https://docs.gitlab.com/ee/ssh/#deploy-keys.
The .gitlab-ci.yml file in the consuming project includes the private key into the ssh-agent like so:
before_script:
- apk update
- 'which ssh-agent || ( apk update -y && apk add openssh-client -y )'
- mkdir -p ~/.ssh
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | ssh-add -
- ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts
And I have no permission issues. The test stage on the cosuming projects CI looks like this:
test:
stage: test
script:
- apk update
- apk --no-cache add --update nodejs nodejs-npm git
- npm install
- ls node_modules/my-package/
- npm run lint
- npm run test
The strange thing is, the npm install doesn't download and build my-package here (like it does on my computer), it only installs the package.json and readme file. The result of the ls node_modeuls/my-package on the ci platform from above results in this:
$ ls node_modules/my-package/
package.json
readme.md
and of course since my package isn't being downloaded the tests fail to run that use that package.
I've tried a million different permutations of tweaks on both of the projects over the last 2 days. And nothing I do works on the CI platform (yet the whole time it always works locally 🧐). I'd really love to know what I'm overlooking, as since this works on my computer, then there has to be a way for it to work on the CI platform.
Any thoughts?
I got it,
Solution, add this instruction
- npm config set unsafe-perm true
before yours
- npm install
unsafe-perm
Default: false if running as root, true otherwise
Type: Boolean
Set to true to suppress the UID/GID switching when running package scripts. If set explicitly to false, then installing as a non-root user will fail.
This did NOT work npm install --unsafe-perm

How can I see the output of my travis build

I can't get my tests to work when I build my node.js project using travis CI, although they are working locally using "npm test".
My tests (only one) is defined in the package.json file:
"scripts": {
"test": "grunt --verbose"
},
And all I do in the .travis.yml file is:
language: node_js
node_js:
- "0.10"
script:
- cd microservicebus.host && npm test
If I change it to below it works (but still no output)
script:
- echo 'Hello'
If I could only get some useful output from the build, I could probably figure it out, but I all I can see is something like this:
Checking connectivity... done.
$ cd microServiceBus/microservicebus.host
$ git checkout -qf 9ad3d17e94e6f4bacbc.....
This job is running on container-based infrastructure, which does not al....
If you require sudo, add 'sudo: required' to your .travis.yml
See http://docs.travis-ci.com/user/workers/container-based-infrastructure/ for details.
$ nvm install 0.10
##########################
Thankful for any suggestions
The complete error log that you are not seeing is probably:
This job is running on container-based infrastructure, which does not allow use of 'sudo', setuid and setguid executables.
If you require sudo, add 'sudo: required' to your .travis.yml
Try disallowing sudo in your .travis.yml file:
sudo: false
language: node_js
node_js:
- "0.10"
script:
- cd microservicebus.host && npm test
You can get more info about Travis CI container based infraestructure here:
http://docs.travis-ci.com/user/workers/container-based-infrastructure/

Resources