javascriptnode.jsphantomjscasperjsspookyjs

Understanding Scopes in Spooky JS


This implementation of SpookyJS is really spooky. While using Gulp to run Mocha + SpookyJS tests, I am unable to see most console log output. I have been following the Quickstart steps on SpookyJS's github page. Why can't I see these console log outputs?

describe('test', function () {
it('test 1', function(done){

    try {
        var Spooky = require('spooky');
    } catch (e) {
        var Spooky = require('../lib/spooky');
    }
    var spooky = new Spooky({
            child: {
                transport: 'http'
            },
            casper: {
                logLevel: 'debug',
                verbose: true
            }
        }, function (err) {
            if (err) {
                e = new Error('Failed to initialize SpookyJS');
                e.details = err;
                throw e;
            }

            spooky.start(URL);
            console.log('Hello 3');  //currently this is not printing anything to the console
            spooky.then(function () {                
                 this.emit('hello', 'Hello, from ' + this.evaluate(function () {
                 return document.title;
                 }));                  
            });
            spooky.run();
            console.log('Hello 1');  //currently this is not printing anything to the console
        });
        spooky.on('hello', function (line) {
           console.log(line);
        });
        spooky.on('console', function (line) {
           console.log(line);
        });
        spooky.on('error', function (e, stack) {
            console.error(e);

            if (stack) {
                console.log(stack);
            }
        });

    console.log('Hello 2');  //this is printing to the console
    done();

    });
});

Solution

  • Simplifying your code, it roughly looks like this:

    describe('test', function () {
        it('test 1', function(done){
            var Spooky = require('spooky');
            var spooky = create a spooky object with a lot of logic in it;
    
            console.log('Hello 2');  //this is printing to the console
            done();
        });
    });
    

    The spooky object is created, and after that, the program flow simply continues. Anything that the spooky object does, it does asynchronously, later on.

    After creating the spooky object, you get your Hello 2 and then done() is called, which ends this test.

    So, before any hello stuff can be processed, your tests ends already.

    What might help is to move the done() line here:

    spooky.on('hello', function (line) {
        console.log(line);
        done();
    });
    

    Then your test will end once the hello event has been caught.

    Since I'm not familiar with Spooky or with what exactly you want to test, I'm afraid I can't be of any more help. Hopefully this gets you a step further.