flo*_*olu 5 unit-testing typescript jestjs bazel monorepo
我有一个可用的 Bazel BUILD文件,如下所示:
\n\npackage(default_visibility = ["//visibility:public"])\n\nload("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")\nload("@npm_bazel_typescript//:index.bzl", "ts_library")\n\n# TODO run jest tests and stop build if test not passes\n# TODO also run tests from dependent packages\nload("@lbm//:jest.bzl", "jest_test")\njest_test(\n name = "test",\n srcs = glob(\n include = ["**/*.ts"],\n ),\n jest_config = "@lbm//:jest.config.js",\n deps = [\n "//packages/enums/src:lib",\n "//packages/hello/src:lib",\n "@npm//faker",\n "@npm//@types/faker",\n "@npm//express",\n "@npm//@types/express",\n "@npm//jest",\n "@npm//ts-jest",\n "@npm//@types/jest",\n ],\n)\n\nts_library(\n name = "lib",\n srcs = glob(\n include = ["**/*.ts"],\n exclude = ["**/*.spec.ts"]\n ),\n deps = [\n "//packages/enums/src:lib",\n "//packages/hello/src:lib",\n "@npm//faker",\n "@npm//@types/faker",\n "@npm//express",\n "@npm//@types/express",\n ],\n)\n\nnodejs_image(\n name = "server",\n data = [":lib"],\n entry_point = ":index.ts",\n)\n\nload("@io_bazel_rules_docker//container:container.bzl", "container_push")\n\ncontainer_push(\n name = "push_server",\n image = ":server",\n format = "Docker",\n registry = "gcr.io",\n repository = "learning-bazel-monorepo/server",\n tag = "dev",\n)\nRun Code Online (Sandbox Code Playgroud)\n\n建设工程server良好。但运行test失败。
当我运行时bazel test //services/server/src:test,我得到以下输出:
INFO: Analyzed target //services/server/src:test (0 packages loaded, 0 targets configured).\nINFO: Found 1 test target...\nFAIL: //services/server/src:test (see /home/flolu/.cache/bazel/_bazel_flolu/698f7adad10ea020bcdb85216703ce08/execroot/lbm/bazel-out/k8-fastbuild/testlogs\n/services/server/src/test/test.log)\nTarget //services/server/src:test up-to-date:\n bazel-bin/services/server/src/test_loader.js\n bazel-bin/services/server/src/test.sh\nINFO: Elapsed time: 0.947s, Critical Path: 0.72s\nINFO: 2 processes: 2 linux-sandbox.\nINFO: Build completed, 1 test FAILED, 2 total actions\n//services/server/src:test FAILED in 0.1s\n /home/flolu/.cache/bazel/_bazel_flolu/698f7adad10ea020bcdb85216703ce08/execroot/lbm/bazel-out/k8-fastbuild/testlogs/services/server/src/test/test.log\n\nINFO: Build completed, 1 test FAILED, 2 total actions\nRun Code Online (Sandbox Code Playgroud)\n\n该test.log文件具有以下内容:
exec ${PAGER:-/usr/bin/less} "$0" || exit 1\nExecuting tests from //services/server/src:test\n-----------------------------------------------------------------------------\n\xe2\x97\x8f Validation Error:\n\n Module ts-jest in the transform option was not found.\n <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\n\n Configuration Documentation:\n https://jestjs.io/docs/configuration.html\nRun Code Online (Sandbox Code Playgroud)\n\n所以看起来好像有什么东西ts-jest不起作用。手动运行时jest,我没有收到任何错误。
我的[jest.config.js][2]项目根目录如下所示:
module.exports = {\n roots: [\'<rootDir>/services/server/src\', \'<rootDir>/packages/hello/src\'],\n testMatch: [\'**/__tests__/**/*.+(ts|tsx|js)\', \'**/?(*.)+(spec|test).+(ts|tsx|js)\'],\n transform: {\n \'^.+\\\\.(ts|tsx)$\': \'ts-jest\',\n },\n};\nRun Code Online (Sandbox Code Playgroud)\n\n您可以通过克隆此存储库来亲自尝试:https ://github.com/flolude/minimal-bazel-monorepo
\n\n我尝试实现@Charlie OConor的原始答案,但随后出现此错误:
\n\nservices/server/src/util.spec.ts:1:21 - error TS2307: Cannot find module \'./util\'.\n\n1 import { add } from \'./util\';\n ~~~~~~~~\nRun Code Online (Sandbox Code Playgroud)\n\n这就是为什么我将util.ts文件添加到srcs这样的位置:
srcs = glob(\n include = ["**/*.ts"],\n),\nRun Code Online (Sandbox Code Playgroud)\n\n但后来我得到这个错误:
\n\nERROR: /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\nERROR: Analysis of target \'//services/server/src:test\' failed; build aborted: Analysis of target \'//services/server/src:test\' failed; build aborted\nINFO: Elapsed time: 4.487s\nINFO: 0 processes.\nRun Code Online (Sandbox Code Playgroud)\n
我又看了一眼。留下下面的答案,因为我认为它里面有有用的东西。你的版本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"],
)
Run Code Online (Sandbox Code Playgroud)
还要更新"@bazel/typescript": "^1.0.1",package.json 中的 ,因为它们需要同步。您在存储库中使用的其他库之间还有一些其他重大更改。我通过注释掉所有 docker 和其他不相关的东西来让它工作。更新这些应该不会太难。
可能不是您正在寻找的答案,但您可能不需要ts-jest。它的工作是让您的生活更轻松,无需在运行测试之前需要协调单独的编译步骤。如果您正在运行的脚本形成 a ,那就太好了,package.json但是您有 bazel ,它的全部内容都是关于定义依赖关系图的。这使得您可以轻松地为您的测试创建一个ts_library然后依赖它,以及 javascript 输出jest_test
tsconfig.test.json添加另一个
{
"extends": "./tsconfig.json",
"lib": ["jest"]
}
Run Code Online (Sandbox Code Playgroud)
这将使其对所有笑话的内容进行正确的类型检查,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",
],
)
Run Code Online (Sandbox Code Playgroud)
然后为您的测试创建ts_library并定义要使用的 javascript 输出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",
],
)
Run Code Online (Sandbox Code Playgroud)
这些规则有点重复,所以你可以将它们包装到 gen 规则中。
我确实提取了您的代码,但不知道为什么您无法获取ts-node. 您甚至可以将其导入到jestconfig.js. 不是你想要的,但它表明依赖关系是存在的。jest我的假设是 around or存在潜在问题ts-jest,但我找不到任何东西。
| 归档时间: |
|
| 查看次数: |
10265 次 |
| 最近记录: |