COMMUNITY

[tink_unittest] Recipe for browser testing?

For testing my backend apps, I’m using Tink UnitTest. For my frontend apps, I also want to use Tink UnitTest, but I don’t know how to do, so currently I’m using the combination Karma + Mocha + UTest.

Does anyone know how to use Tink UnitTest for browser testing? Or, at least, does anyone know how to turn test failures into exceptions under Tink UnitTest ?

@kevinresol @back2dos

The simplest way is to assert a Boolean value as shown in the doc Tinkerbell Unit Testing

For more advanced use, you can also check out the other ways to construct an assertion instance here Tinkerbell Test Runner

Hmm. I do use tink_unittest for some browser testing (although for libraries as opposed to applications), so in essence it’s fair to say "it works for me"™. For running in the browser, the tests are run through travix, which runs them on node+puppeteer, i.e. directly in chromium (as opposed to a web driver).

Could you perhaps provide a sample test, representative of the things you struggle to port?

@kevinresol I don’t have any issue using Tink UnitTest/TestRunner in itself. What I’m trying to achieve is the same kind of functionality than Karma: automate browser testing and have a report of failures/successes on the command line, and a proper exit code (like what is already done when using the test runner on the CLI).
@back2dos I’ll take a look at travix code for inspiration.

PS: thank you to both of you for your amazing work on Coconut/Tinkerbell and other Haxe libs :+1:

1 Like

"It also works for me"™ :partying_face:

Two small changes: I’m using Playwright instead of Puppeteer (because it supports more browsers and has nearly the same API), and I needed to force the use of the AnsiFormatter (because otherwise I lose the output colorization).

File tests.hxml:

--class-path src
--class-path test
--js build/tests.js
--library tink_unittest
--main RunTests
--cmd node runner.js

File RunTests.hx:

import tink.testrunner.Reporter.AnsiFormatter;
import tink.testrunner.Reporter.BasicReporter;
import tink.testrunner.Runner;
import tink.unit.TestBatch;

function main() {
	final tests = TestBatch.make([/*...*/]);
	ANSI.stripIfUnavailable = false;
	Runner.run(tests, new BasicReporter(new AnsiFormatter()))
		.handle(result -> js.Syntax.code("exit({0})", result.summary().failures.length));
}

File runner.js:

const {firefox} = require("playwright");

(async () => {
	const browser = await firefox.launch();
	const page = await browser.newPage();
	page.on("console", message => console.log(message.text()));
	page.on("pageerror", error => console.error(error));

	await page.exposeFunction("exit", code => {
		process.exitCode = code;
		return browser.close();
	});

	await page.evaluate(() => console.log(navigator.userAgent));
	await page.addScriptTag({path: "build/tests.js"});
})();

Issue solved!

1 Like