make unit tests faster (#241847)

This commit is contained in:
João Moreno
2025-02-25 15:18:20 +01:00
committed by GitHub
parent 649a74e7d8
commit ae43cc72b6
6 changed files with 340 additions and 248 deletions

View File

@@ -34,6 +34,46 @@
const urlParams = new URLSearchParams(window.location.search);
const isCI = urlParams.get('ci');
const $globalThis = globalThis;
const setTimeout0IsFaster = (typeof $globalThis.postMessage === 'function' && !$globalThis.importScripts);
/**
* See https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#:~:text=than%204%2C%20then-,set%20timeout%20to%204,-.
*
* Works similarly to `setTimeout(0)` but doesn't suffer from the 4ms artificial delay
* that browsers set when the nesting level is > 5.
*/
const setTimeout0 = (() => {
if (setTimeout0IsFaster) {
const pending = [];
$globalThis.addEventListener('message', (e) => {
if (e.data && e.data.vscodeScheduleAsyncWork) {
for (let i = 0, len = pending.length; i < len; i++) {
const candidate = pending[i];
if (candidate.id === e.data.vscodeScheduleAsyncWork) {
pending.splice(i, 1);
candidate.callback();
return;
}
}
}
});
let lastId = 0;
return (callback) => {
const myId = ++lastId;
pending.push({
id: myId,
callback: callback
});
$globalThis.postMessage({ vscodeScheduleAsyncWork: myId }, '*');
};
}
return (callback) => setTimeout(callback);
})();
Mocha.Runner.immediately = setTimeout0;
mocha.setup({
ui: 'tdd',
timeout: isCI ? 30000 : 5000