Published
Edited
Jun 3, 2021
Fork of Test Utils
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
catchException = fn => {
try {
fn();
} catch (e) {
return e;
}
throw new Error('No exception triggered!');
}
Insert cell
Insert cell
Insert cell
tests = (fn, specs) => {
let output = html`<div style="padding-bottom: 1px"></div>`;
for (const test of specs) {
const [name, given, expected, format] = test;
let received;
try {
received = typeof format === "function" ? format(fn, given) : fn(given);
} catch (e) {
received = e;
}
const equal = typeof expected === "function"
? expected(received)
: _.isEqual(expected, received)
if (equal) {
// pass
output.append(html`
<details>
<summary>✅ <b>${_.capitalize(name)}</b></summary>

<div style="padding: 1px 10px; margin: 10px 0; background-color: #faf8ed; border-radius: 8px">
<pre><code> Given <br><br>${JSON.stringify(given, null, 2)
.split('\n')
.map(line => `<span style="color: #2a9d8f;"> ${line}</span>`)
.join(
'<br>'
)}</code></pre><hr style="margin: 20px 0 0; backgound: #E7E4D1; padding: 0"/>
<pre><code> Received <br><br>${JSON.stringify(received, null, 2)
.split('\n')
.map(line => `<span style="color: #2a9d8f;"> ${line}</span>`)
.join('<br>')}</code></pre>
</div>

</details>
`);
} else {
// fail
output.append(html`
<details open>
<summary>❌ <b>${_.capitalize(name)}</b><br></summary>

<div style="padding: 1px 10px; margin: 10px 0; background-color: #faf8ed; border-radius: 8px">
<pre><code> Given <br><br>${JSON.stringify(given, null, 2)
.split('\n')
.map(line => `<span style="color: #2a9d8f;"> ${line}</span>`)
.join(
'<br>'
)}</code><hr style="margin: 20px 0 0; backgound: #E7E4D1; padding: 0"/></pre><pre><code>${diff.default(
expected,
received,
{
aColor: text => `<span style="color: #2a9d8f;">${text}</span>`,
bColor: text => `<span style="color: #e76f51;">${text}</span>`,
commonColor: _.identity
}
)}${(typeof expected !== typeof received &&
`<br><br> <span style="color: #e76f51;">${JSON.stringify(
received,
null,
2
)}</span>`) ||
''}</code></pre>
</div>
</details>
`);
}
}
return output;
}
Insert cell
tests(a => "testing", [
["string fail", "test", "test"],
["string pass", "testing", "testing"],
["different type fail", "test", { fail: true }]
])
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