2021-04-16 15:46:31 +02:00
|
|
|
load("@npm//@bazel/typescript:index.bzl", "ts_library")
|
2021-04-17 08:38:45 +02:00
|
|
|
load("@esbuild_toolchain//:esbuild.bzl", esbuild = "esbuild_macro")
|
2021-04-16 15:46:31 +02:00
|
|
|
load("@npm//jest-cli:index.bzl", _jest_test = "jest_test")
|
|
|
|
|
2021-04-17 09:05:48 +02:00
|
|
|
def jest_test(deps, name = "jest", protobuf = False):
|
2021-04-16 15:46:31 +02:00
|
|
|
"Build *.test.ts into a library, then test it with Jest."
|
|
|
|
|
2021-04-17 08:38:45 +02:00
|
|
|
ts_sources = native.glob(["*.test.ts"])
|
|
|
|
|
|
|
|
# compile sources for type checking
|
2021-04-16 15:46:31 +02:00
|
|
|
ts_library(
|
|
|
|
name = name + "_lib",
|
2021-04-17 08:38:45 +02:00
|
|
|
srcs = ts_sources,
|
2021-04-16 15:46:31 +02:00
|
|
|
tsconfig = "//ts:tsconfig.json",
|
|
|
|
deps = deps + [
|
|
|
|
"@npm//@types/jest",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2021-04-17 08:38:45 +02:00
|
|
|
# bundle each test file up with its dependencies for jest
|
|
|
|
bundled_srcs = []
|
2021-04-17 09:05:48 +02:00
|
|
|
esbuild_extra_args = []
|
|
|
|
esbuild_extra_srcs = []
|
|
|
|
if protobuf:
|
|
|
|
esbuild_extra_args.append(
|
|
|
|
"--inject:$(location //ts:protobuf-no-long.js)",
|
|
|
|
)
|
|
|
|
esbuild_extra_srcs.append(
|
|
|
|
"//ts:protobuf-no-long.js",
|
|
|
|
)
|
|
|
|
|
2021-04-17 08:38:45 +02:00
|
|
|
for ts_src in ts_sources:
|
|
|
|
base = ts_src.replace(".test.ts", "")
|
|
|
|
bundle_name = base + ".bundle.test"
|
|
|
|
bundled_srcs.append(bundle_name)
|
|
|
|
esbuild(
|
|
|
|
name = bundle_name,
|
|
|
|
args = [
|
|
|
|
"--resolve-extensions=.mjs,.js",
|
|
|
|
"--log-level=warning",
|
|
|
|
"--platform=node",
|
|
|
|
"--keep-names",
|
2021-04-17 09:05:48 +02:00
|
|
|
] + esbuild_extra_args,
|
2021-04-17 08:38:45 +02:00
|
|
|
entry_point = ts_src,
|
|
|
|
output = bundle_name + ".js",
|
2021-04-17 09:05:48 +02:00
|
|
|
srcs = esbuild_extra_srcs,
|
2021-04-17 08:38:45 +02:00
|
|
|
deps = [
|
|
|
|
name + "_lib",
|
|
|
|
] + deps,
|
2021-04-25 10:40:02 +02:00
|
|
|
# the code shaking saves close to a second off the deckoptions/lib.test.ts test
|
2021-04-17 08:38:45 +02:00
|
|
|
minify = True,
|
|
|
|
)
|
|
|
|
|
2021-04-16 15:46:31 +02:00
|
|
|
# then test them
|
|
|
|
_jest_test(
|
|
|
|
name = name,
|
|
|
|
args = [
|
|
|
|
"--no-cache",
|
|
|
|
"--no-watchman",
|
|
|
|
"--ci",
|
|
|
|
"--colors",
|
|
|
|
"--config",
|
|
|
|
"$(location //ts:jest.config.js)",
|
|
|
|
],
|
2021-04-17 08:38:45 +02:00
|
|
|
data = bundled_srcs + [
|
2021-04-16 15:46:31 +02:00
|
|
|
"//ts:jest.config.js",
|
|
|
|
],
|
|
|
|
target_compatible_with = select({
|
|
|
|
"@platforms//os:osx": [],
|
|
|
|
"@platforms//os:linux": [],
|
|
|
|
"//conditions:default": ["@platforms//os:linux"],
|
|
|
|
}),
|
|
|
|
)
|