What is the test command while creating package.json?

node.jspackage.json

node.js Problem Overview


While creating package.json from command line using npm init for creating a module in Node.js, there is a test command field that I don't know about. There's no mention of it in the docs too on executing npm help json also in the CLI.

Please explain what it is about.

node.js Solutions


Solution 1 - node.js

The test command is the command that is run whenever you call npm test.

This is important when integrating with continuous integration/continuous deployment tools (such as jenkins, codeship, teamcity).

Example:

  • say you deploy a project to AWS or some other cloud hosting provider,
  • you can set up your infrastructure to automatically run npm test.
  • If there are problems within those tests, your ci/cd will automatically rollback before deploying.

To execute tests
You can use karma, jest, or selenium/nightmare/phantomjs or about any other test scripting library/framework that allows you to write and execute tests and then set the required command in scripts.test and finally run it from npm test.

Solution 2 - node.js

Assuming you mean scripts.test:

"scripts" : {
  "test" : "echo \"Error: no test specified\" && exit 1"
}

This field contains the program(/command line) that should run when you call npm test. Typically, that program is a test-runner like mocha, ava, jest, ...

The default value is a placeholder that prints an error message (try running npm test in the same directory as your package.json).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAakash VermaView Question on Stackoverflow
Solution 1 - node.jsDenis TsoiView Answer on Stackoverflow
Solution 2 - node.jsrobertklepView Answer on Stackoverflow