With a node package manager's (npm) package.json
script property, you can preconfigure common tasks like running unit tests with
When npm initialized our package.json file for us it created this scripts field and within it entered a test entry which right now is just going to echo out an error message. The scripts field is extremely useful and a recommended way for you to provide workflow scripts for your module for people that are using it. Right now, if we were just to say, npm test, it's going to spit out that error message that we see defined here. But making sure that your module's test can be run with npm test is a big part of being a good module author.
To demonstrate this, I've just defined the functionality of our module to just be a simple function that will convert a string to upper case. I've written a simple mocha test here that requires that file. This ./ just means that it's going to look in the current directory and default to a file named "index." We just have a simple test here that just says expect up-hello to equal hello in uppercase. We can see that that does work. That test passes if we just run mochatest.js.
We can see that one is passing. But in order for somebody using your module to know that you're using Mocha, that the file is called test.js, that's a little bit more work than they should really have to do. What it's better to do, is to go in here and update this test field with that exact command. If we put that into our test field we can then come down here and say, npm test, and it's going to run that exact command for us so we can see that our tests are passing.
Now somebody can install your module, run npm test, and they don't need to know or care about what sort of testing library you're using or how things are set up. They know that your tests are passing. There's also a shortcut. You can do npm t. It's the same thing. Test and a few other ones are sort of special fields in the npm world. You can run them directly like we just saw with npm test. You can also add custom ones here.
If we wanted to add something like a check version. We'll make that run Mocha version. If we then save this we can then run this custom script by doing npm run checkversion. Any time that you run a custom script you can just do it with npm run and the name. It will look for that name within your scripts field.