Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @bazel/karma with all npm packages installed. Try it out:

var karma = require("@bazel/karma")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@bazel/karma v0.27.12

Karma rules for Bazel

Karma rules for Bazel

WARNING: this is beta-quality software. Breaking changes are likely. Not recommended for production use without expert support.

The Karma rules run karma tests with Bazel.

Installation

Add the @bazel/karma npm package to your devDependencies in package.json.

Your WORKSPACE should declare a yarn_install or npm_install rule named npm. It should then install the rules found in the npm packages using the `install_bazel_dependencies' function. See https://github.com/bazelbuild/rules_nodejs/#quickstart

This causes the @bazel/karma package to be installed as a Bazel workspace named npm_bazel_karma.

Now add this to your WORKSPACE to install the Karma dependencies:

# Fetch transitive Bazel dependencies of npm_bazel_karma
load("@npm_bazel_karma//:package.bzl", "rules_karma_dependencies")
rules_karma_dependencies()

This installs the io_bazel_rules_webtesting repository, if you haven't installed it earlier.

Finally, configure the rules_webtesting:

# Setup web testing, choose browsers we can test on
load("@io_bazel_rules_webtesting//web:repositories.bzl", "browser_repositories", "web_test_repositories")

web_test_repositories()
browser_repositories(
    chromium = True,
)

Installing with self-managed dependencies

If you didn't use the yarn_install or npm_install rule to create an npm workspace, you'll have to declare a rule in your root BUILD.bazel file to execute karma:

# Create a karma rule to use in ts_web_test_suite karma
# attribute when using self-managed dependencies
nodejs_binary(
    name = "karma/karma",
    entry_point = "karma/bin/karma",
    # Point bazel to your node_modules to find the entry point
    node_modules = ["//:node_modules"],
)

Unit testing with Karma

run_karma_web_test

run_karma_web_test(ctx)

Internal utility for use by Bazel rule authors.

Creates an action that can run karma. This is also used by ts_web_test_rule.

Returns: The runfiles for the generated action.

Attributes

ctx

Unknown; Required

Bazel rule execution context

karma_web_test

karma_web_test(srcs, deps, data, configuration_env_vars, bootstrap, runtime_deps, static_files, config_file, tags, **kwargs)

Runs unit tests in a browser with Karma.

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.

This rule will use your system Chrome by default. In the default case, your environment must specify CHROME_BIN so that the rule will know which Chrome binary to run. Other browsers and customLaunchers may be set using the a base Karma configuration specified in the config_file attribute.

Attributes

srcs

List of strings; Optional

A list of JavaScript test files

deps

List of strings; Optional

Other targets which produce JavaScript such as ts_library

data

List of strings; Optional

Runtime dependencies

configuration_env_vars

List of strings; Optional

Pass these configuration environment variables to the resulting binary. Chooses a subset of the configuration environment variables (taken from ctx.var), which also includes anything specified via the --define flag. Note, this can lead to different outputs produced by this rule.

bootstrap

List of strings; Optional

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.

runtime_deps

List of strings; Optional

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 strings; Optional

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

config_file

Unknown; Optional

User supplied Karma configuration file. Bazel will override certain attributes of this configuration file. Attributes that are overridden will be outputted to the test log.

tags

List of strings; Optional

Standard Bazel tags, this macro adds tags for ibazel support

**kwargs

Unknown; Optional

Passed through to karma_web_test

karma_web_test_suite

karma_web_test_suite(name, browsers, 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 karma_web_test target.

This macro also accepts all parameters in karma_web_test. See karma_web_test docs for details.

Attributes

name

Name; Required

The base name of the test

browsers

List of strings; Optional

A sequence of labels specifying the browsers to use.

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.

Unit testing in a browser

ts_web_test

ts_web_test(srcs, deps, data, configuration_env_vars, bootstrap, runtime_deps, static_files, tags, **kwargs)

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.

This rule will use your system Chrome. Your environment must specify CHROME_BIN so that the rule will know which Chrome binary to run.

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

Attributes

srcs

List of strings; Optional

A list of JavaScript test files

deps

List of strings; Optional

Other targets which produce JavaScript such as ts_library

data

List of strings; Optional

Runtime dependencies

configuration_env_vars

List of strings; Optional

Pass these configuration environment variables to the resulting binary. Chooses a subset of the configuration environment variables (taken from ctx.var), which also includes anything specified via the --define flag. Note, this can lead to different outputs produced by this rule.

bootstrap

List of strings; Optional

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.

runtime_deps

List of strings; Optional

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 strings; Optional

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

tags

List of strings; Optional

Standard Bazel tags, this macro adds tags for ibazel support as well as

**kwargs

Unknown; Optional

Passed through to ts_web_test

ts_web_test_suite

ts_web_test_suite(name, browsers, 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.

This macro also accepts all parameters in ts_web_test. See ts_web_test docs for details.

Attributes

name

Name; Required

The base name of the test.

browsers

List of strings; Optional

A sequence of labels specifying the browsers to use.

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_suite 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_suite.

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.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free