This may be either a Gradle or a JS question, but ...
# javascript
l
This may be either a Gradle or a JS question, but I'm trying here: I have a multiplatform project where the majority of the code is in the common module, and that's where the absolute majority of the tests are. All the JS tests are run inside nodejs. Now, I've been working on a webapplication as part of this project, and I ran into a weird project when I tried to add tests for it: A simple dummy test just gives me this error from gradle:
Copy code
java.lang.IllegalStateException: Errors occurred during launch of browser for testing.
- Firefox
Please make sure that you have installed browsers.
Or change it via
browser {
    testTask {
        useKarma {
            useFirefox()
            useChrome()
            useSafari()
        }
    }
}
I have exactly this in the gradle file (well, I tried that, and then I changed it to just contain
useFirefox()
because that's the only browser I need to test with at the moment. I have read through the Kotlin JS documentation, which doesn't explain anything about how to configure a browser, and I also did some web searches, and got remarkably few hits, and the ones I got were completely irrelevant. Any ideas?
a
Hey 🫡 Just a dumb clarification from my side: you have Firefox installed, right? Is it a regular Firefox or a Nightly one?
e
it uses https://www.npmjs.com/package/karma-firefox-launcher to launch firefox, see that package for how it finds the firefox installation
l
@Artem Kobzar it's regular firefox, installed from Fedora repositories
@ephemient I read the page you linked, but it's not clear at all how it works. It does talk about how to change the binary location, but that's with a configuration file that isn't available from gradle (and I'm not sure why it'd fail anyway, since firefox is available in
/usr/bin/firefox
like on any standard Linux installation)
Looking at the decompilation for
org.jetbrains.kotlin.gradle.targets.js.testing.karma.KotlinKarma
reveals that there is a lot of customisation options that can be used. Are these documented anywhere?
e
you can try
useFirefoxHeadless()
if it's some graphical setup issue
l
That gave me a different problem:
Copy code
Module not found: Error: Can't resolve 'fs' in '/home/elias/prog/array/build/js/packages/array-standalonejs-test/kotlin'
Cannot load browser "FirefoxHeadless": it is not registered! Perhaps you are missing some plugin?
Error: Found 1 load error
e
https://kotlinlang.org/docs/js-project-setup.html#test-task does describe how to add additional Karma config
hmm I dunno then. ChromeHeadless has always just worked out of the box for me
l
I did try to use this to create a custom config, but I had a whole bunch of separate issues with that, but long story short, it parsed the file (because typos in it were reported) but then it refused to allow me to use the new definition.
I very explicitly do not want to use chrome, and I only have it installed in a flatpak, so it doesn't run properly from the commandline.
But when I tried it, it explicitly tells me that I don't have a chrome on the commandline (this is correct) and I should set the
CHROME_BIN
environment variable.
It never said this for the firefox case, which leads me to believe the problem isn't that it can't find the binary.
Wait... The log actually displays the error about "please make sure..." twice, and if I scroll up before the first time it showed up, it shows this:
Copy code
Module not found: Error: Can't resolve 'fs' in '/home/elias/prog/array/build/js/packages/array-standalonejs-test/kotlin'
Cannot load browser "Firefox": it is not registered! Perhaps you are missing some plugin?
That's the same as what I saw in the FirefoxHeadless case.
a
Oh, I see. Do you use
fs
module somewhere?
l
@Artem Kobzar I don't think so. Let me check... I don't explicitly require it, but the js code does this in one place:
var jsFilesystem: dynamic = js("require('fs')")
.
However, this code is not used. It's only actually called when running with node.
I mean, the above is a global declaration, but the function that refers to it is only called from the node tests.
a
Oh, unfortunately webpack goes crazy with it. It tries to find the dependency and because it's only for Node.js it may throw the error to you. What you can try is mark the global like this:
Copy code
@JsModule("fs")
val jsFilesystem: dynamic
So, as soon as you don't use it, it should be not included into your bundle.
l
I see. I'll try that. Thanks a lot!
a
You're welcome. If it doesn't work, please feel free to ping me in the thread
l
@Artem Kobzar I went back to this, made the necessary changes (I apparently needed both
@JsModule
and
@JsNonModule
but even after I made that change I get the same error.
I then changed it to read:
val jsFilesystem: dynamic = null
. This ensures that I'm not touching the fs stuff. The message about fs disappeared, but it didn't get me any closer to solving the problem. Here's the log: https://pastebin.com/cUc9a2aG
In case anyone wants to see it.
a
Oh, sad to hear it. But, if you use it only in tests, does it make sense just to move the property in the test source set?
l
Well, right now I completely removed it altogether, just for testing purposes. And the problem is that the it's still not able to instantiate the firefox test, even though the error about fs disappeared. It leads me to believe that the fs issue was a red herring, and the actual problem is somewhere else.
I'm starting to think that the browser engine tests simply don't work on Linux with Firefox?
a
I don't think so. In this case most of the CI/CD environment wouldn't work for testing with Karma. Could you please run the test task with
--debug
flag and attach the log here or in DM?
l
Thanks. I sent it in a private message.
a
@Ilya Goncharov [JB] have you ever faced such a problem with our setup of the Karma testing?
i
Probably firefox launcher cannot find the browser on the system. You can set environment variable with
FIREFOX_BIN
http://karma-runner.github.io/0.13/config/browsers.html#correct-path-to-browser-binary