Skip to content

test_runner - method to check if being run as a test and/or ability to pipe TestsStream to an internal test reporter #47314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ForbiddenEra opened this issue Mar 30, 2023 · 2 comments
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.

Comments

@ForbiddenEra
Copy link

ForbiddenEra commented Mar 30, 2023

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:

export default () => {
  test('my test suite', async(t) => {
    console.log('begin my test');

    await t.test('test one', async() => {
      assert.equal(1,1);
    });
  });
};

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 to stdout doesn't work. I also mention that I'm aware that I can watch for the events from TestsStream or use a TransformStream, however this doesn't give me the 'pretty' ANSI-colored output (spec) reporter I get when running it as above or running the tests with node --test.

However, if the test files export the test function then nothing is actually called within the test file and thus run or node --test won't work. If I could somehow test whether the files been called by run or node --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 the node --test method but I'm not sure about the run 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 my run-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 with run-tests.js method or firing events on the TestsStream for the run method).

This is one (main) reason why I'm using run-tests.js along with not knowing if it's possible/how to pipe the TestsStream from run to any of the built-in test reporters like spec or tap (and then onto stdout or otherwise) as the only example provided is piping to stdout, 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?

  • Ability to know whether a file is being run as a test or not via node:test.run or node --test
  • Also, the ability to, or documentation on piping the TestsStream returned from run 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 a run-tests.js file to import files for testing and calling their default exported function to initiate the tests.

@ForbiddenEra ForbiddenEra added the feature request Issues that request new features to be added to Node.js. label Mar 30, 2023
@ForbiddenEra ForbiddenEra changed the title test_runner - method to check if being run as a test test_runner - method to check if being run as a test and/or ability to pipe TestsStream to an internal test reporter Mar 30, 2023
@MoLow MoLow added the test_runner Issues and PRs related to the test runner subsystem. label Mar 30, 2023
@cjihrig
Copy link
Contributor

cjihrig commented Mar 30, 2023

Once 334bb17 is released, the NODE_TEST_CONTEXT environment variable should be present and set to child if the tests are run via --test or run().

@ForbiddenEra
Copy link
Author

I also noticed that 29d7aec should expose the reporters, which satisfies my aside.

Once both those commits are landed, I'd consider my issue resolved. As commits have already been made toward that end, I'll close this issue. Feel free to re-open if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
Projects
None yet
Development

No branches or pull requests

3 participants