diff --git a/examples/jest/jest.bzl b/examples/jest/jest.bzl index 02dbe98484..7978899867 100644 --- a/examples/jest/jest.bzl +++ b/examples/jest/jest.bzl @@ -1,6 +1,7 @@ "Shows how you might create a macro for the autogenerated Jest rule" load("@npm//jest-cli:index.bzl", "jest", _jest_test = "jest_test") +load("@bazel_skylib//rules:write_file.bzl", "write_file") def jest_test(name, srcs, deps, jest_config, **kwargs): "A macro around the autogenerated jest_test rule" @@ -22,10 +23,27 @@ def jest_test(name, srcs, deps, jest_config, **kwargs): **kwargs ) + # Make sure the update command runs with a working directory in the workspace + # so that any created snapshot files are in the sources, not in the runfiles + write_file( + name = "chdir", + out = "chdir.js", + content = [ + # cd /path/to/workspace + "process.chdir(process.env['BUILD_WORKSPACE_DIRECTORY'])", + # cd subdir/package + "process.chdir('%s')" % native.package_name() if native.package_name() else "", + ], + ) + # This rule is used specifically to update snapshots via `bazel run` jest( name = "%s.update" % name, - data = data, - templated_args = templated_args + ["-u"], + data = data + ["chdir.js"], + templated_args = templated_args + [ + "--updateSnapshot", + "--runInBand", + "--node_options=--require=$(rootpath chdir.js)", + ], **kwargs )