Skip to content

Commit

Permalink
fix(examples): run jest updateSnapshot in the workspace (#3041)
Browse files Browse the repository at this point in the history
Otherwise it creates snapshot files in the runfiles tree
  • Loading branch information
alexeagle committed Dec 3, 2021
1 parent 96d1e5a commit e005d82
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions examples/jest/jest.bzl
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
)

0 comments on commit e005d82

Please sign in to comment.