Unit testing with Karma
Bazel

Unit testing with Karma


ts_web_test_macro

ts_web_test_macro(karma, tags, data, **kwargs)

ibazel wrapper for ts_web_test

This macro re-exposes the ts_web_test rule with some extra tags so that it behaves correctly under ibazel.

This is re-exported in //:defs.bzl as ts_web_test so if you load the rule from there, you actually get this macro.

Attributes

karma

Unknown; Optional

karma binary label

tags

List of strings; Optional

standard Bazel tags, this macro adds a couple for ibazel

data

List of strings; Optional

runtime dependencies

**kwargs

Unknown; Optional

passed through to ts_web_test


ts_web_test_suite

ts_web_test_suite(name, browsers, karma, args, browser_overrides, config, flaky, local, shard_count, size, tags, test_suite_tags, timeout, visibility, web_test_data, wrapped_test_tags, **remaining_keyword_args)

Defines a test_suite of web_test targets that wrap a ts_web_test target.

Attributes

name

Name; Required

The base name of the test.

browsers

List of strings; Optional

A sequence of labels specifying the browsers to use.

karma

Unknown; Optional

karma binary label

args

Unknown; Optional

Args for web_test targets generated by this extension.

browser_overrides

Unknown; Optional

Dictionary; optional; default is an empty dictionary. A dictionary mapping from browser names to browser-specific web_test attributes, such as shard_count, flakiness, timeout, etc. For example: {'//browsers:chrome-native': {'shard_count': 3, 'flaky': 1} '//browsers:firefox-native': {'shard_count': 1, 'timeout': 100}}.

config

Unknown; Optional

Label; optional; Configuration of web test features.

flaky

Unknown; Optional

A boolean specifying that the test is flaky. If set, the test will be retried up to 3 times (default: 0)

local

Unknown; Optional

boolean; optional.

shard_count

Unknown; Optional

The number of test shards to use per browser. (default: 1)

size

Unknown; Optional

A string specifying the test size. (default: 'large')

tags

List of strings; Optional

A list of test tag strings to apply to each generated web_test target. This macro adds a couple for ibazel.

test_suite_tags

Unknown; Optional

A list of tag strings for the generated test_suite.

timeout

Unknown; Optional

A string specifying the test timeout (default: computed from size)

visibility

Unknown; Optional

List of labels; optional.

web_test_data

List of strings; Optional

Data dependencies for the web_test.

wrapped_test_tags

Unknown; Optional

A list of test tag strings to use for the wrapped test

**remaining_keyword_args

Unknown; Optional

Arguments for the wrapped test target.


ts_web_test

ts_web_test(name, deps, data, srcs, bootstrap, karma, runtime_deps, static_files)

Runs unit tests in a browser.

When executed under bazel test, this uses a headless browser for speed. This is also because bazel test allows multiple targets to be tested together, and we don't want to open a Chrome window on your machine for each one. Also, under bazel test the test will execute and immediately terminate.

Running under ibazel test gives you a "watch mode" for your tests. The rule is optimized for this case - the test runner server will stay running and just re-serve the up-to-date JavaScript source bundle.

To debug a single test target, run it with bazel run instead. This will open a browser window on your computer. Also you can use any other browser by opening the URL printed when the test starts up. The test will remain running until you cancel the bazel run command.

Currently this rule uses Karma as the test runner, but this is an implementation detail. We might switch to another runner like Jest in the future.

Attributes

name

Name; Required

A unique name for this rule.

deps

List of labels; Optional; Default is []

Other targets which produce JavaScript such as ts_library

data

List of labels; Optional; Default is []

Runtime dependencies

srcs

List of labels; Optional; Default is []

JavaScript source files

bootstrap

List of labels; Optional; Default is []

JavaScript files to include before the module loader (require.js). For example, you can include Reflect,js for TypeScript decorator metadata reflection, or UMD bundles for third-party libraries.

karma

Label; Optional; Default is @npm//@bazel/karma/bin:karma

runtime_deps

List of labels; Optional; Default is []

Dependencies which should be loaded after the module loader but before the srcs and deps. These should be a list of targets which produce JavaScript such as ts_library. The files will be loaded in the same order they are declared by that rule.

static_files

List of labels; Optional; Default is []

Arbitrary files which are available to be served on request. Files are served at: /base/<WORKSPACE_NAME>/<path-to-file>, e.g. /base/build_bazel_rules_typescript/examples/testing/static_script.js