使用Bazel进行Jest测试抛出“在transform选项中未找到模块ts-jest”

时间:2019-12-21 06:06:26

标签: typescript unit-testing jestjs bazel monorepo

我有一个有效的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} "$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',
  },
};

您可以通过克隆以下存储库来自己尝试:https://github.com/flolude/minimal-bazel-monorepo

1 个答案:

答案 0 :(得分:2)

编辑实际问题

我再次看了看。留下波纹管的答案,因为我认为它包含有用的内容。您的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,但您却有赖于定义依赖关系图,这真是太好了。这样一来,您可以轻松为测试创建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中。不是您想要的,而是表明存在依赖性。我的假设是jestts-jest周围存在一个潜在的问题,但我什么也找不到。