I'm trying to create a test to read a .txt file in...
# webassembly
s
I'm trying to create a test to read a .txt file in WebAssembly, but I'm getting an error: <NOT FOUND>. I added test.txt in wasmJsTest/resources/test.txt. After running this test, I'm getting the following error: AssertionError: Expected <ABCDEF>, but got <NOT FOUND>. Here is test code:
Copy code
@Test
fun testFileRead() = runTest(timeout = 60.seconds) {
    val path = "test.txt"
    val result: Response = window.fetch(path).await()
    val body = result.text().await<JsString>().toString()
    assertEquals("ABCDEF", body)
}
a
Seems like you should start some dummy server to be able to read the file. Or, if you are running your tests not on browser but using nodejs (it's quite popular to use this option), you could also try to read it using the
fs
module, like this:
Copy code
// NodeFileSystem.kt

// This is the simplest way

@JsModule("node:fs")
external object FileSystem {
  fun readFileSync(path: String): JsAny // don't use it directly, it returns Buffer, by default
}

fun FileSystem.readText(path: String) = 
  readFileSync(path).toString() 

// If you need to make it async 
@JsModule("node:fs/promises")
external object FileSystem {
  fun readFile(path: String): Promise<JsAny> // don't use it directly, it returns Buffer, by default
}

suspend fun FileSystem.readText(path: String): String {
  val buffer: JsAny = readFile(path).await()
  return buffer.toString()
}

// Your Test Files
@Test
fun testFileRead() = runTest(timeout = 60.seconds) {
    val path = "test.txt"
    val body = FileSystem.readText(path)
    assertEquals("ABCDEF", body)
}