I’m trying to read resource files during unit test...
# multiplatform
j
I’m trying to read resource files during unit tests. I have an
except
function like so :
Copy code
internal expect fun readResourceFile(resourceName: String): ByteArray
This is the jvm implementation :
Copy code
actual fun readResourceFile(resourceName: String) =
    File("./src/commonTest/resources/$resourceName").readBytes()
I wonder how I’m supposed to implement it for my js target? I tried to use this :
Copy code
js("require('../../../resources/main/$resourceName')")
but it complains that the string must be a constant. And the return type is
dynamic
which doesn’t help neither
h
Do you target node.JS? Then use the file/io api. Otherwise, you can't load a file in the browser (except the experimental localfile API) If you want to refer to kotlin resources from the browser, you need to refer them statically (otherwise webpack will remove them) and add some map to fetch them dynamically
j
I’m targeting the browser. Maybe I need something like this to be sure they are not removed?
Copy code
tasks.register<Copy>("copyJsTestResources") {
    from("src/commonTest/resources")
    into("build/bin/iosX64/debugTest/resources") // this is for ios, what would be the equivalent for js?
}

tasks.findByName("jsTest")!!.dependsOn("copyJsTestResources")