title: How to test opening new tab with Jasmine in Chrome extension?
body: <p>I'm building chrome extension and I'm having some trouble getting tests to work the way I'd like them to.  I access this page by simply going to extension url ending tests.html.</p>
  
  <p>Let's say I want to test the following very simple scenario: new tab is opened in chrome and it is loaded, this event is detected by my extension, new tab is added to array of tabs within my apps. I have all my Jasmine tests in a separate extension page.</p>
  
  <p>I tried the following things, and they all fail. </p>
  
  <p>One, trying to call Jasmine "expect" with some Timeout (needed for loading the page).  </p>
  
  <pre><code>describe("Tab collection", function () {
      it("after opening new tab it should be in app.tabs", function () { 
          chrome.tabs.create({"url":"http://www.google.com"});
          setTimeout(function () {
              expect(app.tabs.length).toBe(1);
              console.log("app.tabs.length after timeout", app.tabs.length);
          },100);
      });
  </code></pre>
  
  <p>Console.log does not log anything, tests is green, even tough clearly it wasn't run. </p>
  
  <p>Two, trying to run 'expect' in chrome.tabs.create callback function.  </p>
  
  <pre><code>    it("after opening new tab it shoule be in app.tabs", function () {
          chrome.tabs.create({"url":"http://www.google.com"}, function () {
              expect(app.tabs.length).toBe(1); //
              console.log("app.tabs.length in callback to chrome.tabs.create",app.tabs.length); 
          })
      });
  });
  </code></pre>
  
  <p>Console.log logs 0, but for some reasons test passes. Why it passes if app.tabs.length is 0?</p>
  
  <p>Third scenario involves running all my tests after opening new chrome tab. Something along the lines</p>
  
  <pre><code>chrome.tabs.open({some_url},function (tab) {
      execJasmine()
      chrome.tabs.remove(tab['id'])
  })
  </code></pre>
  
  <p>this works, but is extremely inconvenient, all my tests now run with this open tab. It doesn't really allow me to test things. </p>
  
  <p>Fourth I tried to do this with Jasmine BeforeEach</p>
  
  <pre><code>beforeEach(function () {
      chrome.tabs.create(some_url);
  }); 
  </code></pre>
  
  <p>This is closest to being ok, but now the tests fails, app.tabs.length is 0, because the page is not yet loaded when the test suite is called.</p>
  
  <p>Has anyone encountered simlar problems? How can I test described scenario?</p>