【问题标题】:scala.js testing with jestscala.js 用玩笑进行测试
【发布时间】:2017-11-29 22:20:04
【问题描述】:

https://facebook.github.io/jest/docs/en/getting-started.html

Jest runner 需要一个后缀为 .test/spec.js 的文件

sum.test.js

const sum = require('./sum');

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

我通过创建新的 sjs 项目实现了类似的目标

package myapp.test

import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobalScope

object App {

  def main(args: Array[String]): Unit = {
    import Jest._
    test("add", () => {
      expect(1+ 2).toBe(3);
    })

  }
}


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

  def test(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
}

//build.sbt

artifactPath in Compile in fastOptJS := new File("./scalajs.test.js")

scalaJSLinkerConfig ~= (_.withModuleKind(ModuleKind.CommonJSModule))

scalaJSUseMainModuleInitializer := true

现在npm run test 运行我的测试,是否可以通过在 test/scala 中编写测试来实现类似的效果?(无需创建新的单独项目)。可能我正在寻找一种方法来定义 main method 用于测试和 scalaJSUseMainModuleInitializer in Test := true ? :s

【问题讨论】:

    标签: scala unit-testing scala.js jestjs


    【解决方案1】:

    完全可以在src/test/scala/目录下定义一个main对象/方法,并使用

    scalaJSUseMainModuleInitializer in Test := true
    

    要运行它,请使用来自 sbt 的 test:run。

    【讨论】:

      猜你喜欢
      • 2022-06-15
      • 1970-01-01
      • 2021-02-10
      • 2020-07-31
      • 1970-01-01
      • 2018-01-29
      • 2019-02-22
      • 1970-01-01
      • 2020-11-14
      相关资源
      最近更新 更多