Anatole Lucet
Anatole Lucet

Reputation: 1813

Testing with Jest in Typescript using a Bazel custom rule

I'm trying to test my Typescript code using Jest with Bazel.

There is an example on the rules_nodejs's repo but it's simply using raw Javascript files: https://github.com/bazelbuild/rules_nodejs/tree/master/examples/jest

My goal is to compile my Typescript code with the ts_library rule (from rules_nodejs) and then pass it to the Jest rule (I'm using the same rule for Jest as in the example on rules_nodejs's repo).

Here's the Jest rule:

# //: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
    )

My build file in 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"
    ],
)

I've also taken the Jest config from the example:

// //:jest.config.js

module.exports = {
  testEnvironment: "node",

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

And my file to test:

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

test("It can test", () => {
  expect(2).toEqual(2);
});

After all that, when I run bazel run //src:test I get:

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"

According to the last line of the error, it seems like the Jest rule only get the .d.ts files (and I have no idea why).

Upvotes: 6

Views: 4396

Answers (1)

ant_Ti
ant_Ti

Reputation: 2425

It looks like you need to retrieve js files from ts_library. This can be achieved with filegroup

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

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

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

More information about this can be found in Accessing JavaScript outputs section of rules_typescript documentation.

Upvotes: 6

Related Questions