Search code examples
javascriptcsstestingqunit

How to add temporary css for a single test in qUnit?


I have a component under test that behaves differently based on a current css of the site. I would like to add a css for a single test. Is it possible to add it without creating a file, like a temporary css for the test only?

Is it possible to add a css code in the qUnit test function?


Solution

  • You can create style node dynamically with desired CSS rule.

    var tempCSS = document.createElement("style");
    tempCSS.type = "text/css";
    tempCSS.innerHTML = ".myClass { background-color: red; }";
    document.body.appendChild(tempCSS);
    <div class="myClass">yahoooooooo</div>