Cypress rules for Bazel
The Cypress rules run tests under the Cypress e2e testing framework with Bazel.
Installation
Add @bazel/cypress
and cypress
npm packages to your devDependencies
in package.json
.
npm install --save-dev @bazel/cypress cypress
or using yarn
yarn add -D @bazel/cypress cypress
Then, load and invoke cypress_repositories
within your WORKSPACE
file.
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
load("@npm//@bazel/concatjs:index.bzl", "ts_library")
load("@npm//@bazel/cypress:index.bzl", "cypress_web_test")
# You must create a cypress plugin in order to boot a server to serve your application. It can be written as a javascript file or in typescript using ts_library or ts_project.
ts_library(
name = "plugin_file",
testonly = True,
srcs = ["plugin.ts"],
tsconfig = ":tsconfig.json",
deps = [
"@npm//@types/node",
"@npm//express",
],
)
# You can write your cypress tests a javascript files or in typescript using ts_library or ts_project.
ts_library(
name = "hello_spec",
testonly = True,
srcs = ["hello.spec.ts"],
tsconfig = ":tsconfig.json",
deps = [
"@npm//cypress",
],
)
cypress_web_test(
# The name of your test target
name = "test",
srcs = [
# Load javascript test files directly as sources
"world.spec.js",
# Load ts_library tests as a target to srcs
":hello_spec",
],
# A cypress config file is required
config_file = "cypress.json",
# Any runtime dependencies you need to boot your server or run your tests
data = [],
# Your cypress plugin used to configure cypress and boot your server
plugin_file = ":plugin_file",
)
cypress_toolchain
USAGE
cypress_toolchain(name, cypress_bin, cypress_bin_path, cypress_files)
Defines a cypress toolchain.
For usage see https://docs.bazel.build/versions/main/toolchains.html#defining-toolchains.
ATTRIBUTES
name
(Name, mandatory): A unique name for this target.
cypress_bin
(Label): A hermetically downloaded cypress executable binary for the target platform.
Defaults to None
cypress_bin_path
(String): Path to an existing cypress executable for the target platform.
Defaults to ""
cypress_files
(Label): A hermetically downloaded cypress filegroup of all cypress binary files for the target platform. Must be set when cypress_bin is set.
Defaults to None
cypress_web_test
USAGE
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)
Identical to 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 nodejs_test
here:
https://github.com/angular/angular/blob/master/tools/ts-api-guardian/index.bzl
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 mocha_test
.
Some test runners like Karma and Jasmine have custom rules with added features, e.g. jasmine_node_test
.
By default, Bazel runs tests with a working directory set to your workspace root.
Use the 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
remote debugger.
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.
ATTRIBUTES
name
(Name, mandatory): A unique name for this target.
chdir
(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 nodejs_binary
/ nodejs_test
, use
chdir = package_name()
(or if you're in a macro, use native.package_name()
)
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.
Defaults to ""
config_file
(Label, mandatory): cypress.json configuration file. See https://docs.cypress.io/guides/references/configuration
configuration_env_vars
(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.
Defaults to []
cypress_npm_package
(Label): The cypress npm package. If you installed cypress as a peer dependency, this should not need to be set.
Defaults to //cypress:cypress
data
(List of labels): Runtime dependencies which may be loaded during execution.
Defaults to []
default_env_vars
(List of strings): Default environment variables that are added to configuration_env_vars
.
This is separate from the default of configuration_env_vars
so that a user can set configuration_env_vars
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
Defaults to ["VERBOSE_LOGS", "NODE_DEBUG", "RUNFILES_LIB_DEBUG"]
entry_point
(Label): Entry point JS file which bootstraps the cypress cli
Defaults to @npm//@bazel/cypress/internal:run-cypress.js
env
(Dictionary: String -> String): Specifies additional environment variables to set when the target is executed, subject to location and make variable expansion.
Defaults to {}
expected_exit_code
(Integer): The expected exit code for the test. Defaults to 0.
Defaults to 0
link_workspace_root
(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.
Defaults to False
plugin_file
(Label): Your cypress plugin file. See https://docs.cypress.io/guides/tooling/plugins-guide
Defaults to @npm//@bazel/cypress/internal:plugins/base.js
srcs
(List of labels): A list of test files. See https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Test-files
Defaults to []
templated_args
(List of strings): Arguments which are passed to every execution of the program.
To pass a node startup option, prepend it with --node_options=
, e.g.
--node_options=--preserve-symlinks
.
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 execpath
, execpaths
, rootpath
, rootpaths
, location
, and locations
take
label parameters (e.g. $(execpath //foo:bar)
) and substitute the file paths denoted by that label.
See https://docs.bazel.build/versions/main/be/make-variables.html#predefined_label_variables for more info.
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 internal/common/expand_into_runfiles.bzl
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_binary
or nodejs_test
:
BUILD.bazel:
nodejs_test(
name = "my_test",
data = [":bootstrap.js"],
templated_args = ["--node_options=--require=$$(rlocation $(rootpath :bootstrap.js))"],
)
or if you're in the context of a .js script you can pass the $(rootpath) as an argument to the script and use the javascript runfiles helper to resolve to the absolute path:
BUILD.bazel:
nodejs_test(
name = "my_test",
data = [":some_file"],
entry_point = ":my_test.js",
templated_args = ["$(rootpath :some_file)"],
)
my_test.js
const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']);
const args = process.argv.slice(2);
const some_file = runfiles.resolveWorkspaceRelative(args[0]);
NB: Bazel will error if it sees the single dollar sign $(rlocation path) in templated_args
as it will try to
expand $(rlocation)
since we now expand predefined & custom "make" variables such as $(COMPILATION_MODE)
,
$(BINDIR)
& $(TARGET_CPU)
using 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
the 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.
Defaults to []
toolchain
(Label)
Defaults to None
cypress_repositories
USAGE
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.
PARAMETERS
name
Name of the external workspace where the cypress binary lives
version
Version of cypress binary to use. Should match package.json
linux_urls
(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.
Defaults to []
linux_sha256
(Optional) SHA-256 of the linux cypress binary
Defaults to ""
darwin_urls
(Optional) URLs at which the cypress binary for darwin can be downloaded. If omitted, https://cdn.cypress.io/desktop will be used.
Defaults to []
darwin_sha256
(Optional) SHA-256 of the darwin cypress binary
Defaults to ""
darwin_arm64_urls
(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).
Defaults to []
darwin_arm64_sha256
(Optional) SHA-256 of the darwin arm64 cypress binary
Defaults to ""
windows_urls
(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.
Defaults to []
windows_sha256
(Optional) SHA-256 of the windows cypress binary
Defaults to ""