25 Oct 2011, 14:54
Jasmine, Chrome, and Access-Control-Allow-OriginI recently updated a project I’ve been working on from jQuery 1.4 to jQuery 1.6.4 and ran the suite of Javascript unit tests (written in Jasmine) associated with it. It took me quite a while and lots of digging and debugging before I even noticed that Chrome’s console had logged a few errors. One of which was:
Origin null is not allowed by Access-Control-Allow-Origin when trying to call loadFixtures();
Well of course this would be responsible for all of my jQuery selectors coming back empty for elements that I knew existed; Jasmine couldn’t load the fixture so there weren’t any elements to actually select.
As it turns out, this issue is isolated Chrome’s behavior and how it deals with accessing local files. You’ll see similar behavior when trying to fire off AJAX requests in some situations. You can work around this problem for local debugging by calling Chrome as chrome.exe --allow-file-access-from-files
which will disable that access control. After launching Chrome with that parameter, running the test suite passed (as it should) and all was happy!