2

This gulp task hangs on exec('node config/app') line. first exec works fine but the second just hangs.

gulp.task('test', function(cb) {
    var exec = require('child_process').exec;

    exec('echo 3', function(err, stdout) {
        console.log(stdout);
    });

    exec('node config/app', function(err, stdout, stderr) {

        console.log(stdout);

        var testemOptions = {
            file: 'testem.json'
        };

        var t = new testem();

        return t.startCI(testemOptions, function() {
            cb();
        });
    });

});

I can see the output 3 but no output is shown for the second console.log.

I am trying to run my server before running the tests with testem.

I've tried this similar solution but it doesn't work: Exec not returning anything when trying to run git shortlog with nodejs.

Also I've recently asked a hanging testem gulp task question: Testem gulp task hangs after finished.

Edit:

My current solution is:

gulp.task('test', /*['build'],*/ function(cb) {
    var spawn = require('child_process').spawn;

    var proc = spawn('node', ['config/app']);

    proc.stdout.on('readable', function() {
        var output = proc.stdout.read();

        if (output && output.toString().match('express listening')) {

            var testemOptions = {
                file: 'testem.json'
            };

            var t = new testem();

            t.startCI(testemOptions, function() {
                proc.kill();
                cb();
            });
        }

    });

});
Community
  • 1
  • 1
eguneys
  • 6,028
  • 7
  • 31
  • 63
  • try with exec('node config/app', { timeout: 1000 }, function(err, stdout, stderr) { console.log('done') }), with and without the timeout to understand if it is node config/app that hangs – Jerome WAGNER Aug 24 '14 at 08:53
  • @Jerome WAGNER wtf it worked. – eguneys Aug 24 '14 at 08:56
  • if it works with timeout then it is because "node config/app" never returns and you have to find why – Jerome WAGNER Aug 24 '14 at 08:57
  • @Jerome WAGNER oh it is supposed to not return, because it's the server listening. What approach do you suggest to what I am trying to do here. (I am trying to run my server before running the tests with testem.) – eguneys Aug 24 '14 at 08:58
  • Oh wait I understand ; config/app is a server and you want to test it with testem ? – Jerome WAGNER Aug 24 '14 at 08:58

2 Answers2

3

If you want to use testem to test the "node config/app" server, you cannot use exec.

Exec is supposed to callback when the command is finished so in your case it will never callback.

try with

gulp.task('test', function(cb) {
    var spawn = require('child_process').spawn;

    var proc = spawn('node', ['config/app']);

    var testStarted = false;

    proc.stdout.on('readable', function() {

        if (testStarted) return;

        testStarted = true;

        var testemOptions = {
           file: 'testem.json'
        };

        var t = new testem();

        t.startCI(testemOptions, function() {
            proc.kill()
            cb();
        });

    }
});

Note that I did not test this code and that it probably does not handle all the corner cases you might encounter (if the server stops unexpectedly for example)

you may also want to check the plugin https://github.com/sargentsurg/gulp-testem

Jerome WAGNER
  • 21,986
  • 8
  • 62
  • 77
  • sometimes `readable` event gets called many times, so i tweaked a little, see my edit please, and see if anything looks suspicious. – eguneys Aug 24 '14 at 09:41
  • you are right 'readable' is emitted everytime the stdout stream becomes readable so you need to protect agains this and there was a bug in my original proposal. Your solution is event better if your server has not yet started listening on the first 'readable'. you could also use the 'fork' command and communicate with your server to ask it if it is ready for the tests – Jerome WAGNER Aug 25 '14 at 14:22
0

There is ŧestem plugin on github.

stringparser
  • 735
  • 1
  • 6
  • 16