I'm working on a custom test runner for Kotlin/JS ...
# javascript
a
I'm working on a custom test runner for Kotlin/JS tests. How can I get the compiled .js files? What Gradle task do I run, and where is the output directory?
e
The JS compilation task depends on the binary type and the target type. For example, if you're targeting the
browser()
and
binaries.library()
, look for
jsBrowserDevelopmentLibraryDistribution
The outputted JS files are under
build/compileSync/*
if I'm not mistaken.
You can infer the correct task by looking at the task chain when running the built-in test runner
But you're probably better off extending the existing test runner task.
a
for context, I'm trying to update Kotest so that it can support nested JS tests (and that's pulled a thread that has unravelled the whole thing, so it looks like the only way is to implement it from scratch!)
okay thanks, I see the files inside
build/compileSync/js/test/testDevelopmentExecutable/kotlin
. However, I am a JS noob. How can I load these files inside of another .js file?
I've got a
fun main() {}
inside
src/jsTest/kotlin/testMain.kt
. It will log some stuff to the browser console, and I want to be able to run the
main()
from inside a custom
index.html
e
Ahhh we are entering in a topic that requires some time to answer. But if you're trying to support JS in Kotest (wasn't it supported already?) then you have to cover all the compilations type: browser, Node, CJS, ESM, library, executable
a
at the moment I'm just focusing on browser testing
e
I don't fully understand why nested tests would be an issue with Kotest now
a
Kotest supports JS, but not nested tests
well... basically, no JS test frameworks supports async registration of nested tests (e.g. Jasmine supports async tests, but not async describes). But Kotest requires async test-containers.
and there's no runBlocking in JS, so it can't really be worked around
e
And how does Kotest work now? Like, is it backed by a JS test framework or is it done from scratch via Kotlin?
a
right now, it uses externals to use the Jasmine `it`/`describe` methods.
just like kotlin-test
e
What about Mocha? Did you check if it supports what you need?
a
it does not :(
e
Then I'm wondering, if they don't support that behavior, is there a reason or it's just unexplored territory?
a
even if they did, it'd still be tricky to report the test events. When running tests with KGP, it wires in a listener for the Jasmine/Karma test events, to report the events to KGP over stdout. And then KGP listens to stdout, parses the events, and uses some Gradle internals (which is naughty) to report the test events to Gradle.
from what I can tell, it's a bit of a legacy situation. Jasmine requires that all tests are registered before starting execution, and changing that would be a big change in architecture that doesn't seem necessary.
'legacy' is the wrong word. Synchronous test registration was a reasonable requirement, and it wasn't wrong, but it does restrict the possibilities now.
e
So you target right now is to re-implement an entire test runner, just via Kotlin?
a
basically, yes
although Kotest is already a pure-Kotlin test runner
I can use Ktor to host the compiled Kotlin .js code, and Playwright to launch the browser.
I 'just' need to figure out how to run the compiled Kotlin .js code in a web browser.
Then Kotest can report the test events over stdout, and then I've got a Gradle task that can report the test events to IntelliJ. And Kotest has a test report generator.
So, I've written a basic index.html that loads the scripts that are generated into
build/compileSync/js/test/testDevelopmentExecutable/kotlin
.
Copy code
<html>
<head><title>KotestJSTestEngine</title></head>
<body>
    <script src="/src/kotlin-js-wasm-testing-v3.js"></script>
    <script src="/src/kotlin-js-wasm-testing-v3-test.js"></script>
</body>
</html>
However, I now run into the same problem I've had before in other situations, where I need to manually load all of the dependencies...
Copy code
Uncaught Error: Error loading module 'kotlin-js-wasm-testing-v3'. Its dependency 'kotlin-kotlin-stdlib' was not found. Please, check whether 'kotlin-kotlin-stdlib' is loaded prior to 'kotlin-js-wasm-testing-v3'.
There's supposed to be an all-in-one .js file that loads everything...
if I run
gradle build
then I can see that all-in-one file for the main source set
build/dist/js/productionExecutable/v3.js
(v3 is the name of the Gradle subproject)`. But not for the test sources.
I have a very very very rough implementation of a custom Kotlin/JS browser test runner, if you'd like to take a look: https://github.com/aSemy/kotlin-js-wasm-testing/blob/5613b0ea2005cfafec11782b96c142b78391327e/v3/src/jsTest/kotlin/tests.kt It's in the v3 subproject. Run
gradle :v3:jsTestKotest
, and it should run the tests, and report some of the results to IntelliJ.