8000 Actual tests for `js.import`. · sueka/scala-js@d8ca770 · GitHub
[go: up one dir, main page]

Skip to content < 8000 react-partial partial-name="keyboard-shortcuts-dialog" data-ssr="false" data-attempted-ssr="false" data-react-profiling="false" >

Commit d8ca770

Browse files
committed
Actual tests for js.import.
This is a followup to 17407e7 with actual tests for the asynchronous result of `js.import`, now that we can use async tests.
1 parent 3f7a4a2 commit d8ca770

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

project/Build.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,7 @@ object Build {
16241624

16251625
inConfig(Bootstrap)(testSuiteBootstrapSetting)
16261626
).withScalaJSCompiler.withScalaJSJUnitPlugin.dependsOn(
1627-
library, jUnitRuntime, testBridge % "test"
1627+
library, jUnitRuntime, testBridge % "test", jUnitAsyncJS % "test"
16281628
)
16291629

16301630
lazy val testSuiteJVM: Project = (project in file("test-suite/jvm")).settings(

test-suite/js/src/test/require-dynamic-import/org/scalajs/testsuite/jsinterop/DynamicImportTest.scala

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,35 @@ import scala.concurrent.ExecutionContext.Implicits.global
1616
import org.junit.Assert._
1717
import org.junit.Test
1818

19+
import org.scalajs.junit.async._
20+
1921
/* This is currently hard-coded for Node.js modules in particular.
2022
* We are importing built-in Node.js modules, because we do not have any
2123
* infrastructure to load non-built-in modules. In the future, we should use
2224
* our own user-defined ES6 modules written in JavaScript.
2325
*/
2426
class DynamicImportTest {
27+
import DynamicImportTest._
28+
29+
@Test def testSuccessfulImport(): AsyncResult = {
30+
await(js.`import`[QueryStringAPI]("querystring").toFuture.map { qs =>
31+
assertEquals("object", js.typeOf(qs))
32+
33+
val dict = js.Dictionary("foo" -> "bar", "baz" -> "qux")
2534

26-
@Test def testDynImportParsesAndExecutes(): Unit = {
27-
/* Since we do not have support for asynchronous tests, all we can do here
28-
* is test that `js.import` parses, links, and executes without error,
29-
* returning a `Promise`. We can't actually wait for the promise to
30-
* resolve and hence cannot test its result.
31-
*
32-
* We will be able to revisit this in Scala.js 1.x.
33-
*/
34-
35-
def isPromise(x: Any): Boolean = x.isInstanceOf[js.Promise[_]]
36-
37-
assertTrue(isPromise(js.`import`[js.Any]("fs")))
38-
39-
val failedPromise = js.`import`[js.Any]("non-existent-module")
40-
assertTrue(isPromise(failedPromise))
41-
// Recover to avoid the unhandled rejected Promise warning of Node.js
42-
failedPromise.toFuture.recover {
43-
case th: Throwable => ()
44-
}
35+
assertEquals("foo=bar&baz=qux", qs.stringify(dict))
36+
assertEquals("foo:bar;baz:qux", qs.stringify(dict, ";", ":"))
37+
})
4538
}
4639

40+
@Test(expected = classOf[js.JavaScriptException])
41+
def testFailedImport(): AsyncResult =
42+
await(js.`import`[js.Any]("non-existent-module").toFuture)
43+
}
44+
45+
object DynamicImportTest {
46+
trait QueryStringAPI extends js.Any {
47+
def stringify(obj: js.Dictionary[String]): String
48+
def stringify(obj: js.Dictionary[String], sep: String, eq: String): String
49+
}
4750
}

0 commit comments

Comments
 (0)
0