lukeed / uvu

uvu is an extremely fast and lightweight test runner for Node.js and the browser
MIT License
2.98k stars 99 forks source link

Reporters API #238

Open epszaw opened 1 year ago

epszaw commented 1 year ago

Hey, @lukeed!

I'm ready to develop reporters API for uvu (#227) to be able to use Allure integrations with the test runner. Even outside of allure context, universal reporters API could be very useful for users (for example, it's possible to export test results as JSON #52 using custom report).

What do you think about this?

wf9a5m75 commented 1 year ago

Ah, I'm just looking for it. Test report is important factor for DevOps. That's the evidence for what tests did and what tests didn't.

mohamed-ait-brahim-nw commented 6 months ago

Hey @lukeed, this is a great simple test runner, it would be great if we can have support for custom reporters. Custom reporters will allow others to extend the use cases to integrate with other services.

Some existing example reporters APIs:

We can start with a simple API, I propose to refactor current implementation to use a basic reporter.

diff --git a/src/index.js b/src/index.js
index ced88a8..c02523b 100644
--- a/src/index.js
+++ b/src/index.js
@@ -62,13 +62,26 @@ function format(name, err, suite = '') {
    return str + '\n';
 }

+const basicReporter = {
+   onBegin(suite) {
+       write(SUITE(kleur.black(` ${suite} `)) + ' ')
+   },
+   onTestPass(suite,test){write(PASS +`[${suite}] - ${test}`)},
+   onTestFail(suite,test,err){write(FAIL + `[${suite}] - ${test}` + '\n' + format(test, err, suite))},
+   onEnd(suite,report){
+       const {num,total,errors} = report;
+       let msg = `[${suite}] (${num} / ${total})\n`;
+       write(errors.length ? kleur.red(msg) : kleur.green(msg));
+   }
+}
+
 async function runner(ctx, name) {
-   let { only, tests, before, after, bEach, aEach, state } = ctx;
+   let { only, tests, before, after, bEach, aEach, state,reporter } = ctx;
    let hook, test, arr = only.length ? only : tests;
    let num=0, errors='', total=arr.length;

    try {
-       if (name) write(SUITE(kleur.black(` ${name} `)) + ' ');
+       if (name) reporter.onBegin(name);
        for (hook of before) await hook(state);

        for (test of arr) {
@@ -77,21 +90,20 @@ async function runner(ctx, name) {
                for (hook of bEach) await hook(state);
                await test.handler(state);
                for (hook of aEach) await hook(state);
-               write(PASS);
+               reporter.onTestPass(name, test.name)
                num++;
            } catch (err) {
                for (hook of aEach) await hook(state);
                if (errors.length) errors += '\n';
                errors += format(test.name, err, name);
-               write(FAIL);
+               reporter.onTestFail(name, test.name, err)
            }
        }
    } finally {
        state.__test__ = '';
        for (hook of after) await hook(state);
-       let msg = `  (${num} / ${total})\n`;
        let skipped = (only.length ? tests.length : 0) + ctx.skips;
-       write(errors.length ? kleur.red(msg) : kleur.green(msg));
+       reporter.onEnd(name, { num, total, errors, skipped });
        return [errors || true, num, skipped, total];
    }
 }
@@ -112,8 +124,8 @@ function setup(ctx, name = '') {
    test.after.each = hook(ctx, 'aEach');
    test.only = into(ctx, 'only');
    test.skip = () => { ctx.skips++ };
-   test.run = () => {
-       let copy = { ...ctx };
+   test.run = ({reporter}={reporter:basicReporter}) => {
+       let copy = { ...ctx,reporter };
        let run = runner.bind(0, copy, name);
        Object.assign(ctx, context(copy.state));
        UVU_QUEUE[globalThis.UVU_INDEX || 0].push(run);