binhonglee/GlobeTrotte

View on GitHub
build_defs/npm/jest.build_defs

Summary

Maintainability
Test Coverage
subinclude("//build_defs/npm")

def jest_dir(
  name:str,
  srcs:list=[],
  deps:list=[],
  visibility:list=[],
):
  return build_rule(
    name = name,
    srcs = srcs,
    cmd = "zip -r " + name + ".jest_dir ./ > /dev/null",
    outs = [name + ".jest_dir"],
    deps = deps,
    test_only = True,
    visibility = visibility,
  )

def jest_tests(
  name:str,
  srcs:list=[],
  jest_dirs:list=[],
  deps:list=[],
  visibility:list=[],
):
  test_rules = []

  for src in srcs:
    test = jest_test(
      name = src[:-8].replace("/", "_"),
      src = src,
      jest_dirs = jest_dirs,
      deps = deps,
      visibility = visibility,
    )
    test_rules += [test]

  return build_rule(
    name = name,
    data = test_rules + srcs,
    test_cmd = "echo 'test'",
    test_only = True,
    visibility = visibility,
    requires = ["npm-test"],
  )

def jest_test(
  name:str,
  src:str,
  jest_dirs:list=[],
  deps:list=[],
  visibility:list=[],
):
  pre_build = _set_node_modules()
  for jest_dir in jest_dirs:
    jest_dir_name = jest_dir.split(":")
    if len(name) < 2:
      temp = jest_dir.split("/")
      jest_dir_name = temp[len(temp) - 1]
    else:
      jest_dir_name = jest_dir_name[1]
    pre_build += " && unzip -o " + jest_dir_name + ".jest_dir > /dev/null"

  out_dir = _jest_out_dir(name)
  if CONFIG.CORE_FILES != "":
    deps += [CONFIG.CORE_FILES]

  return build_rule(
    name = name,
    data = [src] + jest_dirs + deps,
    test_cmd = " && ".join([
      pre_build,
      # Remove unrelated snapshots.
      " || ".join([
        " | ".join([
          "find src/cockpit/tests/__snapshots__/ -name '*.spec.snap*'",
          "grep -v " + name.replace("_", "/"),
          "xargs rm -- "
        ]),
        "echo \"No snapshots to delete\"",
      ]),
      " ".join([
        "$TOOL",
        "jest",
        "--maxConcurrency=1",
        "--maxWorkers=1",
        "--silent",
        "$PKG_DIR/" + src,
      ]),
      "cp jest_coverage/test_results.xml test.results",
      "cp jest_coverage/cobertura-coverage.xml test.coverage",
      "mkdir -p " + out_dir,
      "mv jest_coverage/* " + out_dir,
    ]),
    test_outputs = [out_dir],
    test = True,
    test_only = True,
    visibility = visibility,
    test_tools = [CONFIG.NPX_TOOL],
    labels = ['jest'],
    requires = ["npm-test"],
  )

def _jest_out_dir(name: str):
  return name + "/.nyc_output"