Skip to content

Commit 993a027

Browse files
devversionmmalerba
authored andcommitted
build: add option to start karma without browser (#17912)
Adds a way to start Karma without a browser. Similar to `yarn gulp test:static`. The targets can be run the following way and work with `ibazel` watch mode. `yarn ibazel run src/cdk/a11y:unit_tests_local`.
1 parent 26e73ac commit 993a027

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

test/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package(default_visibility = ["//visibility:public"])
22

33
load("//tools:defaults.bzl", "ts_library")
44

5+
exports_files(["bazel-karma-local-config.js"])
6+
57
# Common set-up for all Angular Material and CDK tests.
68
ts_library(
79
name = "angular_test_init",

test/bazel-karma-local-config.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Karma configuration that is used by Bazel karma_web_test targets which do not
3+
* want to launch any browser and just enable manual browser debugging.
4+
*/
5+
6+
module.exports = config => {
7+
const overwrites = {};
8+
9+
// By default "@bazel/karma" configures Chrome as browser. Since we don't want
10+
// to launch any browser at all, we overwrite the "browsers" option. Since the
11+
// default config tries to extend the browsers array with "Chrome", we need to
12+
// always return a new empty array.
13+
Object.defineProperty(overwrites, 'browsers', {
14+
get: () => [],
15+
set: () => {},
16+
enumerable: true
17+
});
18+
19+
// Ensures that tests start executing once browsers have been manually connected. We need
20+
// to use "defineProperty" because the default "@bazel/karma" config overwrites the option.
21+
Object.defineProperty(overwrites, 'autoWatch', {
22+
value: true,
23+
writable: false,
24+
});
25+
26+
config.set(overwrites);
27+
};

tools/defaults.bzl

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
load("@io_bazel_rules_sass//:defs.bzl", _sass_binary = "sass_binary", _sass_library = "sass_library")
44
load("@npm_angular_bazel//:index.bzl", _ng_module = "ng_module", _ng_package = "ng_package")
55
load("@npm_bazel_jasmine//:index.bzl", _jasmine_node_test = "jasmine_node_test")
6-
load("@npm_bazel_karma//:index.bzl", _karma_web_test_suite = "karma_web_test_suite")
6+
load("@npm_bazel_karma//:index.bzl", _karma_web_test = "karma_web_test", _karma_web_test_suite = "karma_web_test_suite")
77
load("@npm_bazel_protractor//:index.bzl", _protractor_web_test_suite = "protractor_web_test_suite")
88
load("@npm_bazel_typescript//:index.bzl", _ts_library = "ts_library")
99
load("//:packages.bzl", "VERSION_PLACEHOLDER_REPLACEMENTS", "getAngularUmdTargets")
@@ -154,14 +154,38 @@ def ng_e2e_test_library(deps = [], tsconfig = None, **kwargs):
154154
**kwargs
155155
)
156156

157-
def karma_web_test_suite(deps = [], srcs = [], **kwargs):
157+
def karma_web_test_suite(name, **kwargs):
158+
web_test_args = {}
159+
kwargs["srcs"] = ["@npm//:node_modules/tslib/tslib.js"] + getAngularUmdTargets() + kwargs.get("srcs", [])
160+
kwargs["deps"] = ["//tools/rxjs:rxjs_umd_modules"] + kwargs.get("deps", [])
161+
162+
for opt_name in kwargs.keys():
163+
# Filter out options which are specific to "karma_web_test" targets. We cannot
164+
# pass options like "browsers" to the local web test target.
165+
if not opt_name in ["wrapped_test_tags", "browsers", "wrapped_test_tags", "tags"]:
166+
web_test_args[opt_name] = kwargs[opt_name]
167+
168+
# Custom standalone web test that can be run to test against any browser
169+
# that is manually connected to.
170+
_karma_web_test(
171+
name = "%s_local_bin" % name,
172+
config_file = "//test:bazel-karma-local-config.js",
173+
tags = ["manual"],
174+
**web_test_args
175+
)
176+
177+
# Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1429
178+
native.sh_binary(
179+
name = "%s_local" % name,
180+
srcs = ["%s_local_bin" % name],
181+
data = [":%s_local_bin" % name],
182+
tags = ["manual", "local", "ibazel_notify_changes"],
183+
testonly = True,
184+
)
185+
186+
# Default test suite with all configured browsers.
158187
_karma_web_test_suite(
159-
deps = ["//tools/rxjs:rxjs_umd_modules"] + deps,
160-
# Required for running the compiled ng modules that use TypeScript import helpers.
161-
# TODO(jelbourn): remove UMDs from here once we don't have to manually include them
162-
srcs = [
163-
"@npm//:node_modules/tslib/tslib.js",
164-
] + getAngularUmdTargets() + srcs,
188+
name = name,
165189
**kwargs
166190
)
167191

0 commit comments

Comments
 (0)