Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Unit Testing Behavior Driven Development with Mocha & Chai General Structure of Test Files

Alejandro Molina
Alejandro Molina
3,997 Points

Why do "npm test" and "mocha test" both do the same thing? Is there a difference?

They both go into the folder named "test" and run all the scripts in them. Are they just interchangeable?

3 Answers

Tom Geraghty
Tom Geraghty
24,174 Points

npm looks in the package.json file for instructions on what tasks it should do when you type npm <command>. It can do a lot of tasks (as in the scripts link provided by Matthew).

In this course we used npm init to create the package.json file and npm install --save-dev mocha chai to install those two as development dependencies. Because npm is pretty smart it automatically setup mocha as the program that is in charge of the npm task that runs tests.

It might have been more informative for Guil to have demonstrated that we could also manually edit our package.json file to instruct npm to use mocha when performing the test task with npm test.

npm test is a shortened version of npm run test; npm is running the test command as defined in the package.json configuration file.

So to answer your question, no it's not the same thing. npm isn't doing any testing on it's own; it is merely running the mocha command for you.

What i got from the course was that npm test is the built-in task npm runs to test your script. npm test mocha just test the mocha script. Here is a link for more info. https://docs.npmjs.com/misc/scripts

Gabbie Metheny
Gabbie Metheny
33,778 Points

If you're working locally rather than in workspaces, and you've never used mocha before, you'll notice beginning in video 3 that your computer has no mocha command. The way Guil runs it initially only works if mocha is installed globally on your computer, but when you add "test": "mocha" to your package.json scripts, npm can use your project installation of mocha to run the tests for you. Some more info in this thread :smile: