Karma with Jasmine and script.onload

455 views
Skip to first unread message

Imre Mehesz

unread,
Nov 17, 2016, 10:15:12 AM11/17/16
to karma-users
hello,

in a nutshell. I have an app (A) that I load in other "host apps" (B and C). These apps are all independent, and working fine normally, but unfortunately I can't get testing to work properly.

When A loads into B, it creates a DIV in the page with a specific ID (I can see this happening with Karma if I use the "Chrome" browser configuration, I can see that it creates that DIV on the page, however, the tests fail with :
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

Here is my somewhat pseudo karma/jasmine code:

describe("App A should be loaded", () => {

          const host = "http://127.0.0.1:8080";
          const appDomId = "APP-A-TEST-ID";

  const link = document.createElement( "link" );
  link.href = host + "/apps/a/init.css";
  link.type = "text/css";
  link.rel = "stylesheet";
  link.media = "screen,print";
  document.getElementsByTagName( "head" )[0].appendChild( link );

  const script = document.createElement('script');
  script.src = host + "/apps/a/init.js"; // CONTAINS APP_A
  document.head.appendChild(script);

          beforeEach((done) => {
            script.onload = () => {
              $("body").append("<div id='" + appDomId + "'></div>");

        APP_A.setOptions({
          host: host,
          user: "blahblah",
          domId: appDomId,
          onReady: function() {
                  console.log("WOW! I can't believe we made it!"); // THIS RUNS WHEN APP A IS COMPLETELY LOADED (IT WORKS, I CAN SEE THE LOG IN PHANTOM AND IN CHROME)
                  done();
                }
              });
          };

          // THIS FAILS WITH THE MESSAGE DESCRIBED ABOVE
          it("should be 1", (done) => expect(1).toBe(1));
});


any help would be appreciated ....

thanks,
--im

Mark Trostler

unread,
Nov 17, 2016, 12:18:45 PM11/17/16
to karma-users
You're not calling done() in your async test?
--
You received this message because you are subscribed to the Google Groups "karma-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to karma-users...@googlegroups.com.
To post to this group, send email to karma...@googlegroups.com.
Visit this group at https://groups.google.com/group/karma-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/karma-users/f838a386-4ecb-4ec1-b657-563f59be0c6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Imre Mehesz

unread,
Nov 18, 2016, 4:47:15 PM11/18/16
to karma-users
I updated my test with this:

    it("should be 1", (done) => {
      expect(1).toBe(1);
      done();
    });

now this made it work in Chrome, but still fails in PhantomJS with the same error ... (i'm still not understanding why it half-works the way it does)

--im

Mark Trostler

unread,
Nov 18, 2016, 4:54:33 PM11/18/16
to karma...@googlegroups.com
Don't know why it's failing in phantom but if you accept a 'done' function in your test then you have to call it within within a certain timeframe or jasmine will timeout and fail your test.  
That timeframe is define by jasmine.DEFAULT_TIMEOUT_INTERVAL (which is 5000ms unless you change it).


To unsubscribe from this group and stop receiving emails from it, send an email to karma-users+unsubscribe@googlegroups.com.

To post to this group, send email to karma...@googlegroups.com.
Visit this group at https://groups.google.com/group/karma-users.
Reply all
Reply to author
Forward
0 new messages