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

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

Related

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.

Gitlab CI job succeeds before mocha tests are run

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.

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

npm run build production does not distribute the production

I have a problem with my Angular project. Especially on the build of the project.
When I build my Angular poject with
ng build
it creates the dist-folder with the correct build. When I use the following command:
ng build --prod
it creates the production build (correct)
When I use NPM (used by build server) I use this:
run build
But I want the production build. Whatever I do, it doesn't work locally or on the buildserver. I used these:
npm run build --prod
npm run build --target=production
npm run build --environment=prod
npm run build --environment=production
npm run build --env=production
npm run build --env=prod
npm run build *projectname* --environment=production
npm run build *projectname* --env=production
npm run build *projectname* production
And probably a lot more. It just does not build the production!
I have a environment.prod.ts (production = true). It is set in the Angular.json. I have no clue what I am doing wrong.
You could also use -- to separate the params passed to npm command itself and params passed to your script.
So the correct syntax for your case would be:
npm run build -- --prod
npm run build should have the script in the package.json
have a look there and maybe add the line in the scripts
{
...
scripts: {
"build": "ng build --prod"
},
...
}
this should do the trick
What you can do is update the build script in the package.json as
scripts:{
"build": "npm run ng build --",
"ng": "ng"
}
-- allows you to pass arguments to a script.
Then you can pass the arguments to ng build like ng build --prod
Try this if your environment is production:
npm run build -- --configuration=production
or this if it's prod:
npm run build -- --configuration=prod
If you don't specify the "configuration" parameter name you may see this error:
npm run build -- --prod
Error: Unknown argument: prod
npm run build -- --production
Error: Unknown argument: production

Run grunt build command on Travis CI

I am using Travis CI to test and build my project and as part of it I want travis to run grunt build i have tried the following but have had no luck.
script: "grunt build"
script: "./node_modules/grunt build"
script: "./node_modules/grunt/grunt build"
script: "./node_modules/grunt/grunt.js build"
Have you made sure to install grunt-cli globally on your Travis node?
My Travis CI config looks like:
language: node_js
node_js:
- "0.8"
before_install: npm install -g grunt-cli
install: npm install
before_script: grunt build
And my package.json:
{
...
"scripts": {
"test": "grunt test"
},
...
}
I will explain the flow of steps that Travis will execute:
The first step to be executed is the before_install. My only prerequisite (besides node.js) is grunt-cli so I use this step to install it.
Next is the install step, in my case this will simply install my npm modules
The before script is then executed, running grunt build
Lastly Travis will look for scripts in the package.json, there I indicated the test step should run grunt test
I'd like to note that this is my own opinion on how to configure Travis. I'm certainly not inclining you should use exactly the same approach.
You likely miss in your travis.yml file:
before_script:
- npm install -g grunt-cli
Then "grunt whatever" should execute ok (assuming you do require grunt in your devDependencies in your package.json).
(see http://www.mattgoldspink.co.uk/2013/02/10/using-travis-ci-with-grunt-0-4-x/)
Make sure that you have grunt as part of your devDependencies. Here is a sample file: https://github.com/fraxedas/raspi-cloud/blob/master/package.json
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-jshint": "^0.11.2",
"grunt-contrib-watch": "^0.6.1"
}
Travis-ci will install grunt on the install step:
npm install
...
grunt#0.4.5 node_modules/grunt
...
In my case I wanted to run jshint with grunt.
Here is my travis.yml file: https://github.com/fraxedas/raspi-cloud/blob/master/.travis.yml
To integrate grunt all I needed was:
before_script: grunt jshint
You can change jshint by another command.
My .travis.yml looks like this:
It runs much faster than npm as NodeJSpackage manager, I'm using Yarnon this example. It installs yarn, grunt cli, rubyand sass.
Hopefully it helps.
language: node_js
node_js:
- "7.1.0"
before_install:
- npm install -g yarn
- yarn add global ruby
- gem install sass
install:
- yarn add global sass
- yarn add global grunt-cli
- yarn add yarn install
before_script: grunt

Resources