I'm using Karma + Jasmine to test my AngularJS directives, I wrote more than 300 tests and I was very happy... until I found an issue that taken me here because I'm stuck: some tests are failing because they need a CSS applied to some elements (a piece of code in my directive does a size computation based on this style), and despite I added the file containing the CSS implementation, this file seems ignored during tests. In my karma config I added the css file in this way:
files: [
// ..
'styles/foo.css',
// ..
],
the settings above generates a link tag in the body rather than head, so I tried to inject the CSS "manually" using js:
angular.element(document).find('head').prepend(
'<style type="text/css">@charset "UTF-8";' +
'/* THE STYLE I NEED FOR MY TESTS HERE */' +
'</style>'
);
but neither this approach seems to work (I tested and the style tag gets injected correctly).
I finally tried to add a block of style among the html that I compile using angular $compile:
var element = ng.element('<div>' +
'<style type="text/css">@charset "UTF-8";' +
'/* THE STYLE I NEED FOR MY TESTS HERE */' +
'</style>' +
'<my-directive></my-directive>' +
+ '</div>');
$compile(element)(scope);
...but still no lucky... I also tried to switch the test runner from PhantomJS to Chrome, but the problem is the same: styles are somehow ignored (they are injected but they don't get applied to elements, in fact using jQuery(targetElement).css('width') it returns 0)...
so, my big question: how should I import and successfully apply stylesheet in karma tests??? :(