1
\$\begingroup\$

The purpose: an express node.js server which can be launched using naught, with an integration test using supertest. Each worker naught runs will test itself on a unique port, and only if passes it will listen in on the main port and join the pool.

Major concerns are - no idea what I'm doing. How I provision the ports for the test nodes, closing the test servers (need to clear up memory)?

This just a POC, structuring is required for a production app.

var testport = 10000 + process.pid;
var server = app.listen(testport, function() {
    console.log('pid %d listening on port %d ...', process.pid, testport);

    supertest(server)
    .get('/test')
    .expect(200)
    .end(function(err, res){

        if (err) {
            console.log(err); 
            if (process.send) process.send('offline');
            process.exit(-1);
        }
        else {
            console.log('pid %d tested OK, closing test server and launching main one...', process.pid);
            server.close();
            listenToMainport();                
        }

    });
});

You can see the full context of the example here.

\$\endgroup\$
1
\$\begingroup\$

I think you should write more code before submitting, the fun part to review is the code that you wrote after the prototype ;)

From a quick once over:

  • JsHint finds no fault except for some mixed use of tabs and space
  • Consider 'use strict', it can save lives
  • Consider something better than console.log, especially if you are going to use this code for performance testing
  • Use lowerCamelCase consistently: testport -> testPort
\$\endgroup\$
1
\$\begingroup\$

Supertest is a great tool. You don't actually have to run it on a test port as it does that for you, but it won't hurt.

I have found these tools useful for testing my node server endpoints:

  • Mocha to run the tests. Install this a -g global module.
  • Chai is used to make assertions against test result. This is better than using console.log for sure. This module would need to be required in the test file.
  • should is another alternative for making assertions against your results.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.