使用Bazel自定义规则在Typescript中使用Jest进行测试

使用Bazel自定义规则在Typescript中使用Jest进行测试,typescript,jestjs,bazel,Typescript,Jestjs,Bazel,我试图用Jest和Bazel测试我的打字脚本代码 在rules\u nodejs的repo中有一个例子,但它只是使用原始Javascript文件: 我的目标是使用ts\u库规则(来自rules\u nodejs)编译我的Typescript代码,然后将其传递给Jest规则(我对Jest使用的规则与rules\u nodejs的repo示例中相同) 以下是笑话规则: #/:rules/jest.bzl 加载(@npm//jest cli:index.bzl),\u jest\u test=“jes

我试图用Jest和Bazel测试我的打字脚本代码

rules\u nodejs
的repo中有一个例子,但它只是使用原始Javascript文件:

我的目标是使用
ts\u库
规则(来自
rules\u nodejs
)编译我的Typescript代码,然后将其传递给Jest规则(我对Jest使用的规则与
rules\u nodejs
的repo示例中相同)

以下是笑话规则:

#/:rules/jest.bzl
加载(@npm//jest cli:index.bzl),\u jest\u test=“jest\u test”)
def jest_测试(名称、srcs、deps、jest_配置、**kwargs):
“围绕自动生成的jest_测试规则的宏”
args=[
“--无缓存”,
“--没有看守人”,
“--ci”,
]
args.extend([“--config”,“$(位置%s)”%jest\u config])
对于src中的src:
args.extend([“--runTestsByPath”,“$(位置%s)”%src])
_玩笑测验(
name=name,
数据=[jest_配置]+srcs+deps,
args=args,
**夸尔斯
)
我在
src
中的生成文件:

#/:src/BUILD.bazel
加载(“@npm_bazel_typescript/:index.bzl”,“ts_库”)
加载(“//:rules/jest.bzl”,“jest_测试”)
清华大学图书馆(
name=“src”,
srcs=glob([“*.ts”]),
deps=[
“@npm//:节点\u模块”
]
)
玩笑测验(
name=“test”,
srcs=[“:src”],
jest_config=“/:jest.config.js”,
标签=[
#需要设置pwd以避免jest需要runfiles助手
#具有权限的Windows用户可以使用--enable\u运行文件
#为了让这个测试有效
“没有bazelci窗口”,
],
deps=[
“@npm//:节点\u模块”
],
)
我还采用了示例中的Jest配置:

//:jest.config.js
module.exports={
测试环境:“节点”,
转换:{“^.+\.jsx?$”:“babel jest”},
testMatch:[“***.test.js”]
};
和我要测试的文件:

//:src/foo.test.ts
测试(“它可以测试”,()=>{
期望(2)、toEqual(2);
});
在所有这些之后,当我运行
bazel run//src:test
时,我得到:

Executing tests from //src:test
-----------------------------------------------------------------------------
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
No files found in /home/anatole/.cache/bazel/_bazel_anatole/8d84caec5a0442239ce878a70e921a6b/execroot/examples_app/bazel-out/k8-fastbuild/bin/src/test.sh.runfiles/examples_app.
Make sure Jest's configuration does not exclude this directory.
To set up Jest, make sure a package.json file exists.
Jest Documentation: facebook.github.io/jest/docs/configuration.html
Files: "src/foo.test.d.ts"

根据错误的最后一行,Jest规则似乎只获取
.d.ts
文件(我不知道为什么)。

看起来您需要从
ts\u库
检索js文件。这可以通过
filegroup

ts_library(
    name = "src",
    ...
)

filegroup(
    name = "js_src",
    srcs = [":src"],
    output_group = "es5_sources",
)

jest_test(
    name = "test",
    srcs = [":js_src"],
    ...
)


有关这方面的更多信息,请参见
规则\u typescript
文档的一节。

它可以工作!我不知道这个
输出组
。不管怎样,谢谢你。