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

Closed
@ForbiddenEra

Description

@ForbiddenEra

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.test_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions