fix(common): fix utils tests by improving jest dependency resolution

This commit is contained in:
41666 2020-10-12 17:38:33 -04:00
parent 5b440ffa8d
commit 61f6c3b34b
4 changed files with 37 additions and 32 deletions

View file

@ -1,7 +1,9 @@
# Copied from https://github.com/bazelbuild/rules_nodejs/blob/stable/examples/jest/jest.bzl
# Licensed under Apache-2.0, not modified.
# Licensed under Apache-2.0, modifications made:
# - improved dependency resolution
load("@npm//jest-cli:index.bzl", "jest", _jest_test = "jest_test")
load("//:hack/utils.bzl", "render_deps")
DEFAULT_DEPS = [
"@npm//ts-jest",
@ -25,7 +27,7 @@ def _impl_jest_test(name, srcs, deps, jest_config, **kwargs):
"--no-cache",
"--no-watchman",
"--ci",
"--colors",
"--no-colors",
]
templated_args.extend(["--config", "$(rootpath %s)" % jest_config])
for src in srcs:
@ -51,6 +53,6 @@ def jest_test(src, deps = []):
_impl_jest_test(
name = src[1:] + "_test",
srcs = native.glob(["*.spec.ts", "*.spec.tsx"]),
deps = [src] + deps + DEFAULT_DEPS,
deps = [src] + render_deps(deps) + DEFAULT_DEPS,
jest_config = "//:jest.config.js",
)

View file

@ -1,26 +1,5 @@
load("@npm//@bazel/typescript:index.bzl", "ts_library")
def _render_deps(deps = []):
output_deps = []
has_added_grpc_deps = False
for dep in deps:
if dep.startswith("//src/rpc"):
output_deps.append(dep + ":ts")
if has_added_grpc_deps == False:
output_deps.extend([
"@npm//google-protobuf",
"@npm//@types/google-protobuf",
"@npm//@improbable-eng/grpc-web",
])
has_added_grpc_deps = True
elif dep.startswith("//"):
output_deps.append(dep)
else:
output_deps.append("@npm//" + dep)
return output_deps
load("//:hack/utils.bzl", "render_deps")
def react_library(name, deps = [], **kwargs):
ts_library(
@ -36,6 +15,6 @@ def react_library(name, deps = [], **kwargs):
"*.stories.tsx",
]),
),
deps = _render_deps(deps),
deps = render_deps(deps),
**kwargs
)

22
hack/utils.bzl Normal file
View file

@ -0,0 +1,22 @@
def render_deps(deps = []):
output_deps = []
has_added_grpc_deps = False
for dep in deps:
if dep.startswith("//src/rpc"):
output_deps.append(dep + ":ts")
output_deps.append(dep + ":ts_proto")
if has_added_grpc_deps == False:
output_deps.extend([
"@npm//google-protobuf",
"@npm//@types/google-protobuf",
"@npm//@improbable-eng/grpc-web",
])
has_added_grpc_deps = True
elif dep.startswith("//"):
output_deps.append(dep)
else:
output_deps.append("@npm//" + dep)
return output_deps

View file

@ -1,4 +1,5 @@
load("//:hack/react.bzl", "react_library")
load("//:hack/jest.bzl", "jest_test")
package(default_visibility = ["//visibility:public"])
@ -15,9 +16,10 @@ react_library(
],
)
# jest_test(
# src = ":utils",
# deps = [
# "//hack/fixtures",
# ],
# )
jest_test(
src = ":utils",
deps = [
"//hack/fixtures",
"//src/rpc/shared",
],
)