Javascript 突变观察员未能通过单元测试,但我没有';我不知道为什么
我有一个突变观察函数:Javascript 突变观察员未能通过单元测试,但我没有';我不知道为什么,javascript,unit-testing,dom,Javascript,Unit Testing,Dom,我有一个突变观察函数: function trackItemsSelected(elementId, Event) { const targetNode = document.getElementById(elementId); const config = { attributes: true, childList: true, subtree: true }; var removedItemCou
function trackItemsSelected(elementId, Event) {
const targetNode = document.getElementById(elementId);
const config = { attributes: true, childList: true, subtree: true };
var removedItemCount = 0;
const callback = function (mutationsList) {
for (var mutation of mutationsList) {
if (mutation.type === 'childList') {
Event("item added");
}
if (mutation.type === 'attributes') {
removedItemCount += 1;
}
}
if (removedItemCount > 0) {
Event("item removed");
removedItemCount = 0;
}
};
const observer = new MutationObserver(callback);
observer.observe(targetNode, config);
}
我的qunit测试失败,错误为“预期”在第136行(这是这一行:for(mutationsList的var突变){
但是,我看不到我在哪里丢失了任何东西或任何不正确的语法?Visual Studio中也没有显示任何错误。有人能看到我在该函数中丢失的错误是否可能导致单元测试失败吗?看起来运行这些测试的任何工具都不理解ES2015+语法。 ES2015中增加了for of
循环
您可以将其切换为for
循环:
for (var i = 0, len = mutationsList.length; i < len; ++i) {
var mutation = mutations[i];
// ...
}
要了解更多备选方案,请查看,其中包含了各种阵列循环选项-请确保忽略ES2015+选项
mutationsList.forEach(function(mutation) {
// ...
});