4

Scenario

We are trying to use the Node.js QUnit Module aka qunitjs for our Server-Side Testing so that we can write one test and run it on both server and client e.g: https://github.com/nelsonic/learn-tdd/blob/master/test.js

When we run a single file using the command:

node test/my_test.js

It works as expected.

However, when we have more than one test in a /test directory and try to run all the files as a suite using the following command:

node test/*.js

only the first file (alphabetically) gets executed.

see: https://github.com/nelsonic/hapi-socketio-redis-chat-example/tree/master/test

Question

How do we execute multiple test files with a single command?

We tried to dig through existing StackOverflow + GitHub Q/A for this but did not find any match. (any suggestions/help much appreciated!)

nelsonic
  • 31,111
  • 21
  • 89
  • 120

1 Answers1

0

QUnit has command-line runner out-of-the-box, so just run:

npm install -g qunit
qunit

It will search for tests in test folder by default automatically and execute all of them. Also you may specify test scripts manually qunit test/only_this_test.js test/also_this_test.js

It's running, but tests are failing!

If the failure is related to undefined functions, that's because you need to tell your tests how to find your functions-under-test.

Modify tests.js:

// Add this in the beginning of tests.js
// Use "require" only if run from command line
if (typeof(require) !== 'undefined') {
    // It's important to define it with the very same name in order to have both browser and CLI runs working with the same test code
    doSomething = require('../index.js').doSomething;
}

Modify your js scripts:

//This goes to the very bottom of index.js
if (typeof module !== 'undefined' && module.exports) {
  exports.doSomething = doSomething; 
}

See more details in my other answer here https://stackoverflow.com/a/51861149/1657819

P.S. Nice tutorial in the repo! ;)

The Godfather
  • 4,235
  • 4
  • 39
  • 61