taufique
taufique

Reputation: 2751

Running mocha test in subfolders

I am using Sails.js for some project. Now I wrote some unit tests. My test folder hierarchy is like following:

/test
    bootstrat.test.js
    mocha.opts
    /unit
        /controller
            User.js
            Representative.js
        /model
            User.js
            Representative.js
        /service
            Utility.js

For testing I now have to run all these following commands:

$ mocha test/unit/controller
$ mocha test/unit/model
$ mocha test/unit/service

But this is time consuming and too manual. I wanted to run all these tests with just one command and searched Google. I tried running mocha --recursive test. But it's not working. It results in following error -

  1) "before all" hook
  2) "after all" hook

  0 passing (2s)
  2 failing

  1)  "before all" hook:
     Error: timeout of 2000ms exceeded
      at null.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:159:19)
      at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

  2)  "after all" hook:
     TypeError: Cannot call method 'lower' of undefined
      at Context.<anonymous> (/home/gladiator/Codes/rms-generic-web/test/bootstrap.test.js:21:9)
      at Hook.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:218:15)
      at next (/usr/local/lib/node_modules/mocha/lib/runner.js:259:10)
      at Object._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)
      at processImmediate [as _immediateCallback] (timers.js:345:15)

Is there a way in which I can run all these tests with just one command?

Upvotes: 4

Views: 2906

Answers (2)

Edgar
Edgar

Reputation: 1313

In your package.json file just add the following:

"scripts": {
    "test": "mocha ./test -b -t 10000"
 }

-b stops your tests after the first failure.

-t is test-case timeout in milliseconds

Then from the directory containing the package.json and test folder run npm test command. It will run all your tests.

Upvotes: 1

jaumard
jaumard

Reputation: 8292

Did you try to make you own bash script ?

Create mocha.sh and put this in it :

#!/bin/bash

mocha test/unit/controller
mocha test/unit/model
mocha test/unit/service

And run it with sh ./mocha.sh

I don't know enough mocha to do it with it.

Upvotes: 3

Related Questions