1"use strict"; 2 3// This does not work as you expect because mutation observer compound microtasks are confusing. 4// Basically you can only use it once per test. 5function queueMicrotaskViaMO(cb) { 6 const observer = new MutationObserver(cb); 7 const node = document.createTextNode(""); 8 observer.observe(node, { characterData: true }); 9 node.data = "foo"; 10} 11 12// Need to use promise_test to get sequential ordering; otherwise the global mutation observer 13// compound microtask business screws us over. 14 15promise_test(() => { 16 return new Promise(resolve => { 17 const happenings = []; 18 19 queueMicrotaskViaMO(() => happenings.push("x")); 20 queueMicrotask(() => happenings.push("a")); 21 22 queueMicrotask(() => { 23 assert_array_equals(happenings, ["x", "a"]); 24 resolve(); 25 }); 26 }); 27}, "It interleaves with MutationObservers as expected"); 28 29promise_test(() => { 30 return new Promise(resolve => { 31 const happenings = []; 32 33 queueMicrotask(() => happenings.push("a")); 34 Promise.reject().catch(() => happenings.push("x")); 35 queueMicrotaskViaMO(() => happenings.push(1)); 36 Promise.resolve().then(() => happenings.push("y")); 37 queueMicrotask(() => happenings.push("b")); 38 queueMicrotask(() => happenings.push("c")); 39 40 queueMicrotask(() => { 41 assert_array_equals(happenings, ["a", "x", 1, "y", "b", "c"]); 42 resolve(); 43 }); 44 }); 45}, "It interleaves with MutationObservers and promises together as expected"); 46