Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reporters API #238

Open
epszaw opened this issue Aug 18, 2023 · 2 comments
Open

Reporters API #238

epszaw opened this issue Aug 18, 2023 · 2 comments

Comments

@epszaw
Copy link

epszaw commented Aug 18, 2023

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
Copy link

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
Copy link

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants