describe = {
const formatMessage = (msg, color) =>
html`<div style="font: var(--mono_fonts); color: ${color};">${msg}</div>`;
let describePromise = Promise.resolve();
let itPromise = Promise.resolve();
let report, counter;
async function* describe(title, action) {
let prev = describePromise;
let start,
startPromise = new Promise((y) => (start = y));
describePromise = describePromise
.then(() => startPromise)
.then(action)
.catch((err) => {})
.then(() => itPromise);
await prev;
counter = 0;
let container = html`
<div>
${title ? html`<h3 style="color:gray;">${title}</h3>` : ""}
</div>`;
report = html`<div></div>`;
container.appendChild(report);
yield container;
start();
}
describe.it = (title, action) => {
const id = ++counter;
itPromise = itPromise.then(async () => {
try {
await action();
report.appendChild(
formatMessage(`[${id}] Success: ${title || "test passed"}`, "green")
);
} catch (error) {
const message = (error + "")
.split("\n")
.map((_) => ` > ${_}`)
.join("\n");
report.appendChild(
formatMessage(
html`[${id}] Failure: ${
title || "test failed"
}<pre>${message}</pre>`,
"red"
)
);
}
});
};
return describe;
}