如何在 qUnit 中为单个测试添加临时 css?

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

我有一个正在测试的组件,它根据站点的当前 css 表现不同。我想为单个测试添加一个 css。是否可以在不创建文件的情况下添加它,例如仅用于测试的临时 css?

是否可以在 qUnit 测试函数中添加 css 代码?

您可以使用所需的 CSS 规则动态创建 style 节点。

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