1

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.

SaganRitual
  • 3,143
  • 2
  • 24
  • 40
  • 1
    The code block you show is the expected output from running the test. – Dave Newton Sep 08 '16 at 15:00
  • @DaveNewton I understand that much, but I still have no idea how to run the test. – SaganRitual Sep 08 '16 at 15:18
  • @GreatBigBore Your last paragraph is the reason we do not automatically *delete* duplicates. This question of yours will serve as a signpost for people looking for an error. However, it is not a reason to *not* close your post as a duplicate. It is a duplicate. If you use the guide on the Mocha site and run that code with `node` rather than `mocha` you do get the *exact same error* as in the other question. It's the same problem, and has the same solution. – Louis Sep 08 '16 at 15:28
  • When the guide says "Getting Started" and you do the things in "Getting Started" and they don't work, it doesn't occur to everyone to read further in the guide. – SaganRitual Sep 08 '16 at 15:30

1 Answers1

2

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.

Ryan27
  • 453
  • 4
  • 16