eval-genius
Version:
eval-genius enables evals of arbitrary async code. It is generally intended for making multiple assertions on outputs which are generated nondeterministically. These assertions can be used to score algorithms on their effectiveness.
65 lines (64 loc) • 1.54 kB
JavaScript
const p = (n) => typeof n == "function" ? n() : n, M = async ({
vitest: n,
concurrent: l,
metadata: a,
data: w,
task: o,
exporters: x
}) => {
const h = [
"run",
Date.now(),
a.label,
Math.floor(Math.random() * 1e5)
].join("-"), y = await p(o.renderer.fields), r = [];
for (const e of x)
r.push(
await e().start({
title: a.name,
fields: ["runId", ...y]
})
);
const s = await p(w.values), c = {};
s.forEach(({ name: e }) => {
const t = c[e] || 0;
c[e] = t + 1;
}), Object.entries(c).forEach(([e, t]) => {
if (t > 1)
throw new Error(
`[name="${e}"] was found [${t}] times in data. Each key should be unique.`
);
});
let i = s.length;
const E = async () => {
if (i--, i === 0)
for (const e of r) await e.flush();
}, { test: b, expect: g } = l ? {
test: n.test.concurrent,
expect: new Proxy(n.expect, {
get: (e, t) => t === "soft" ? e : e[t]
})
} : { test: n.test, expect: n.expect };
for (const { name: e, input: t, expected: f, only: m } of s)
b(e, { only: m }, async ({ onTestFinished: j }) => {
j(E);
const u = await o.execute(t), d = await o.renderer.render({
input: t,
output: u,
expected: f
}), q = o.test(g, {
input: t,
output: u,
expected: f,
rendered: d
});
for (const I of r)
await I.report({
result: { runId: h, ...d }
});
await q;
});
};
export {
M as genius
};