test_runner - method to check if being run as a test and/or ability to pipe TestsStream to an internal test reporter #47314
Labels
feature request
Issues that request new features to be added to Node.js.
test_runner
Issues and PRs related to the test runner subsystem.
What is the problem this feature will solve?
I have a
run_tests.js
file that imports tests from./test/*-tests.js
and runs them.Those tests files presently look something like this:
This is a convenient way to run the tests. I know I could use the
run
function and provide the files, however in #47312 I mention the example in the docs for piping the output tostdout
doesn't work. I also mention that I'm aware that I can watch for the events fromTestsStream
or use aTransformStream
, however this doesn't give me the 'pretty' ANSI-colored output (spec) reporter I get when running it as above or running the tests withnode --test
.However, if the test files
export
the test function then nothing is actually called within the test file and thusrun
ornode --test
won't work. If I could somehow test whether the files been called byrun
ornode --test
then I can simply call the exported function in the file in those cases and have it work for all methods. I suppose I could also export a null/noop function and have the tests run on import, but that feels a bit yucky (import side effects anyone?)Aside from my case, I can imagine other potential uses for knowing whether a file is being run as a test or not, such as enabling/disabling inline tests or providing them with mocked data.
Forgive me if there's a roundabout way of doing this, I suppose one could check
process.argv
for thenode --test
method but I'm not sure about therun
method.I should also mention that running
node --test
only gives me the top-level output of tests and I can't find any documentation on how to change this, it also doesn't print any output from the tests and only shows each test file as an individual test regardless of the test reporter.When using the
run
call or using myrun-tests.js
, this is not the case, it will print output from each test and show the sub-tests for each file as well (either printing withrun-tests.js
method or firing events on theTestsStream
for therun
method).This is one (main) reason why I'm using
run-tests.js
along with not knowing if it's possible/how to pipe theTestsStream
fromrun
to any of the built-in test reporters likespec
ortap
(and then ontostdout
or otherwise) as the only example provided is piping tostdout
, which perhaps previously maybe automatically piped through a test reporter?Using my
run-tests.js
, I can also still use--test-reporter [spec|tap|dot]
as well to get the output I desire.What is the feature you are proposing to solve the problem?
node:test.run
ornode --test
TestsStream
returned fromrun
or otherwise to a built in test reporter.What alternatives have you considered?
For finding out if run as a test, workarounds like checking
process.argv
for the--test
parameter, however this doesn't work in all cases.I suppose I could also export a null/noop function and have the tests run on import, but that feels a bit yucky (import side effects anyone?)
For being able to pipe
TestsStream
to one of the internal test reporters / getting the output I desire, using arun-tests.js
file to import files for testing and calling their default exported function to initiate the tests.The text was updated successfully, but these errors were encountered: