Skip to content

Instantly share code, notes, and snippets.

@cklanac
Created December 18, 2017 13:21
Show Gist options
  • Save cklanac/d054ba9f1331e62a5fb76e8e402f0ca6 to your computer and use it in GitHub Desktop.
Save cklanac/d054ba9f1331e62a5fb76e8e402f0ca6 to your computer and use it in GitHub Desktop.
NPM script commands at various phases of the course

npm-run-setups

The primary question is whether npm start and npm test should be setup for developer convenience and other commands like npm run heroku and npm run travis should be setup for services. Or should npm start and npm test should be kept for production and test environments respectively and customer scripts like npm run dev:start and npm run dev:test should be created for development.

The secondary question is... what should they be named?

Below are examples of the scripts at the various phases of the course

Basic - Node/Express

This version would be used at the beginning of the course, before testing has been introduced

"scripts": {
  "start": "node server.js"
},

Introduce this setup during the during express but before mocha

"scripts": {
  "start": "node server.js",
  "dev:start": "nodemon server.js"
}

Intermediate: Mocha Testing

Introduce this setup for mocha testing

  • Keep npm start and npm test reserved for Heroku and Travis. Create npm run dev:start and npm run dev:test scripts for development.
"scripts": {
  "start": "node server.js",
  "test": "cross-env NODE_ENV=test mocha",
  "dev:start": "nodemon server.js",
  "dev:test": "nodemon --exec npm test"
},

The proposed solutions use process.env.NODE_ENV to determine the correct database connection and suppress logging. It is considered bad practice to set process.env.NODE_ENV in code or use globals like so we'll set it in the command using cross-env so it works cross platform. This also assumes the use of a .env file and the dotenv package for database credentials and jwt secrets.

Side Note: Never use mocha --watch. It may run faster but it causes a the app to create multiple processes and database connections. IMHO, the overhead of adding graceful shutdown scripts for --watch (which don't work for SIGINT or SIGTERM) is not worth the effort.

Advanced: Test Coverage

Propose: Introduce test coverage using nyc formerly known as istanbul as an advanced/optional lesson.

"scripts": {
    "start": "node server.js",
    "test": "cross-env NODE_ENV=test mocha",
    "dev:start": "nodemon server.js",
    "dev:test": "nodemon --exec npm test",
    "dev:cover": "nodemon --exec nyc --reporter=lcov --reporter=text-summary npm test"
  },
@tsongas
Copy link

tsongas commented Jan 3, 2018

@cklanac oops yes I meant forever corrected that.

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