Skip to content

Instantly share code, notes, and snippets.

@heiso
Last active Nov 19, 2021
Embed
What would you like to do?
From Tech with Love

Code examples

Misc.

Graphql

Misc

Security

Front

JS

Back

Front

npm

Security

Dockerfile & node

If you use npm to start your app, you will lose some important intels like complete error messages relative to node. To avoid this, you should set a CMD at the end of your Dockerfile:

FROM node:alpine

CMD ["node", "index.js"]

Gitlab-CI

gitlab-ci.yml

stages

Gitlab CI provides stages defined at the begining like so:

stages:
  - tests
  - publish

Then you define differents jobs. In each of these you can specify a stage:

test_lint:
  stage: tests
  script:
    - my_lint_script

publish_docker:
  stage: publish
  script:
    - my_deployment_script

test_unit:
  stage: tests
  script:
    - my_testing_script

Here we can see 3 jobs, as defined in the stages object; CI will begin with tests jobs and then proceed with the publish job. So here, test_lint and test_unit will be running in parallel and when both are done, publish_docker will run. Keep in mind that any job returning an error will break the pipe, so publish_docker will only run if previous jobs didn't return any error.

npm ci

npm ci is a new command that will install node dependencies faster than npm i. The difference is that npm ci will only fetch dependencies from the package-lock.json file without checking for new versions or deprecated ones. It's recommended to use npm ci for a faster CI pipeline.

Coverage

Coverage is a great tool to enhance testing and avoid regression.

Install & config

Install it with npm i --save-dev nyc and add it to your testing script like

"scripts": {
    [...]
    "test:unit": "mocha -R spec test/unit/**/*.specs.js --recursive",
    "test": "nyc npm run test:unit",
    [...]
  },

and add a configuration to it in package.json

"scripts": {...}
"nyc": {
    "reporter": [
      "lcov",
      "text-summary"
    ],
    "include": [
      "lib/**/*.js"
    ],
    "all": true
  }

This config will generate a coverage folder with lvoc formatted report that can be used by your IDE to show coverage while coding. You will also have a text summary at the end of your testing script. This summary can then be interpreted by Gitlab and help to generate a coverage badge

To do so, go to your repository settings on gitlab then click on CI/CD pipelines and somewhere at the end you can find a Test coverage parsing form field, use this regex ^Statements\s*:\s*([^%]+), save, and now Gitlab can read your coverage report.

Also if you want to host your coverage report on gitlab (and maybe use it with gitlab pages, cf. pages job below), you can add an artifact to your gitlab-ci testing job:

test_unit:
  stage: tests
  script:
    - npm ci
    - npm test
  artifacts:
      paths:
        - coverage/

pages:
  stage: publish
  only:
    - master
  dependencies:
    - test_unit
  script:
    - mv coverage/lcov-report public
  artifacts:
    paths:
      - public

Gitlab pages

Adding gitlab pages on a project is easy, the only thing you have to do is to add a pages job into you .gitlab-ci file.

pages:
  script:
    - mkdir public
    - mv index.html public/
  artifacts:
    paths:
      - public

Each file you want to deploy on a gitlab pages should be stored into a public folder, here we just moved an index.html to the public directory, and then we tell to gitlab to keep it as an artifact, this artifact will represent the content of our static site published by the gitlab pages job.

Then go to your project Settings/Pages and you should see a list of current hosted pages.

Coverage on Integration tests

Same configuration in package.json than for coverage on test unit

In Gitlab-ci

test_integration:
  stage: tests
  script:
    - npm ci
    # Run service wrapped with nyc as a background task
    - NODE_ENV=ci npx nyc node index.js &
    # Store node's pid
    - echo $! > pid
    # Wait for our service to boot
    - sleep 3
    # Run tests and kill service if test fail
    - NODE_ENV=ci npm test || ( ( kill -SIGINT `cat pid` || true ) && rm pid && false )
    # Kill service
    - ( kill -SIGINT `cat pid` || true ) && rm pid
    # Wait one second to let the nyc wrapper generate coverage result
    - sleep 1
  artifacts:
    paths:
      - coverage

Infra

Life & Work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment