rules_js icon indicating copy to clipboard operation
rules_js copied to clipboard

[FR]: Native node:test support in js_test

Open YvanGuidoin opened this issue 1 year ago • 3 comments

What is the current behavior?

js_test requires an entrypoint to run, you can run NodeJS native tests (https://nodejs.org/api/test.html) by setting the entrypoint as your test file, but you can't have multiple files, or you need one js_test per test file

js_test(
    name = "lib_js_testrun",
    size = "small",
    chdir = package_name(),
    data = [":lib_js_test"],
    entry_point = "testfile.test.mjs",
    node_options = [
        "--test",
        "--test-reporter",
        "spec",
    ],
)

Another workaround is to use a main test file importing other files:

import "./first.test.mjs";
import "./second.test.mjs";

and putting this as the entry_point, but it doesn't scale well for bigger projects

Describe the feature

Allowing a different flag to run node tests on the current folder would be enough for simple use cases, but maybe a separate macro js_node_test would make more sense to simplify attribute types would make more sense in the long term

YvanGuidoin avatar Oct 10 '23 17:10 YvanGuidoin

You could generate that one entry file since you know all the paths to import.

Or can this --test API still accept a separate (maybe empty) entry point? node empty.js --test t1.js t2.js? I've never used the API and haven't explored it yet so I'm not sure.

jbedard avatar Oct 10 '23 19:10 jbedard

Probably could generate that file indeed, and as I said it's already working for me now. It is more of a "nice-to-have" feature, as node is integrating tests natively now, we could have a rule that play nice with it like for Jest and Jasmine, not requiring any dependency for simple tests.

You can either node --test in which case it looks recursively for test files in your current folder and subfolders, or a list of files to tests (probably more appropriate for Bazel determinism), but the entrypoint of js_test accept only 1 file right now

YvanGuidoin avatar Oct 11 '23 11:10 YvanGuidoin

We have another case where we generate a test entry point that points to all the test files, over here in python https://github.com/aspect-build/rules_py/blob/main/docs/rules.md#py_pytest_main so yeah, that's one possible answer, a node_test_main rule or something to generate the entry point.

It's also possible to use list comprehensions in BUILD along with glob so you could just do something like

[
  js_test(
    name = "test_" + i,
    entry_point = s
 ...
] for i, s in enumerate(glob(["*.spec.cjs"]))

alexeagle avatar Nov 01 '23 02:11 alexeagle