How does `npm test` work?


In creating our own test suites, we only need to invoke npm test and the tests execute as required. How does the command know which test suite to execute, as there may be many? Also, how would we set up our environment so that npm test runs our tests?


The behavior of npm test is specified in the package.json file that is part of all of our applications. If we look inside this file, we will see the following lines

"scripts": {
    "start": "node ./bin/www",
    "test": "bin/wdio-test"

Notice the line "test": "bin/wdio-test". This specifies to our application that when looking to execute tests, via npm test, we should execute the file located at bin/wdio-test. It’s no surprise then that when executing npm test for this assignment we find the following two lines

> calculator-js@0.0.0 test /home/ccuser/workspa
> bin/wdio-test

So to respond to both questions:

  1. The command knows which test suite to specify based on which value it finds corresponding to the “test” key in “scripts”.
  2. We can set up our own tests in our environment by specifying a "test" : "file/path" key/value pair in "scripts"once we have a package.json file for our project.