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/cypress//:index.bzl", "cypress_web_test")
load("@npm//@bazel/typescript:index.bzl", "ts_library")

# 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",
)

Rules

cypress_toolchain

Defines a cypress toolchain.

For usage see https://docs.bazel.build/versions/main/toolchains.html#defining-toolchains.

name

A unique name for this target.

cypress_bin

A hermetically downloaded cypress executable binary for the target platform.

cypress_bin_path

Path to an existing cypress executable for the target platform.

cypress_files

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

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.

name

A unique name for this target.

chdir

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.

config_file

cypress.json configuration file. See https://docs.cypress.io/guides/references/configuration

configuration_env_vars

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.

cypress_npm_package

The cypress npm package. If you installed cypress as a peer dependency, this should not need to be set.

data

Runtime dependencies which may be loaded during execution.

default_env_vars

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

entry_point

Entry point JS file which bootstraps the cypress cli

env

Specifies additional environment variables to set when the target is executed, subject to location expansion.

expected_exit_code

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.

plugin_file

Your cypress plugin file. See https://docs.cypress.io/guides/tooling/plugins-guide

srcs

A list of test files. See https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Test-files

templated_args

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.

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

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

cypress_repositories

Repository rule used to install cypress binary.

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.

linux_sha256

(Optional) SHA-256 of the linux cypress binary

darwin_urls

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

darwin_sha256

(Optional) SHA-256 of the darwin cypress binary

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.

windows_sha256

(Optional) SHA-256 of the windows cypress binary