Cypress rules for Bazel
The Cypress rules run tests under the Cypress e2e testing framework with Bazel.
cypress npm packages to your
npm install --save-dev @bazel/cypress cypress
or using yarn
yarn add -D @bazel/cypress cypress
Then, load and invoke
cypress_repositories within your
load("@build_bazel_rules_nodejs//toolchains/cypress:cypress_repositories.bzl", "cypress_repositories") # The name you pass here names the external repository you can load cypress_web_test from cypress_repositories(name = "cypress", version = "MATCH_VERSION_IN_PACKAGE_JSON")
Example use of cypress_web_test
This example assumes you've named your external repository for node_modules as
npm and for cypress as
cypress_toolchain(name, cypress_bin, cypress_bin_path, cypress_files)
Defines a cypress toolchain.
(Name, mandatory): A unique name for this target.
(Label): A hermetically downloaded cypress executable binary for the target platform.
(String): Path to an existing cypress executable for the target platform.
(Label): A hermetically downloaded cypress filegroup of all cypress binary files for the target platform. Must be set when cypress_bin is set.
cypress_web_test(name, chdir, config_file, configuration_env_vars, cypress_npm_package, data, default_env_vars, entry_point, env, expected_exit_code, link_workspace_root, plugin_file, srcs, templated_args, toolchain)
nodejs_binary, except this can be used with
bazel test as well.
When the binary returns zero exit code, the test passes; otherwise it fails.
nodejs_test is a convenient way to write a novel kind of test based on running
your own test runner. For example, the
ts-api-guardian library has a way to
assert the public API of a TypeScript program, and uses
If you just want to run a standard test using a test runner from npm, use the generated
*_test target created by npm_install/yarn_install, such as
Some test runners like Karma and Jasmine have custom rules with added features, e.g.
By default, Bazel runs tests with a working directory set to your workspace root.
chdir attribute to change the working directory before the program starts.
To debug a Node.js test, we recommend saving a group of flags together in a "config".
Put this in your
tools/bazel.rc so it's shared with your team:
# Enable debugging tests with --config=debug test:debug --test_arg=--node_options=--inspect-brk --test_output=streamed --test_strategy=exclusive --test_timeout=9999 --nocache_test_results
Now you can add
--config=debug to any
bazel test command line.
The runtime will pause before executing the program, allowing you to connect a
You can also change the default args that are sent to nodejs. This can be done through a flag. The default is --preserve-symlinks while anything can be passed. The flag is --@rules_nodejs//nodejs:default_args="" ex: bazel test --@rules_nodejs//nodejs:default_args="--preserve-symlinks --no-warnings" //:target. This will pass --preserve-symlinks and --no-warnings flags to nodejs. Available node flags can be found here: https://nodejs.org/api/cli.html.
(Name, mandatory): A unique name for this target.
(String): Working directory to run the binary or test in, relative to the workspace. By default, Bazel always runs in the workspace root. Due to implementation details, this argument must be underneath this package directory.
To run in the directory containing the
chdir = package_name()
(or if you're in a macro, use
WARNING: this will affect other paths passed to the program, either as arguments or in configuration files,
which are workspace-relative.
You may need
../../ segments to re-relativize such paths to the new working directory.
(Label, mandatory): cypress.json configuration file. See https://docs.cypress.io/guides/references/configuration
(List of strings): 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.
(Label): The cypress npm package. If you installed cypress as a peer dependency, this should not need to be set.
(List of labels): Runtime dependencies which may be loaded during execution.
(List of strings): Default environment variables that are added to
This is separate from the default of
configuration_env_vars so that a user can set
without losing the defaults that should be set in most cases.
The set of default environment variables is:
VERBOSE_LOGS: use by some rules & tools to turn on debug output in their logs
NODE_DEBUG: used by node.js itself to print more logs
RUNFILES_LIB_DEBUG: print diagnostic message from Bazel runfiles.bash helper
["VERBOSE_LOGS", "NODE_DEBUG", "RUNFILES_LIB_DEBUG"]
(Label): Entry point JS file which bootstraps the cypress cli
(Dictionary: String -> String): Specifies additional environment variables to set when the target is executed, subject to location expansion.
(Integer): The expected exit code for the test. Defaults to 0.
(Boolean): Link the workspace root to the bin_dir to support absolute requires like 'my_wksp/path/to/file'. If source files need to be required then they can be copied to the bin_dir with copy_to_bin.
(Label): Your cypress plugin file. See https://docs.cypress.io/guides/tooling/plugins-guide
(List of labels): A list of test files. See https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Test-files
(List of strings): Arguments which are passed to every execution of the program.
To pass a node startup option, prepend it with
Subject to 'Make variable' substitution. See https://docs.bazel.build/versions/main/be/make-variables.html.
- Subject to predefined source/output path variables substitutions.
The predefined variables
label parameters (e.g.
$(execpath //foo:bar)) and substitute the file paths denoted by that label.
NB: This $(location) substition returns the manifest file path which differs from the *_binary & *_test
args and genrule bazel substitions. This will be fixed in a future major release.
See docs string of
expand_location_into_runfiles macro in
for more info.
The recommended approach is to now use
$(rootpath) where you previously used $(location).
To get from a
$(rootpath) to the absolute path that
$(rlocation $(location)) returned you can either use
$(rlocation $(rootpath)) if you are in the
templated_args of a
nodejs_test( name = "my_test", data = [":bootstrap.js"], templated_args = ["--node_options=--require=$(rlocation $(rootpath :bootstrap.js))"], )
nodejs_test( name = "my_test", data = [":some_file"], entry_point = ":my_test.js", templated_args = ["$(rootpath :some_file)"], )
const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']); const args = process.argv.slice(2); const some_file = runfiles.resolveWorkspaceRelative(args);
NB: Bazel will error if it sees the single dollar sign $(rlocation path) in
templated_args as it will try to
$(rlocation) since we now expand predefined & custom "make" variables such as
ctx.expand_make_variables. See https://docs.bazel.build/versions/main/be/make-variables.html.
To prevent expansion of
$(rlocation) write it as
$(rlocation). Bazel understands
$ to be
the string literal
$ and the expansion results in
$(rlocation) being passed as an arg instead
of being expanded.
$(rlocation) is then evaluated by the bash node launcher script and it calls
rlocation function in the runfiles.bash helper. For example, the templated arg
$(rlocation $(rootpath //:some_file)) is expanded by Bazel to
$(rlocation ./some_file) which
is then converted in bash to the absolute path of
//:some_file in runfiles by the runfiles.bash helper
before being passed as an argument to the program.
NB: nodejs_binary and nodejs_test will preserve the legacy behavior of
$(rlocation) so users don't
need to update to
$(rlocation). This may be changed in the future.
- Subject to predefined variables & custom variable substitutions.
Predefined "Make" variables such as $(COMPILATION_MODE) and $(TARGET_CPU) are expanded. See https://docs.bazel.build/versions/main/be/make-variables.html#predefined_variables.
Custom variables are also expanded including variables set through the Bazel CLI with --define=SOME_VAR=SOME_VALUE. See https://docs.bazel.build/versions/main/be/make-variables.html#custom_variables.
Predefined genrule variables are not supported in this context.
cypress_repositories(name, version, linux_urls, linux_sha256, darwin_urls, darwin_sha256, darwin_arm64_urls, darwin_arm64_sha256, windows_urls, windows_sha256)
Repository rule used to install cypress binary.
Name of the external workspace where the cypress binary lives
Version of cypress binary to use. Should match package.json
(Optional) URLs at which the cypress binary for linux distros of linux can be downloaded. If omitted, https://cdn.cypress.io/desktop will be used.
(Optional) SHA-256 of the linux cypress binary
(Optional) URLs at which the cypress binary for darwin can be downloaded. If omitted, https://cdn.cypress.io/desktop will be used.
(Optional) SHA-256 of the darwin cypress binary
(Optional) URLs at which the cypress binary for darwin arm64 can be downloaded. If omitted, https://cdn.cypress.io/desktop will be used (note: as of this writing (11/2021), Cypress does not have native arm64 builds, and this URL will link to the x86_64 build to run under Rosetta).
(Optional) SHA-256 of the darwin arm64 cypress binary
(Optional) URLs at which the cypress binary for windows distros of linux can be downloaded. If omitted, https://cdn.cypress.io/desktop will be used.
(Optional) SHA-256 of the windows cypress binary