"""Simple macro around jest_test"""

load("@npm//jest-cli:index.bzl", _jest_test = "jest_test")
load("//packages/concatjs:index.bzl", "ts_library")

def ts_jest_test(name, srcs, jest_config, deps = [], data = [], **kwargs):
    """A macro around the autogenerated jest_test rule that takes typescript sources

Uses ts_library prodmode esm output"""

    ts_library(
        name = "%s_ts" % name,
        srcs = srcs,
        data = data,
        # Ideally we'd use @types/jest, but it causes typings conflicts
        # if installed together with @types/jasmine and they both end up
        # ambiently included in a TS compile
        deps = deps + ["@npm//@types/jasmine"],
        # NB: hacky hidden configuration setting so that es6_sources does not include tsickle
        #     .externs.js outputs
        runtime = "nodejs",
        # Prevent requesting the .d.ts output as that will also produce lib.test.js
        # Without sandbox (e.g. windows) that will be loaded instead of lib.test.mjs
        tags = ["manual"],
    )
    native.filegroup(
        name = "%s_esm" % name,
        srcs = [":%s_ts" % name],
        output_group = "es6_sources",
    )

    args = [
        "--no-cache",
        "--no-watchman",
        "--ci",
    ]
    args.extend(["--config", "$$(rlocation $(rootpath %s))" % jest_config])

    _jest_test(
        name = name,
        data = [jest_config, ":%s_esm" % name] + deps + data,
        templated_args = args,
        **kwargs
    )
