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
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"
}Introduce this setup for mocha testing
- Keep
npm startandnpm testreserved for Heroku and Travis. Createnpm run dev:startandnpm run dev:testscripts 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.
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"
},
Alternatively, we keep
npm startandnpm testfor development and usenodemonfor file watching. Then create custom scripts for Travis and Heroku. This has the benefit of being easy to type (npm startopposed tonpm run dev:start) but adds complexity/fragility to the setup.This setup requires a
Procfilefor Heroku to run the right command. It's a way good to introduce Procfiles but it is a bit more complex and fragile.Procfile
And it requires an additional line in the
.travis.yml. Again, a good teaching opportunity, but fragile..travis.yml