Skip to content

Instantly share code, notes, and snippets.

@antholeole
Created June 29, 2024 00:48
Show Gist options
  • Save antholeole/8c7e9744394ddde12325a5b5b2783fee to your computer and use it in GitHub Desktop.
Save antholeole/8c7e9744394ddde12325a5b5b2783fee to your computer and use it in GitHub Desktop.
a BUILD.bazel that builds a container that runs jest tests (https://anthonyoleinik.com/blog_directory/bazel-jest-container/)
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("@integration_npm//:defs.bzl", "npm_link_all_packages", "npm_link_targets")
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_image_layer")
load("@rules_oci//oci:defs.bzl", "oci_image")
load("@aspect_rules_jest//jest:defs.bzl", "jest_test")
load("@aspect_bazel_lib//lib:copy_file.bzl", "copy_file")
# The jest configuration file for the docker image to pick up.
copy_file(
name = "runtime_config",
src = select({
"//bazel:local_registry": "configs/jest.local.config.js",
"//bazel:staging_registry": "configs/jest.staging.config.js",
# default to the local registry.
"//conditions:default": "configs/jest.local.config.js",
}),
out = "jest.config.js",
)
npm_link_all_packages(name = "node_modules")
ts_project(
name = "integration_test_js",
srcs = glob(["src/**/*.ts"]) + [
"decs.d.ts"
],
deps = [":node_modules"],
)
# so you can run locally against a minikube.
jest_test(
name = "tests",
config = "configs/jest.local.config.js",
data = [
":integration_test_js",
],
node_modules = ":node_modules",
)
# bazel run this will NOT work; bazel does not
# understand how to decypher the symlinks here.
js_binary(
name = "test_binary",
chdir = package_name(),
data = [
":integration_test_js",
":runtime_config",
"//integration_tests:node_modules",
"driver.js"
],
# make this private so no one thinks this is a runnable target;
# it will not work outside of the container.
visibility = ["//visibility:private"],
entry_point = "driver.js"
)
js_image_layer(
name = "test_layer",
binary = ":test_binary",
root = "/tests"
)
CMD = "/tests/integration_tests/test_binary"
oci_image(
name = "integration_test_image",
base = "@ubuntu_linux_amd64",
cmd = [CMD],
entrypoint = ["bash"],
tars = [
":test_layer",
],
visibility = ["//visibility:public"],
workdir = select({
"@aspect_bazel_lib//lib:bzlmod": CMD + ".runfiles/_main",
"//conditions:default": CMD + ".runfiles/__main__",
}),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment