Karma: Tests start executing before page is loaded

Created on 14 May 2015  路  9Comments  路  Source: karma-runner/karma

I am using karma-jasmine: 0.3.5. I have tests that run perfectly from a HTML runner that loads jasmine 2.2 but when I run them through karma using karma-jasmine they fail. I have investigated the problem and it seems that the tests start executing before the DOM is ready (which is not the case when run from a HTML runner). After updating the createStartFn function in the adapter.js file to the code below which waits for the DOM ready the tests started passsing.

function createStartFn(karma, jasmineEnv) {
  // This function will be assigned to `window.__karma__.start`:
  return function () {
    $(function () {
      jasmineEnv = jasmineEnv || window.jasmine.getEnv();

      jasmineEnv.addReporter(new KarmaReporter(karma, jasmineEnv));
      jasmineEnv.execute();
    });
  };
}

Look at the discussion about the issue here

discuss

Most helpful comment

Guys should we wait event DOMContentLoaded or load before starting tests?

\cc @karma-runner/contributors

All 9 comments

Guys should we wait event DOMContentLoaded or load before starting tests?

\cc @karma-runner/contributors

Yes please :-) :+1:

Yes please

+1

Or karma can be configured to not start automatically.

+1

+1

+1

Have you tried to add something like

beforeAll((done) => {
   document.addEventListener("DOMContentLoaded", function(event) {
    console.log("DOM fully loaded and parsed");
    done();
  });
});

Also: if you are using an asynchronous framework like angular, then you need to schedule your tests after the framework initializes, which of course is framework dependent (not DOM load dependent really).

Was this page helpful?
0 / 5 - 0 ratings