GreatBigBore GreatBigBore - 3 months ago 7
Node.js Question

Can't make sense of the Mocha 'Getting Started' instructions

Trying to use Mocha for unit testing. The Getting Started instructions are straightforward enough at first:


  • Install Mocha with
    npm install --global mocha

  • Create a file called test.js

  • Edit the file and paste some predefined stuff into it



All great, so far. But then, "Back in the terminal":

Array
#indexOf
...etc


Ok, I'm totally baffled. Obviously, it doesn't mean to type this on the command line. But I tried running node and doing a
require('<test file>');
to no avail. Edit: The module does not load (I had said it does). Running
require('assert')
does work, but that does me no good. I can run Array().indexOf(), but that has nothing to do with my testing.

What does this last instruction really mean? What am I really supposed to do to get this framework going so I can use it for unit testing?

Edit: Also tried
require('assert');
followed by
require('mytest.js');
also with no joy.

Edit: just got a clue that Array().indexOf() is just JS code, having nothing to do with mocha or the assert module. But still, no luck with mocha.

Edit: As for there already being an answer to this question, there is no way I could have known that this was about a reference error. Reference error was only one of many failures that occurred during my wild grasping. The real problem is that the mocha instructions are not clear, and that makes this question totally different from the one about the reference error.

Answer

You can run your test with mocha test.js. That is the part they are missing in the 'Getting Started' in order to generate the output mentioned.