Jeanluca Scaljeri
Jeanluca Scaljeri

Reputation: 29129

jasmine unit tests not executed in phantomjs script

I've written a small script which loads my SpecRunner.html (jasmine unit tests) from disk. Although everything looks fine, the tests are not executed. Here is my code:

var page = require('webpage').create()
    , file = "./SpecRunnerCoverage.html" ;

page.open(file, function (status) {
    var json = page.evaluate(function () {
        return window._yuitest_coverage;
    });

    console.log(JSON.stringify(json));
    phantom.exit();
});

It is executed as follows:

$> phantomjs myScript

From the code coverage data I can tell no test was executed. Is there anyway I can execute these tests ?

Thanks a lot

UPDATE: I just noticed that when I wrap a setTimeout around the evaluate function and delay the whole process, it works. Is there a better solution than a setTimeout ?

Upvotes: 0

Views: 151

Answers (1)

Gregg
Gregg

Reputation: 2638

The jasmine boot process starts running the tests when window.onload is called, so your specs won't necessarily have finished by the time the phantom page load callback is invoked. By using a setTimeout, you're allowing time for the tests to run in the browser after the page loads. The setTimeout can still be fragile because as you add more specs, the time it takes to run your full suite will change and your current timeout duration may not be long enough to allow the suite to finish.

If your project can have ruby dependencies, you can use the jasmine ruby gem. The gem includes a rake task rake jasmine:ci that will start up a server with your tests and use phantom to load the page and wait for all of the specs to complete and print failures out to the command line.

Upvotes: 1

Related Questions