使用 Bazel 抛出的 Jest 测试 "Module ts-jest in the transform option was not found"
Jest Testing with Bazel Throws "Module ts-jest in the transform option was not found"
我有一个工作 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",
)
构建 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} "[=13=]" || 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
的东西不工作。手动 运行ning 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',
},
};
您可以通过克隆此存储库来自己尝试:https://github.com/flolude/minimal-bazel-monorepo
更新 1
我试图实现 的原始答案,但后来出现此错误:
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"],
)
同时更新 package.json 中的 "@bazel/typescript": "^1.0.1",
,因为它们需要同步。您在回购中使用的其他库之间还有一些其他重大变化。我通过注释掉所有 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",
],
)
这些规则有点乏味,因此您可以将它们包装成通用规则。
我确实提取了您的代码,但不知道为什么您无法获取 ts-node
。您甚至可以在 jestconfig.js
中导入它。不是你想要,但它表明存在依赖关系。我的假设是 jest
或 ts-jest
附近存在潜在问题,但我找不到任何问题。
我有一个工作 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",
)
构建 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} "[=13=]" || 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
的东西不工作。手动 运行ning 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',
},
};
您可以通过克隆此存储库来自己尝试:https://github.com/flolude/minimal-bazel-monorepo
更新 1
我试图实现
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"],
)
同时更新 package.json 中的 "@bazel/typescript": "^1.0.1",
,因为它们需要同步。您在回购中使用的其他库之间还有一些其他重大变化。我通过注释掉所有 docker 和其他不相关的东西来让它工作。更新这些应该不会太难。
原答案
可能不是您正在寻找的答案,但您可能不需要 ts-jest
。它的工作是让您的生活更轻松,而无需在 运行 测试之前需要协调单独的编译步骤。如果您的 运行 脚本形成 package.json
很好,但是您有 bazel,它是关于定义依赖关系图的。这使得为您的测试创建 ts_library
然后依赖于它变得容易,以及 jest_test
添加另一个 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
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",
],
)
这些规则有点乏味,因此您可以将它们包装成通用规则。
我确实提取了您的代码,但不知道为什么您无法获取 ts-node
。您甚至可以在 jestconfig.js
中导入它。不是你想要,但它表明存在依赖关系。我的假设是 jest
或 ts-jest
附近存在潜在问题,但我找不到任何问题。