Skip to content

Commit

Permalink
build: add bazel-jest rule that works with tests that do not require …
Browse files Browse the repository at this point in the history
…TestBed (#2533)
  • Loading branch information
alex-okrushko committed May 21, 2020
1 parent bda97fa commit 914de92
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
4 changes: 4 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ package(default_visibility = ["//visibility:public"])
exports_files([
"tsconfig.json",
"LICENSE",
# used by ts_jest_test
"jest.config.js",
"setup-jest.ts",
"tsconfig.json",
])
13 changes: 3 additions & 10 deletions modules/component-store/spec/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("//tools:defaults.bzl", "jasmine_node_test", "ts_test_library")
load("//tools:defaults.bzl", "ts_jest_test")

ts_test_library(
name = "test_lib",
ts_jest_test(
name = "test",
srcs = glob(
[
"**/*.ts",
Expand All @@ -12,10 +12,3 @@ ts_test_library(
"@npm//rxjs",
],
)

jasmine_node_test(
name = "test",
deps = [
":test_lib",
],
)
30 changes: 30 additions & 0 deletions tools/defaults.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Re-export of some bazel rules with repository-wide defaults."""

load("@build_bazel_rules_nodejs//:index.bzl", _pkg_npm = "pkg_npm")
load("@npm//jest-cli:index.bzl", _jest_test = "jest_test")
load("@npm_angular_bazel//:index.bzl", _ng_module = "ng_module", _ng_package = "ng_package")
load("@npm_bazel_jasmine//:index.bzl", _jasmine_node_test = "jasmine_node_test")
load("@npm_bazel_typescript//:index.bzl", _ts_library = "ts_library")
Expand Down Expand Up @@ -108,6 +109,35 @@ def jasmine_node_test(node_modules = None, deps = [], **kwargs):
**kwargs
)

def ts_jest_test(name, srcs, deps = [], **kwargs):
# A macro around the autogenerated jest_test rule.
# Takes TS files as srcs and runs jest with them. Jest uses TS compiler
# to transform the sources into JS.
args = [
"--no-cache",
"--no-watchman",
"--ci",
]

jest_config = "//:jest.config.js"
args.extend(["--config", "$(location %s)" % jest_config])

for src in srcs:
args.extend(["--runTestsByPath", "$(locations %s)" % src])

_jest_test(
name = name,
data = [
"@npm//typescript",
"@npm//jest-preset-angular",
"//:setup-jest.ts",
"//:tsconfig.json",
jest_config,
] + deps + srcs,
args = args,
**kwargs
)

def ng_module(name, tsconfig = None, entry_point = None, deps = [], **kwargs):
if not tsconfig:
tsconfig = DEFAULT_TSCONFIG
Expand Down

0 comments on commit 914de92

Please sign in to comment.