I need to unit-test my rest api written in NodeJS. For this, I need to use supertest npm package. I was following this tutorial article for implementing the unit test functionality.
Now, in this article it is mentioned that mocha
is already included in supertest
itself, so there's no need to install it separately. So, I only install supertest
using - npm install --save-dev supertest
.
But, when I add this line in my package.json
- "test": "mocha"
, and run npm test
, it gives error saying 'mocha' is not recognized as an internal or external command
. I need to run npm install --save-dev mocha
to make it run successfully. But why do I need to include mocha
separately and if that's how it is supposed to run, why is it not mentioned in this article or on npm homepage of supertest
?
I went to Supertest page on npmjs and checked the dependencies. Mocha is listed as a dev dependency. it means Mocha was used while creating supertest, but is not required for supertest to function. When I work on Node.js project, I install certain packages as dev dependencies. These could be packages for testing (Mocha) or linting (Standard). What it means is when the project is deployed, it will not need these packages to function.
Also, you can use npm list
command to see which modules are installed. I just installed supertest and ran npm list. This is structure of supertest :
As you can see Mocha is not listed
Further, ran npm ls mocha
and output is:
$ npm ls mocha
[email protected] E:\Nodetest
-- (empty)
and when I ran npm ls supertest
, output is:
$ npm ls supertest
[email protected] E:\Nodetest
-- [email protected]
Why sis the article say mocha is available? may be in an earlier version of supertest it was included as a dependency