Skip to content

Commit b99ca1e

Browse files
committed
feat(bazel): create a base jasmine_test with source map and relative path rewrite support (#3025)
PR Close #3025
1 parent fc1176a commit b99ca1e

File tree

6 files changed

+77
-2
lines changed

6 files changed

+77
-2
lines changed

bazel/jasmine/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("@aspect_rules_js//js:defs.bzl", "js_library")
2+
3+
js_library(
4+
name = "stack-traces",
5+
srcs = [
6+
"stack-traces.mjs",
7+
],
8+
visibility = [
9+
"//visibility:public",
10+
],
11+
deps = [
12+
"//bazel:node_modules/source-map-support",
13+
],
14+
)

bazel/jasmine/jasmine.bzl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load("@aspect_rules_jasmine//jasmine:defs.bzl", _jasmine_test = "jasmine_test")
2+
3+
def jasmine_test(name, data = [], node_options = [], **kwargs):
4+
_jasmine_test(
5+
name = name,
6+
data = data + [
7+
"@devinfra//bazel/jasmine:stack-traces",
8+
],
9+
size = kwargs.pop("size", "medium"),
10+
node_options = [
11+
"--import",
12+
"$$JS_BINARY__RUNFILES/$(rlocationpath @devinfra//bazel/jasmine:stack-traces)",
13+
] + node_options,
14+
**kwargs
15+
)

bazel/jasmine/stack-traces.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {install} from 'source-map-support';
2+
3+
// Set up source map support to ensure we are logging the stack trace for the source file (i.e. .ts file) and
4+
// not the generated file (i.e. .js. file).
5+
install();
6+
7+
/** The root path that the test files are running from within. */
8+
let rootPath = `${process.env.RUNFILES}/${process.env.TEST_WORKSPACE}/`;
9+
/** The root path match for when test files are not within the sandbox, but the executation is happening within the sandbox. */
10+
let sandboxPath = `/.*${process.env.JS_BINARY__WORKSPACE}/${process.env.JS_BINARY__BINDIR}/`;
11+
/** Regex to capture the content and name of the function in the stack trace. */
12+
const basePathRegex = new RegExp(`(at.*)(?:file.*${rootPath}|file.*${sandboxPath})`, 'g');
13+
14+
// Replace the prepareStackTrace function with one which replaces the full path execution location with
15+
// relative paths to the base of the workspace source files.
16+
const originalPrepareStackTrace = Error.prepareStackTrace;
17+
Error.prepareStackTrace = function (e, s) {
18+
return originalPrepareStackTrace(e, s).replaceAll(basePathRegex, '$1./');
19+
};

bazel/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@types/selenium-webdriver": "^4.1.28",
99
"@types/send": "0.17.5",
1010
"@types/wait-on": "^5.3.4",
11+
"@types/source-map-support": "0.5.10",
1112
"@types/yargs": "17.0.33",
1213
"browser-sync": "3.0.4",
1314
"chalk": "5.6.0",
@@ -20,6 +21,7 @@
2021
"protractor": "7.0.0",
2122
"semver": "7.7.2",
2223
"selenium-webdriver": "4.35.0",
24+
"source-map-support": "0.5.21",
2325
"tinyglobby": "0.2.14"
2426
}
2527
}

pnpm-lock.yaml

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/defaults.bzl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", _copy_to_bin = "copy_to_bin")
22
load("@aspect_bazel_lib//lib:write_source_files.bzl", _write_source_file = "write_source_file")
33
load("@aspect_rules_esbuild//esbuild:defs.bzl", _esbuild = "esbuild")
4-
load("@aspect_rules_jasmine//jasmine:defs.bzl", _jasmine_test = "jasmine_test")
54
load("@aspect_rules_js//js:defs.bzl", _js_binary = "js_binary")
65
load("@aspect_rules_js//npm:defs.bzl", _npm_package = "npm_package")
76
load("@aspect_rules_ts//ts:defs.bzl", _ts_config = "ts_config")
@@ -10,6 +9,7 @@ load("@rules_angular//src/ng_project:index.bzl", _ng_project = "ng_project")
109
load("@rules_angular//src/ts_project:index.bzl", _ts_project = "ts_project")
1110
load("@rules_sass//src:index.bzl", _npm_sass_library = "npm_sass_library", _sass_binary = "sass_binary")
1211
load("//bazel:extract_types.bzl", _extract_types = "extract_types")
12+
load("//bazel/jasmine:jasmine.bzl", _jasmine_test = "jasmine_test")
1313
load("//bazel/ts_project:index.bzl", _strict_deps_test = "strict_deps_test")
1414

1515
copy_to_bin = _copy_to_bin
@@ -110,7 +110,6 @@ def jasmine_test(name, **kwargs):
110110
_jasmine_test(
111111
name = name,
112112
node_modules = "//:node_modules",
113-
chdir = native.package_name(),
114113
fixed_args = [
115114
"'**/*+(.|_)spec.js'",
116115
],

0 commit comments

Comments
 (0)