Libraries - GradedJestRisk/js-training GitHub Wiki

Table of Contents

Overview

List:

  • network:
  • git hooks:
  • handle HTTP exception: boom.js
  • handle separate config files:
  • dependency injection: awilix
  • test
  • lint
    • eslint
    • standard
    • lint-staged
    • prettier
    • editorconfig
  • security
    • [eslint-security]
  • routing
  • development
  • environment
    • environment variable:
      • dotenv
      • dotenv-cli
  • string
    • count: letter-count: node -e "console.dir(require('letter-count').countFromFile('./package.json'));"
  • logging

Environment

Code sample here

12-factor app

12factor recommend to store configuration in the environment, not in the codebase. One way to do so is to use environment variables to pass configuration. Your application should not load them, but just read them.

Tradeoffs for local development include .env file (see below). Still, they use local-scope variable so mitigate risks. If you tried to handle configuration with npm script, you may try globals with npm, but it wouldn't work.

Why ?

  • in Linux, you can't mutate globals globally, as they are not shared, merely copied.
  • npm create a subshell each time it executes
So do not create any npm script this way scripts: { "setup": "export FOO=bar"}

2 solutions:

  • do not follow 12-factor and use node .env libraries (see below)
  • use docker and OS-level tools like direnv

.env files

CI/CD platforms (eg. Heroku) load configuration (eg. port):

  • from files named .env
  • to environment variables
In order to replicate this locally:
  • exclude from versioning: add to .gitgnore file
.env*
!.env.example
  • create a .env file
touch .env 
  • set variables
FOO=bar
BAR=3

You can document what you expect (commit these files)

  • .env.example
  • sample.env

dotenv

dotenv

List:

  • install: npm install dotenv
  • create local file: touch .env
  • add a key : API_PORT=3000
  • import require('dotenv').config()
  • use it const port = process.env.API_PORT
Does not handle previously-set variable yet, see this issue

You can extract dotenv import by putting it in the command line.

node -r dotenv/config index.js

dotenv-cli

dotenv-cli expose variables in command-line scripts. Useful for CLI, eg mocha

handle HTTP exception

test

mutation

stryker

List:

  • no clean way to exclude console.log mutation yet (PR here)
Config file

mock http

set response

const nock = require('nock')

const scope = nock('https://api.github.com')
  .get('/repos/atom/atom/license')
  .reply(200, {
    license: {
      key: 'mit',
      name: 'MIT License',
      spdx_id: 'MIT',
      url: 'https://api.github.com/licenses/mit',
      node_id: 'MDc6TGljZW5zZTEz'
    }
  })

assert

// arrange
const GoogleAPICallNock = nock('http://google.com')
  .get('/')
  .reply(200);

// act
callAPI();

// assert
assert.strictEqual(GoogleAPICallNock.isDone() , true);

debug

API DEBUG=nock.* npm test

Collections

montagejs/collection

Install npm install collections

Choose the collection here

⚠️ **GitHub.com Fallback** ⚠️