Dave Stein
Dave Stein

Reputation: 9316

How do I reliably execute Jasmine tests that utilize requirejs via phantomjs?

I am using phantomjs to run jasmine test. My jasmine tests are using require around the describe blocks to ensure all the right modules are loaded.

My tests would not run because page.evaluate -> jasmine.getEnv().execute(); runs BEFORE requirejs finishes loading the modules.

I was wondering if anyone knows a real good way around this. I have an answer I'm going to post below but would love to compare notes via other answers. If yours is better, I will def pick it as answer obviously :)

Upvotes: 4

Views: 2684

Answers (2)

Geoff Gerrietts
Geoff Gerrietts

Reputation: 676

I did something a little different -- my HTML page has a function, wrapped in a require() call:

var run_tests = function (specfiles) {
    require(specfiles, function () {
        jasmine.getEnv().execute();
    });
};

Then I page.evaluate( function (test_specs) { run_tests(test_specs) }, ['test1.spec', 'test2.spec']);

Upvotes: 3

Dave Stein
Dave Stein

Reputation: 9316

My solution, with jQuery available, is this:

Load a config file before any of your tests run.

var jasmine_deferreds = [];

// Setup an event to fire on the document
// I actually did this with native code rather than jquery because
// I wanted to minimize jquery usage

// ....

// setTimeout so all files loaded after this will finish registering their requires
setTimeout( function() {

  $.when.apply( null, jasmine_deferreds ).then( function() {

    // Fire event that was created

  });

}, 5 );

It's up to you how you want to build up the array of deferreds and then resolve them. I essentially pushed to the array and then resolved when the require was done. I wrapped require with my own version that would know to resolve it upon completion automatically - so I don't need to push and resolve manually in each test.

Then in my phantom file I do this:

page.evaluate ->
    mylistener = ( document ) -> jasmine.getEnv().execute();
    document.addEventListener( 'test_ready_event', mylistener, false);

This makes it so I know all my require modules are loaded without having some arbitrary setTimeout that could be too short once I am loading too many files. The one setTimeout I'm using is safe because it's only being used to fire after main callstack is done. It doesn't really care about the time.

Upvotes: 2

Related Questions