我有一个正在工作的 Bazel BUILD看起来像这样的文件:
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",
)
build
server
工作正常。但是运行 test
失败。当我跑
bazel test //services/server/src:test
,我得到以下输出: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
和
test.log
文件必须包含以下内容: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
所以看起来好像是
ts-jest
不管用。运行时 jest
手动,我没有收到任何错误。我的
[jest.config.js][2]
在项目的根目录下是这样的: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',
},
};
你可以通过克隆这个 repo 自己尝试一下:https://github.com/flolude/minimal-bazel-monorepo
更新 1
我试图实现来自 @Charlie OConor 的原始答案,但后来我收到此错误:
services/server/src/util.spec.ts:1:21 - error TS2307: Cannot find module './util'.
1 import { add } from './util';
~~~~~~~~
这就是为什么我添加了
util.ts
文件到 srcs
像这样:srcs = glob(
include = ["**/*.ts"],
),
但是后来我收到了这个错误:
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.
最佳答案
编辑实际问题
我又看了一眼。留下下面的答案,因为我认为它里面有有用的东西。您的版本 build_bazel_rules_nodejs
比较老了。更新到最新版本 1.01
.您使用的是版本 0.42.2
我认为它有一些处理 npm 依赖项的奇怪方式。
http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "e1a0d6eb40ec89f61a13a028e7113aa3630247253bcb1406281b627e44395145",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/1.0.1/rules_nodejs-1.0.1.tar.gz"],
)
同时更新
"@bazel/typescript": "^1.0.1",
在您的 package.json 中,因为它们需要同步。您在 repo 中使用的其他库之间还有一些其他重大更改。我通过注释掉所有 docker 和其他不相关的东西来让它工作。更新这些应该不会太难。原答案
可能不是您正在寻找的答案,但您可能不需要
ts-jest
.它的工作是让您的生活更轻松,而无需在运行测试之前进行单独的编译步骤。如果您的运行脚本形成 package.json
,那就太好了但是你有 bazel,它是关于定义依赖关系图的。这使得创建 ts_library
变得容易用于您的测试然后依赖于它,以及 jest_test
中的 javascript 输出添加另一个
tsconfig.test.json
和{
"extends": "./tsconfig.json",
"lib": ["jest"]
}
这将使它正确地使用所有玩笑的东西进行类型检查,
describe()
, it()
, 等等。写到
tsconfigs.json
使用 ts_config
所以把它放在 /BUILD
load("@npm_bazel_typescript//:index.bzl", "ts_config")
ts_config(
name = "tsconfig.jest.json",
src = "tsconfig.test.json",
deps = [
":tsconfig.json",
],
)
然后创建
ts_library
用于您的测试并定义要在 jest_test
中使用的 javascript 输出load("@npm_bazel_typescript//:index.bzl", "ts_library")
ts_library(
name = "test_lib",
srcs = ["util.spec.ts"],
# References the test tsconfig
tsconfig = "//:tsconfig.jest.json",
deps = [
"//packages/enums/src:lib",
"//packages/hello/src:lib",
"@npm//faker",
"@npm//@types/faker",
"@npm//express",
"@npm//@types/express",
"@npm//cors",
"@npm//@types/jest",
],
)
filegroup(
name = "test_lib.js",
srcs = [":test_lib"],
output_group = "es5_sources",
)
load("@lbm//:jest.bzl", "jest_test")
jest_test(
name = "test",
srcs = [
":test_lib.js",
],
jest_config = "@lbm//:jest.config.js",
deps = [
# UPDATE HERE
":lib",
# END UPDATE
"//packages/enums/src:lib",
"//packages/hello/src:lib",
"@npm//faker",
"@npm//express",
"@npm//jest",
],
)
这些规则有点老套,所以你可以把它们包装成 gen 规则。
我确实提取了您的代码,但不知道为什么您无法获取
ts-node
.您甚至可以在 jestconfig.js
中导入它.不是你想要,但它表明依赖是存在的。我的假设是 jest
附近存在潜在问题或 ts-jest
,但我找不到任何东西。
关于typescript - Bazel 抛出 "Module ts-jest in the transform option was not found"的 Jest 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59433880/