In my working Bazel BUILD file, I have set up the following configurations:
package(default_visibility = ["//visibility:public"])
load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
load("@npm_bazel_typescript//:index.bzl", "ts_library")
# TODO run jest tests and stop build if test not passes
# TODO also run tests from dependent packages
load("@lbm//:jest.bzl", "jest_test")
jest_test(
name = "test",
srcs = glob(
include = ["**/*.ts"],
),
jest_config = "@lbm//:jest.config.js",
deps = [
"//packages/enums/src:lib",
"//packages/hello/src:lib",
"@npm//faker",
"@npm//@types/faker",
"@npm//express",
"@npm//@types/express",
"@npm//jest",
"@npm//ts-jest",
"@npm//@types/jest",
],
)
ts_library(
name = "lib",
srcs = glob(
include = ["**/*.ts"],
exclude = ["**/*.spec.ts"]
),
deps = [
"//packages/enums/src:lib",
"//packages/hello/src:lib",
"@npm//faker",
"@npm//@types/faker",
"@npm//express",
"@npm//@types/express",
],
)
nodejs_image(
name = "server",
data = [":lib"],
entry_point = ":index.ts",
)
load("@io_bazel_rules_docker//container:container.bzl", "container_push")
container_push(
name = "push_server",
image = ":server",
format = "Docker",
registry = "gcr.io",
repository = "learning-bazel-monorepo/server",
tag = "dev",
)
While the server
builds successfully, the test
fails to run.
Running
bazel test //services/server/src:test
results in the following output:
INFO: Analyzed target //services/server/src:test (0 packages loaded, 0 targets configured).
INFO: Found 1 test target...
FAIL: //services/server/src:test (see /home/flolu/.cache/bazel/_bazel_flolu/698f7adad10ea020bcdb85216703ce08/execroot/lbm/bazel-out/k8-fastbuild/testlogs
/services/server/src/test/test.log)
Target //services/server/src:test up-to-date:
bazel-bin/services/server/src/test_loader.js
bazel-bin/services/server/src/test.sh
INFO: Elapsed time: 0.947s, Critical Path: 0.72s
INFO: 2 processes: 2 linux-sandbox.
INFO: Build completed, 1 test FAILED, 2 total actions
//services/server/src:test FAILED in 0.1s
/home/flolu/.cache/bazel/_bazel_flolu/698f7adad10ea020bcdb85216703ce08/execroot/lbm/bazel-out/k8-fastbuild/testlogs/services/server/src/test/test.log
INFO: Build completed, 1 test FAILED, 2 total actions
The test.log
file indicates the following issue:
exec ${PAGER:-/usr/bin/less} "$0" || exit 1
Executing tests from //services/server/src:test
-----------------------------------------------------------------------------
● Validation Error:
Module ts-jest in the transform option was not found.
<rootDir> is: /home/flolu/.cache/bazel/_bazel_flolu/698f7adad10ea020bcdb85216703ce08/sandbox/linux-sandbox/3/execroot/lbm/bazel-out/k8-fastbuild/bin/services/server/src/test.sh.runfiles/lbm/external/lbm
Configuration Documentation:
https://jestjs.io/docs/configuration.html
It appears that there is an issue with ts-jest
. Running jest
manually does not produce any errors.
My [jest.config.js][2]
at the root of the project has the following configuration:
module.exports = {
roots: ['<rootDir>/services/server/src', '<rootDir>/packages/hello/src'],
testMatch: ['**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
};
Feel free to try it out by cloning this repository: https://github.com/flolude/minimal-bazel-monorepo
Update 1
While attempting to implement the solution provided by @Charlie OConor, I encountered the following error:
services/server/src/util.spec.ts:1:21 - error TS2307: Cannot find module './util'.
1 import { add } from './util';
~~~~~~~~
In order to resolve this, I added the util.ts
file to the srcs
like this:
srcs = glob(
include = ["**/*.ts"],
),
However, this led to the following error:
ERROR: /home/flolu/Desktop/minimal-bazel-monorepo/services/server/src/BUILD:33:1: in args attribute of nodejs_test rule //services/server/src:test: label '//services/server/src:test_lib.js' in $(location) expression expands to more than one file, please use $(locations //services/server/src:test_lib.js) instead. Files (at most 5 shown) are: [services/server/src/index.js, services/server/src/util.js, services/server/src/util.spec.js]. Since this rule was created by the macro 'jest_test', the error might have been caused by the macro implementation
ERROR: Analysis of target '//services/server/src:test' failed; build aborted: Analysis of target '//services/server/src:test' failed; build aborted
INFO: Elapsed time: 4.487s
INFO: 0 processes.