-2

I am trying to define facade for : https://facebook.github.io/jest/docs/en/api.html#testonlyname-fn

@JSGlobalScope
@js.native
object JestGlobal extends js.Object {

  def test(str: String, function: js.Function0[_]): Unit = js.native

  @JSName("test.only")
  def testOnly(str: String, function: js.Function0[_]): Unit = js.native

  def expect[T](in:T) : Matcher[T] = js.native

}

@js.native
trait Matcher[T] extends js.Object {

  def toBe(in:T):Unit = js.native
}

Calling a method of the global scope whose name is not a valid JavaScript identifier is not allowed. [error] See https://www.scala-js.org/doc/interoperability/global-scope.html for further information.

Edit:(Answer)

 def test : JestTestObject = js.native

@js.native
trait JestTestObject extends js.Object {

  def only(str: String, function: js.Function0[_]): Unit = js.native
}
invariant
  • 8,758
  • 9
  • 47
  • 61

1 Answers1

1

For all practical purposes, there is no such thing as a JS function whose name is test.only. It is much more likely that there is a top-level object whose name is test, and it has a method named only. You can model that as:

@js.native
@JSGlobal("test")
object JestTest extends js.Object {
  def only(str: String, function: js.Function0[_]): Unit = js.native
}

You can also use the same object to represent the top-level function whose name is test (because apparently the library is presented so), by adding an apply method:

@js.native
@JSGlobal("test")
object JestTest extends js.Object {
  // This is the test(...) function
  def apply(str: String, function: js.Function0[_]): Unit = js.native

  // This is the test.only(...) function
  def only(str: String, function: js.Function0[_]): Unit = js.native
}

Your variant as a self-answer is also valid, but it can be made more idiomatic as follows:

@js.native
@JSGlobalScope
object JestGlobal extends js.Object {

  @js.native
  object test extends js.Object {
    // This is the test(...) function
    def apply(str: String, function: js.Function0[_]): Unit = js.native

    // This is the test.only(...) function
    def only(str: String, function: js.Function0[_]): Unit = js.native
  }

  def expect[T](in: T): Matcher[T] = js.native

}
sjrd
  • 21,805
  • 2
  • 61
  • 91