Sabeeh
05/20/2024, 7:25 PM@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)
}
Artem Kobzar
05/27/2024, 1:39 PMfs
module, like this:
// 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)
}