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

标签 typescript jestjs bazel

我正在尝试使用 Jest 和 Bazel 来测试我的 Typescript 代码。
rules_nodejs上有一个例子的 repo 但它只是使用原始 Javascript 文件:https://github.com/bazelbuild/rules_nodejs/tree/master/examples/jest

我的目标是使用 ts_library 编译我的 Typescript 代码规则(来自 rules_nodejs ),然后将其传递给 Jest 规则(我对 Jest 使用与 rules_nodejs 的存储库中的示例相同的规则)。

这是 Jest 规则:

# //:rules/jest.bzl

load("@npm//jest-cli:index.bzl", _jest_test = "jest_test")

def jest_test(name, srcs, deps, jest_config, **kwargs):
    "A macro around the autogenerated jest_test rule"
    args = [
        "--no-cache",
        "--no-watchman",
        "--ci",
    ]
    args.extend(["--config", "$(location %s)" % jest_config])

    for src in srcs:
        args.extend(["--runTestsByPath", "$(locations %s)" % src])

    _jest_test(
        name = name,
        data = [jest_config] + srcs + deps,
        args = args,
        **kwargs
    )

我的构建文件在 src :

# //:src/BUILD.bazel

load("@npm_bazel_typescript//:index.bzl", "ts_library")
load("//:rules/jest.bzl", "jest_test")

ts_library(
    name = "src",
    srcs = glob(["*.ts"]),
    deps = [
        "@npm//:node_modules"
    ]
)

jest_test(
    name = "test",
    srcs = [":src"],
    jest_config = "//:jest.config.js",
    tags = [
        # Need to set the pwd to avoid jest needing a runfiles helper
        # Windows users with permissions can use --enable_runfiles
        # to make this test work
        "no-bazelci-windows",
    ],
    deps = [
        "@npm//:node_modules"
    ],
)

我还从示例中获取了 Jest 配置:

// //:jest.config.js

module.exports = {
  testEnvironment: "node",

  transform: { "^.+\\.jsx?$": "babel-jest" },
  testMatch: ["**/*.test.js"]
};

还有我要测试的文件:

// //:src/foo.test.ts

test("It can test", () => {
  expect(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_library 中检索 js 文件.这可以通过 filegroup 实现

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

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

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


更多信息可以在 Accessing JavaScript outputs 中找到。 rules_typescript的部分文档。

关于typescript - 使用 Bazel 自定义规则在 Typescript 中使用 Jest 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59059699/

相关文章:

c++ - 如何解决 bazel "undeclared inclusion(s)"错误?

typescript - 如何在此方法中返回 Promise

node.js - @types/node 和 VS 代码 : IntelliSense not working because of comment formatting

reactjs - Jest - 如何使用 id 查找渲染的 DOM 组件?

jestjs - Jest + enzyme 测试 : how to ensure a certain function is passed as prop to a component

c++ - Bazel 允许我包含来自全局安装的库的 header

docker - 如何加速 Google Cloud Platform 上的 bazel 构建

javascript - 'this' 在子类中未定义

javascript - 在 react-table 的第 2+ 页上传递给子组件的 Prop 不正确

javascript - 从 CLI 生成的新 vue 3 项目的单元测试中出错