From 279acfb88ebd4748917cc2296d1c16631caf7dff Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Sat, 20 Apr 2024 15:37:15 -0700 Subject: [PATCH] feat: upgrade to rules_js 2.0 --- MODULE.bazel | 11 +++++------ WORKSPACE | 13 +++---------- docs/jest_test.md | 27 +++++++++++++-------------- e2e/case4/WORKSPACE.bazel | 9 +++------ e2e/npm_packages/MODULE.bazel | 4 ++-- e2e/npm_packages/WORKSPACE.bazel | 14 +++----------- e2e/smoke/MODULE.bazel | 5 +++-- e2e/smoke/WORKSPACE.bazel | 9 +++------ e2e/swc/MODULE.bazel | 6 +++--- jest/defs.bzl | 6 +----- jest/dependencies.bzl | 21 +++++++++++---------- jest/private/jest_test.bzl | 10 ++++++---- 12 files changed, 56 insertions(+), 79 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index 1a2904c..a205fb5 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -6,16 +6,15 @@ module( compatibility_level = 1, ) -bazel_dep(name = "aspect_bazel_lib", version = "1.32.1") -bazel_dep(name = "aspect_rules_js", version = "1.31.0") -bazel_dep(name = "bazel_features", version = "1.9.0") -bazel_dep(name = "bazel_skylib", version = "1.4.1") -bazel_dep(name = "rules_nodejs", version = "5.8.2") +# Lower-bounds (minimum) versions for direct runtime dependencies +bazel_dep(name = "aspect_bazel_lib", version = "2.7.1") +bazel_dep(name = "aspect_rules_js", version = "2.0.0-alpha.2") +bazel_dep(name = "bazel_skylib", version = "1.5.0") +bazel_dep(name = "rules_nodejs", version = "6.1.0") ####### Dev dependencies ######## bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.5.0", dev_dependency = True) bazel_dep(name = "buildifier_prebuilt", version = "6.4.0", dev_dependency = True) bazel_dep(name = "gazelle", version = "0.36.0", dev_dependency = True, repo_name = "bazel_gazelle") -bazel_dep(name = "rules_go", version = "0.46.0", dev_dependency = True) bazel_dep(name = "stardoc", version = "0.6.2", dev_dependency = True, repo_name = "io_bazel_stardoc") diff --git a/WORKSPACE b/WORKSPACE index 1ea8f35..750f43e 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -11,16 +11,9 @@ load("//jest:dependencies.bzl", "rules_jest_dependencies") # Fetch dependencies which users need as well rules_jest_dependencies() -load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies") +load("@aspect_rules_js//js:toolchains.bzl", "DEFAULT_NODE_VERSION", "rules_js_register_toolchains") -aspect_bazel_lib_dependencies(override_local_config_platform = True) - -load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains") - -nodejs_register_toolchains( - name = "nodejs", - node_version = DEFAULT_NODE_VERSION, -) +rules_js_register_toolchains(node_version = DEFAULT_NODE_VERSION) # For running our own unit tests load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") @@ -48,7 +41,7 @@ local_repository( ############################################ # Example npm dependencies -load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock") +load("@aspect_rules_js//npm:repositories.bzl", "npm_translate_lock") npm_translate_lock( name = "npm", diff --git a/docs/jest_test.md b/docs/jest_test.md index 1c4a40e..d8bedaf 100644 --- a/docs/jest_test.md +++ b/docs/jest_test.md @@ -2,7 +2,6 @@ # Public API for Jest rules - ## jest_test @@ -26,18 +25,18 @@ Supports updating snapshots with `bazel run {name}_update_snapshots` if `snapsho | Name | Description | Default Value | | :------------- | :------------- | :------------- | | name | A unique name for this target. | none | -| node_modules | Label pointing to the linked node_modules target where jest is linked, e.g. //:node_modules. jest-cli must be linked into the node_modules supplied. jest-junit is also required by default when auto_configure_reporters is True.

NB: Only the required npm packages are included in data from //:node_modules. Other npm packages are not included as inputs. | none | -| config | "Optional Jest config file. See https://jestjs.io/docs/configuration.

Supported config file types are ".js", ".cjs", ".mjs", ".json" which come from https://jestjs.io/docs/configuration minus TypeScript since we this rule extends from the configuration. TypeScript jest configs should be transpiled before being passed to jest_test with [rules_ts](https://github.com/aspect-build/rules_ts). | None | -| data | Runtime dependencies of the Jest test.

This should include all test files, configuration files & files under test. | [] | -| snapshots | If True, a {name}_update_snapshots binary target is generated that will update all existing __snapshots__ directories when bazel run. This is the equivalent to running jest -u or jest --updateSnapshot outside of Bazel, except that new __snapshots__ will not automatically be created on update. To bootstrap a new __snapshots__ directory, you can create an empty one and then run the {name}_update_snapshots target to populate it.

If the name of the snapshot directory is not the default __snapshots__ because of a custom snapshot resolver, you can specify customize the snapshot directories with a glob or a static list. For example,

 jest_test(     name = "test",     node_modules = "//:node_modules",     config = "jest.config.js",     data = [         "greetings/greetings.js",         "greetings/greetings.test.js",         "link/link.js",         "link/link.test.js",     ],     snapshots = glob(["**/__snaps__"], exclude_directories = 0), ) 


or with a static list,

     snapshots = [         "greetings/__greetings_snaps__",         "link/__link_snaps__",     ] 


Snapshots directories must not contain any files except for snapshots. There must also be no BUILD files in the snapshots directories since they must be part of the same Bazel package that the jest_test target is in.

If snapshots are _not_ configured to output to a directory that contains only snapshots, you may alternately set snapshots to a list of snapshot files expected to be generated by this jest_test target. These must be source files and all snapshots that are generated must be explicitly listed. You may use a glob such as glob(["**/*.snap"]) to generate this list, in which case all snapshots must already be on disk so they are discovered by glob. | False | -| run_in_band | When True, the --runInBand argument is passed to the Jest CLI so that all tests are run serially in the current process, rather than creating a worker pool of child processes that run tests. See https://jestjs.io/docs/cli#--runinband for more info.

This is the desired default behavior under Bazel since Bazel expect each test process to use up one CPU core. To parallelize a single jest_test across many cores, use shard_count instead which is supported by jest_test. See https://docs.bazel.build/versions/main/test-encyclopedia.html#test-sharding. | True | -| colors | When True, the --colors argument is passed to the Jest CLI. See https://jestjs.io/docs/cli#--colors. | True | -| auto_configure_reporters | Let jest_test configure reporters for Bazel test and xml test logs.

When enabled, jest-junit must be linked to the supplied node_modules tree.

The default reporter is used for the standard test log and jest-junit is used for the xml log. These reporters are appended to the list of reporters from the user Jest config only if they are not already set.

The JEST_JUNIT_OUTPUT_FILE environment variable is always set to where Bazel expects a test runner to write its xml test log so that if jest-junit is configured in the user Jest config it will output the junit xml file where Bazel expects by default. | True | -| auto_configure_test_sequencer | Let jest_test configure a custom test sequencer for Bazel test that support Bazel sharding.

Any custom testSequencer value in a user Jest config will be overridden.

See https://jestjs.io/docs/configuration#testsequencer-string for more information on Jest testSequencer config option. | True | -| snapshots_ext | The expected extensions for snapshot files. Defaults to .snap, the Jest default. | ".snap" | -| quiet_snapshot_updates | When True, snapshot update stdout & stderr is hidden when the snapshot update is successful.

On a snapshot update failure, its stdout & stderr will always be shown. | False | -| timeout | standard attribute for tests. Defaults to "short" if both timeout and size are unspecified. | None | -| size | standard attribute for tests | None | -| kwargs | Additional named parameters passed to both js_test and js_binary. See https://github.com/aspect-build/rules_js/blob/main/docs/js_binary.md | none | +| node_modules | Label pointing to the linked node_modules target where jest is linked, e.g. `//:node_modules`. `jest-cli` must be linked into the node_modules supplied. `jest-junit` is also required by default when `auto_configure_reporters` is True.

NB: Only the required npm packages are included in data from `//:node_modules`. Other npm packages are not included as inputs. | none | +| config | "Optional Jest config file. See https://jestjs.io/docs/configuration.

Supported config file types are ".js", ".cjs", ".mjs", ".json" which come from https://jestjs.io/docs/configuration minus TypeScript since we this rule extends from the configuration. TypeScript jest configs should be transpiled before being passed to jest_test with [rules_ts](https://github.com/aspect-build/rules_ts). | `None` | +| data | Runtime dependencies of the Jest test.

This should include all test files, configuration files & files under test. | `[]` | +| snapshots | If True, a `{name}_update_snapshots` binary target is generated that will update all existing `__snapshots__` directories when `bazel run`. This is the equivalent to running `jest -u` or `jest --updateSnapshot` outside of Bazel, except that new `__snapshots__` will not automatically be created on update. To bootstrap a new `__snapshots__` directory, you can create an empty one and then run the `{name}_update_snapshots` target to populate it.

If the name of the snapshot directory is not the default `__snapshots__` because of a custom snapshot resolver, you can specify customize the snapshot directories with a `glob` or a static list. For example,

jest_test(
    name = "test",
    node_modules = "//:node_modules",
    config = "jest.config.js",
    data = [
        "greetings/greetings.js",
        "greetings/greetings.test.js",
        "link/link.js",
        "link/link.test.js",
    ],
    snapshots = glob(["**/__snaps__"], exclude_directories = 0),
)


or with a static list,

    snapshots = [
        "greetings/__greetings_snaps__",
        "link/__link_snaps__",
    ]


Snapshots directories must not contain any files except for snapshots. There must also be no BUILD files in the snapshots directories since they must be part of the same Bazel package that the `jest_test` target is in.

If snapshots are _not_ configured to output to a directory that contains only snapshots, you may alternately set `snapshots` to a list of snapshot files expected to be generated by this `jest_test` target. These must be source files and all snapshots that are generated must be explicitly listed. You may use a `glob` such as `glob(["**/*.snap"])` to generate this list, in which case all snapshots must already be on disk so they are discovered by `glob`. | `False` | +| run_in_band | When True, the `--runInBand` argument is passed to the Jest CLI so that all tests are run serially in the current process, rather than creating a worker pool of child processes that run tests. See https://jestjs.io/docs/cli#--runinband for more info.

This is the desired default behavior under Bazel since Bazel expect each test process to use up one CPU core. To parallelize a single jest_test across many cores, use `shard_count` instead which is supported by `jest_test`. See https://docs.bazel.build/versions/main/test-encyclopedia.html#test-sharding. | `True` | +| colors | When True, the `--colors` argument is passed to the Jest CLI. See https://jestjs.io/docs/cli#--colors. | `True` | +| auto_configure_reporters | Let jest_test configure reporters for Bazel test and xml test logs.

When enabled, `jest-junit` must be linked to the supplied node_modules tree.

The `default` reporter is used for the standard test log and `jest-junit` is used for the xml log. These reporters are appended to the list of reporters from the user Jest `config` only if they are not already set.

The `JEST_JUNIT_OUTPUT_FILE` environment variable is always set to where Bazel expects a test runner to write its xml test log so that if `jest-junit` is configured in the user Jest `config` it will output the junit xml file where Bazel expects by default. | `True` | +| auto_configure_test_sequencer | Let jest_test configure a custom test sequencer for Bazel test that support Bazel sharding.

Any custom testSequencer value in a user Jest `config` will be overridden.

See https://jestjs.io/docs/configuration#testsequencer-string for more information on Jest testSequencer config option. | `True` | +| snapshots_ext | The expected extensions for snapshot files. Defaults to `.snap`, the Jest default. | `".snap"` | +| quiet_snapshot_updates | When True, snapshot update stdout & stderr is hidden when the snapshot update is successful.

On a snapshot update failure, its stdout & stderr will always be shown. | `False` | +| timeout | standard attribute for tests. Defaults to "short" if both timeout and size are unspecified. | `None` | +| size | standard attribute for tests | `None` | +| kwargs | Additional named parameters passed to both `js_test` and `js_binary`. See https://github.com/aspect-build/rules_js/blob/main/docs/js_binary.md | none | diff --git a/e2e/case4/WORKSPACE.bazel b/e2e/case4/WORKSPACE.bazel index 182085e..60c104d 100644 --- a/e2e/case4/WORKSPACE.bazel +++ b/e2e/case4/WORKSPACE.bazel @@ -7,14 +7,11 @@ load("@aspect_rules_jest//jest:dependencies.bzl", "rules_jest_dependencies") rules_jest_dependencies() -load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains") +load("@aspect_rules_js//js:toolchains.bzl", "DEFAULT_NODE_VERSION", "rules_js_register_toolchains") -nodejs_register_toolchains( - name = "nodejs", - node_version = DEFAULT_NODE_VERSION, -) +rules_js_register_toolchains(node_version = DEFAULT_NODE_VERSION) -load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock") +load("@aspect_rules_js//npm:repositories.bzl", "npm_translate_lock") npm_translate_lock( name = "npm", diff --git a/e2e/npm_packages/MODULE.bazel b/e2e/npm_packages/MODULE.bazel index 9d5e799..6740977 100644 --- a/e2e/npm_packages/MODULE.bazel +++ b/e2e/npm_packages/MODULE.bazel @@ -4,8 +4,8 @@ local_path_override( path = "../..", ) -bazel_dep(name = "aspect_bazel_lib", version = "1.32.1", dev_dependency = True) -bazel_dep(name = "aspect_rules_js", version = "1.31.0", dev_dependency = True) +bazel_dep(name = "aspect_bazel_lib", version = "2.7.1", dev_dependency = True) +bazel_dep(name = "aspect_rules_js", version = "2.0.0-alpha.2", dev_dependency = True) npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm", dev_dependency = True) npm.npm_translate_lock( diff --git a/e2e/npm_packages/WORKSPACE.bazel b/e2e/npm_packages/WORKSPACE.bazel index 3ee99cd..60c104d 100644 --- a/e2e/npm_packages/WORKSPACE.bazel +++ b/e2e/npm_packages/WORKSPACE.bazel @@ -7,19 +7,11 @@ load("@aspect_rules_jest//jest:dependencies.bzl", "rules_jest_dependencies") rules_jest_dependencies() -# Register a nodejs toolchain, if you haven't already done so. -load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains") +load("@aspect_rules_js//js:toolchains.bzl", "DEFAULT_NODE_VERSION", "rules_js_register_toolchains") -nodejs_register_toolchains( - name = "nodejs", - node_version = DEFAULT_NODE_VERSION, -) - -load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies") - -rules_js_dependencies() +rules_js_register_toolchains(node_version = DEFAULT_NODE_VERSION) -load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock") +load("@aspect_rules_js//npm:repositories.bzl", "npm_translate_lock") npm_translate_lock( name = "npm", diff --git a/e2e/smoke/MODULE.bazel b/e2e/smoke/MODULE.bazel index e5b88b0..507004b 100644 --- a/e2e/smoke/MODULE.bazel +++ b/e2e/smoke/MODULE.bazel @@ -4,8 +4,9 @@ local_path_override( path = "../..", ) -bazel_dep(name = "aspect_rules_js", version = "1.31.0", dev_dependency = True) -bazel_dep(name = "platforms", version = "0.0.4", dev_dependency = True) +bazel_dep(name = "aspect_bazel_lib", version = "2.7.1", dev_dependency = True) +bazel_dep(name = "aspect_rules_js", version = "2.0.0-alpha.2", dev_dependency = True) +bazel_dep(name = "platforms", version = "0.0.9", dev_dependency = True) npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm", dev_dependency = True) npm.npm_translate_lock( diff --git a/e2e/smoke/WORKSPACE.bazel b/e2e/smoke/WORKSPACE.bazel index 32aa48c..c05e20f 100644 --- a/e2e/smoke/WORKSPACE.bazel +++ b/e2e/smoke/WORKSPACE.bazel @@ -18,14 +18,11 @@ load("@aspect_rules_jest//jest:dependencies.bzl", "rules_jest_dependencies") rules_jest_dependencies() -load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains") +load("@aspect_rules_js//js:toolchains.bzl", "DEFAULT_NODE_VERSION", "rules_js_register_toolchains") -nodejs_register_toolchains( - name = "nodejs", - node_version = DEFAULT_NODE_VERSION, -) +rules_js_register_toolchains(node_version = DEFAULT_NODE_VERSION) -load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock") +load("@aspect_rules_js//npm:repositories.bzl", "npm_translate_lock") npm_translate_lock( name = "npm", diff --git a/e2e/swc/MODULE.bazel b/e2e/swc/MODULE.bazel index 7ecde98..f3844e1 100644 --- a/e2e/swc/MODULE.bazel +++ b/e2e/swc/MODULE.bazel @@ -4,10 +4,10 @@ local_path_override( path = "../..", ) -bazel_dep(name = "aspect_bazel_lib", version = "1.32.1", dev_dependency = True) -bazel_dep(name = "aspect_rules_js", version = "1.31.0", dev_dependency = True) +bazel_dep(name = "aspect_bazel_lib", version = "2.7.1", dev_dependency = True) +bazel_dep(name = "aspect_rules_js", version = "2.0.0-alpha.2", dev_dependency = True) bazel_dep(name = "aspect_rules_swc", version = "1.2.2", dev_dependency = True) -bazel_dep(name = "platforms", version = "0.0.4", dev_dependency = True) +bazel_dep(name = "platforms", version = "0.0.9", dev_dependency = True) npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm", dev_dependency = True) npm.npm_translate_lock( diff --git a/jest/defs.bzl b/jest/defs.bzl index f6622bf..a611cc0 100644 --- a/jest/defs.bzl +++ b/jest/defs.bzl @@ -23,11 +23,7 @@ def _jest_from_node_modules(jest_rule, name, node_modules, auto_configure_report jest_rule( name = name, enable_runfiles = select({ - "@aspect_rules_js//js/private:enable_runfiles": True, - "//conditions:default": False, - }), - unresolved_symlinks_enabled = select({ - "@aspect_rules_js//js/private:experimental_allow_unresolved_symlinks": True, + "@aspect_bazel_lib//lib:enable_runfiles": True, "//conditions:default": False, }), data = data, diff --git a/jest/dependencies.bzl b/jest/dependencies.bzl index f515aa8..74e3759 100644 --- a/jest/dependencies.bzl +++ b/jest/dependencies.bzl @@ -6,28 +6,29 @@ load("//jest/private:maybe.bzl", http_archive = "maybe_http_archive") def rules_jest_dependencies(): http_archive( name = "bazel_skylib", - sha256 = "b8a1527901774180afc798aeb28c4634bdccf19c4d98e7bdd1ce79d1fe9aaad7", - urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.4.1/bazel-skylib-1.4.1.tar.gz"], + sha256 = "cd55a062e763b9349921f0f5db8c3933288dc8ba4f76dd9416aac68acee3cb94", + urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz"], ) http_archive( name = "aspect_bazel_lib", - sha256 = "e3151d87910f69cf1fc88755392d7c878034a69d6499b287bcfc00b1cf9bb415", - strip_prefix = "bazel-lib-1.32.1", - url = "https://github.com/aspect-build/bazel-lib/releases/download/v1.32.1/bazel-lib-v1.32.1.tar.gz", + sha256 = "b554eb7942a5ab44c90077df6a0c76fc67c5874c9446a007e9ba68be82bd4796", + strip_prefix = "bazel-lib-2.7.1", + url = "https://github.com/aspect-build/bazel-lib/releases/download/v2.7.1/bazel-lib-v2.7.1.tar.gz", ) http_archive( name = "aspect_rules_js", - sha256 = "7b2a4d1d264e105eae49a27e2e78065b23e2e45724df2251eacdd317e95bfdfd", - strip_prefix = "rules_js-1.31.0", - url = "https://github.com/aspect-build/rules_js/releases/download/v1.31.0/rules_js-v1.31.0.tar.gz", + sha256 = "b627acf34f928507dfabae69d3fd8702ce3d45bed568ad4c44b243cb789f08c6", + strip_prefix = "rules_js-2.0.0-alpha.2", + url = "https://github.com/aspect-build/rules_js/releases/download/v2.0.0-alpha.2/rules_js-v2.0.0-alpha.2.tar.gz", ) http_archive( name = "rules_nodejs", - sha256 = "764a3b3757bb8c3c6a02ba3344731a3d71e558220adcb0cf7e43c9bba2c37ba8", - urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.8.2/rules_nodejs-core-5.8.2.tar.gz"], + sha256 = "dddd60acc3f2f30359bef502c9d788f67e33814b0ddd99aa27c5a15eb7a41b8c", + strip_prefix = "rules_nodejs-6.1.0", + url = "https://github.com/bazelbuild/rules_nodejs/releases/download/v6.1.0/rules_nodejs-v6.1.0.tar.gz", ) http_archive( diff --git a/jest/private/jest_test.bzl b/jest/private/jest_test.bzl index 6c5c4b5..24dd019 100644 --- a/jest/private/jest_test.bzl +++ b/jest/private/jest_test.bzl @@ -129,11 +129,13 @@ def _impl(ctx): runfiles = ctx.runfiles( files = files, - transitive_files = js_lib_helpers.gather_files_from_js_providers( - targets = ctx.attr.data + [ctx.attr.config] if ctx.attr.config else [], + transitive_files = js_lib_helpers.gather_files_from_js_infos( + targets = ctx.attr.data + [ctx.attr.config] if ctx.attr.config else ctx.attr.data, + include_sources = ctx.attr.include_sources, + include_types = ctx.attr.include_types, include_transitive_sources = ctx.attr.include_transitive_sources, - include_declarations = ctx.attr.include_declarations, - include_npm_linked_packages = ctx.attr.include_npm_linked_packages, + include_transitive_types = ctx.attr.include_transitive_types, + include_npm_sources = ctx.attr.include_npm_sources, ), ).merge(launcher.runfiles).merge_all([ target[DefaultInfo].default_runfiles