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
Defines a cypress toolchain.
Example usage (generated)
load("@npm//@bazel/cypress:index.bzl", "cypress_toolchain") cypress_toolchain( # A unique name for this target. name = "", )
A unique name for this target.
A hermetically downloaded cypress executable binary for the target platform.
Path to an existing cypress executable for the target platform.
A hermetically downloaded cypress filegroup of all cypress binary files for the target platform. Must be set when cypress_bin is set.
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
Example usage (generated)
load("@npm//@bazel/cypress:index.bzl", "cypress_web_test") cypress_web_test( # A unique name for this target. name = "", # cypress.json configuration file config_file = "", )
A unique name for this target.
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.
cypress.json configuration file. See https://docs.cypress.io/guides/references/configuration
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.
The cypress npm package. If you installed cypress as a peer dependency, this should not need to be set.
Runtime dependencies which may be loaded during execution.
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
Entry point JS file which bootstraps the cypress cli
Specifies additional environment variables to set when the target is executed, subject to location expansion.
The expected exit code for the test. Defaults to 0.
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.
Your cypress plugin file. See https://docs.cypress.io/guides/tooling/plugins-guide
A list of test files. See https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Test-files
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.
Macros and Functions
Repository rule used to install cypress binary.
Example usage (generated)
load("@npm//@bazel/cypress:index.bzl", "cypress_repositories") cypress_repositories( # Name of the external workspace where the cypress binary lives name = "", # Version of cypress binary to use version = None, )
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 distros of linux 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 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