Skip to content

Commit 361ceb6

Browse files
committed
fix(bazel): allow types to be provided properly for usage in api_golden tests (#2926)
Correct the types being included during api_golden tests PR Close #2926
1 parent d7a36bc commit 361ceb6

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

bazel/api-golden/api_golden_test.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def api_golden_test(
99
entry_point,
1010
data = [],
1111
strip_export_pattern = default_strip_export_pattern,
12-
types = [],
12+
types = {},
1313
**kwargs):
1414
# We can't directly write `package.json` as this could cause conflicts
1515
# if there are multiple individual file tests in the same Bazel package.

bazel/api-golden/api_golden_test_npm_package.bzl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def api_golden_test_npm_package(
1212
npm_package,
1313
data = [],
1414
strip_export_pattern = default_strip_export_pattern,
15-
types = [],
15+
types = {},
1616
**kwargs):
1717
"""Builds an API report for all entry-points within the given NPM package and compares it.
1818
@@ -31,13 +31,22 @@ def api_golden_test_npm_package(
3131
kwargs["tags"] = kwargs.get("tags", []) + ["api_guard"]
3232

3333
data.append("@devinfra//bazel/api-golden")
34-
data.extend(types)
34+
35+
types_name_and_path = []
36+
for label, n in types.items():
37+
data.append(label)
38+
type_label = Label(label)
39+
40+
# The path for the package is determined by the package location and name and found relative
41+
# to the exec root during our js_test and js_binary runs.
42+
path = "{package}/{name}".format(package = type_label.package, name = type_label.name)
43+
types_name_and_path.append("{name}|{path}".format(name = n, path = path))
3544

3645
js_test(
3746
name = name,
3847
data = data,
3948
entry_point = "@devinfra//bazel/api-golden:index_npm_packages.cjs",
40-
args = [golden_dir, npm_package, "false", quoted_export_pattern],
49+
args = [golden_dir, npm_package, "false", quoted_export_pattern] + types_name_and_path,
4150
**kwargs
4251
)
4352

@@ -46,6 +55,6 @@ def api_golden_test_npm_package(
4655
testonly = True,
4756
data = data,
4857
entry_point = "@devinfra//bazel/api-golden:index_npm_packages.cjs",
49-
args = [golden_dir, npm_package, "true", quoted_export_pattern],
58+
args = [golden_dir, npm_package, "true", quoted_export_pattern] + types_name_and_path,
5059
**kwargs
5160
)

bazel/api-golden/index_npm_packages.cts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async function main(
3232
npmPackageDir: string,
3333
approveGolden: boolean,
3434
stripExportPattern: RegExp,
35-
typeNames: string[],
35+
typeNamesAndLocations: string[],
3636
) {
3737
/** Whether the goldenDir provided is actually pointing to a single file. */
3838
const singleFileMode = fs.existsSync(goldenDir) && fs.statSync(goldenDir).isFile();
@@ -72,7 +72,7 @@ async function main(
7272
const actual = await worker.run([
7373
typesEntryPointPath,
7474
stripExportPattern,
75-
typeNames,
75+
typeNamesAndLocations,
7676
packageJsonPath,
7777
moduleName,
7878
]);

bazel/api-golden/test/BUILD.bazel

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ api_golden_test_npm_package(
3333
npm_package = "bazel/api-golden/test/fixtures/test_package",
3434
# API extractor needs to be able to resolve `@babel/core` due to an aliased namespace
3535
# we expose as part of the `nested.d.ts` fake entry-point.
36-
types = ["//bazel:node_modules/@types/babel__core"],
36+
types = {
37+
"//bazel:node_modules/@types/babel__core": "babel",
38+
},
3739
)
3840

3941
api_golden_test_npm_package(

bazel/api-golden/test_api_report.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ const _origFetchAstModuleExportInfo = ExportAnalyzer.prototype.fetchAstModuleExp
3737
* @param indexFilePath Entry point file that is analyzed to build the API report.
3838
* @param stripExportPattern Regular Expression that can be used to filter out exports
3939
* from the API report.
40-
* @param typePackageNames Package names for which types should be included in the analysis of the
41-
* API-report entry-point. Packages are expected to exist within the external `npm` workspace.
40+
* @param typeNamesAndLocations Package names and their relative filepath location with a |
41+
* character as a separator in the string. Describes which types should be included in the
42+
* analysis of the API-report entry-point. Packages are expected to exist within the
43+
* external `npm` workspace.
4244
* @param packageJsonPath Optional path to a `package.json` file that contains the entry
4345
* point. Note that the `package.json` is currently only used by `api-extractor` to determine
4446
* the package name displayed within the API golden.
@@ -49,17 +51,26 @@ const _origFetchAstModuleExportInfo = ExportAnalyzer.prototype.fetchAstModuleExp
4951
export async function testApiGolden(
5052
indexFilePath: string,
5153
stripExportPattern: RegExp,
52-
typeNames: string[],
54+
typeNamesAndLocations: string[],
5355
packageJsonPath: string,
5456
customPackageName: string,
5557
): Promise<string | null> {
5658
const tempDir =
5759
process.env.TEST_TMPDIR ??
5860
(await fs.promises.mkdtemp(path.join(os.tmpdir(), 'api-golden-rule')));
61+
const typeNames: string[] = [];
62+
const typeLocations: string[] = [];
63+
64+
typeNamesAndLocations.forEach((nameAndLocation) => {
65+
const [name, location] = nameAndLocation.split('|');
66+
typeNames.push(name);
67+
typeLocations.push(`./${location}`);
68+
});
69+
5970
const configObject: IConfigFile = {
6071
compiler: {
6172
overrideTsconfig: {
62-
files: [indexFilePath],
73+
files: [indexFilePath, ...typeLocations],
6374
compilerOptions: {
6475
paths: {},
6576
types: typeNames,

0 commit comments

Comments
 (0)