Public
Edited
Mar 18, 2023
Importers
Insert cell
Insert cell
Insert cell
describe(`Test Demo`, async () => {
it(`should work`, async () => {
expect(1 + 2).toEqual(3);
});
it(`should fail`, async () => {
expect(2 + 2).toEqual(5);
});
it(`should work with async operations as well`, async () => {
expect(1 + 1).toEqual(2);
await Promises.delay(100);
expect(1 + 2).toEqual(3);
});
})
Insert cell
expect = require("jest-expect-standalone/dist/expect.min.js").catch(
(err) => window.expect
)
Insert cell
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) // Wait until the "div" element for reports is attached to the DOM
.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;
}
Insert cell
async function it(title, action) {
await describe.it(title, action);
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more