From 25c52444b54e31dff6d6260af3608d79874b33d4 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sat, 17 Apr 2021 17:05:48 +1000 Subject: [PATCH] add a hack to avoid Long.js in Jest tests --- ts/BUILD.bazel | 1 + ts/deckconfig/BUILD.bazel | 1 + ts/jest.bzl | 15 +++++++++++++-- ts/protobuf-no-long.js | 3 +++ 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 ts/protobuf-no-long.js diff --git a/ts/BUILD.bazel b/ts/BUILD.bazel index 9a3b2b827..5233fc3c5 100644 --- a/ts/BUILD.bazel +++ b/ts/BUILD.bazel @@ -32,6 +32,7 @@ exports_files([ "protobuf-shim.js", "jest.config.js", "package.json", + "protobuf-no-long.js", ]) alias( diff --git a/ts/deckconfig/BUILD.bazel b/ts/deckconfig/BUILD.bazel index 7b8721451..f606822a4 100644 --- a/ts/deckconfig/BUILD.bazel +++ b/ts/deckconfig/BUILD.bazel @@ -126,6 +126,7 @@ svelte_check( ) jest_test( + protobuf = True, deps = [ ":lib", "//ts/lib:backend_proto", diff --git a/ts/jest.bzl b/ts/jest.bzl index 5932b017b..f454d01b0 100644 --- a/ts/jest.bzl +++ b/ts/jest.bzl @@ -2,7 +2,7 @@ load("@npm//@bazel/typescript:index.bzl", "ts_library") load("@esbuild_toolchain//:esbuild.bzl", esbuild = "esbuild_macro") load("@npm//jest-cli:index.bzl", _jest_test = "jest_test") -def jest_test(deps, name = "jest"): +def jest_test(deps, name = "jest", protobuf = False): "Build *.test.ts into a library, then test it with Jest." ts_sources = native.glob(["*.test.ts"]) @@ -19,6 +19,16 @@ def jest_test(deps, name = "jest"): # bundle each test file up with its dependencies for jest bundled_srcs = [] + esbuild_extra_args = [] + esbuild_extra_srcs = [] + if protobuf: + esbuild_extra_args.append( + "--inject:$(location //ts:protobuf-no-long.js)", + ) + esbuild_extra_srcs.append( + "//ts:protobuf-no-long.js", + ) + for ts_src in ts_sources: base = ts_src.replace(".test.ts", "") bundle_name = base + ".bundle.test" @@ -31,9 +41,10 @@ def jest_test(deps, name = "jest"): "--platform=node", "--external:protobufjs", "--keep-names", - ], + ] + esbuild_extra_args, entry_point = ts_src, output = bundle_name + ".js", + srcs = esbuild_extra_srcs, deps = [ name + "_lib", ] + deps, diff --git a/ts/protobuf-no-long.js b/ts/protobuf-no-long.js new file mode 100644 index 000000000..561ee2c26 --- /dev/null +++ b/ts/protobuf-no-long.js @@ -0,0 +1,3 @@ +let protobuf = require("protobufjs"); +protobuf.util.Long = undefined; +protobuf.configure();