Skip to content

console.log() does not work in the test runner #43034

Description

@jim-king-2000

Version

v18.1.0

Platform

Win11

Subsystem

No response

What steps will reproduce the bug?

Use test runner in node v18 to run the test.

test('synchronous passing test', (t) => {
  console.log('abc');
  assert.strictEqual(1, 1);
});

Run the code above and notice that string abc is not printed in the console.

How often does it reproduce? Is there a required condition?

everytime

What is the expected behavior?

We could use console.log() in test runner.

What do you see instead?

console.log() does not work in test runner.

Additional information

We need output some debug information during the test. We were like a blind when nothing could be printed to the console.

Activity

  1. aduh95 commented on May 10, 2022

    @aduh95
    Contributor

    Run the code above and notice that string abc is not printed in the console.

    When I try to run that code, I get ReferenceError: test is not defined. It's always more helpful if you send a full repro with its output, otherwise folks trying to help you are have to play the guessing game of what you actually meant.
    Here's the output for me:

    $ node <<EOF
    const assert=require('node:assert');
    const test=require('node:test');
    test('synchronous passing test', (t) => {
      console.log('abc');      
      assert.strictEqual(1, 1);
    });
    EOF
    abc
    (node:80807) ExperimentalWarning: The test runner is an experimental feature. This feature could change at any time
    (Use `node --trace-warnings ...` to show where the warning was created)
    TAP version 13
    ok 1 - synchronous passing test
      ---
      duration_ms: 0.005491586
      ...
    1..1
    # tests 1
    # pass 1
    # fail 0
    # skipped 0
    # todo 0
    # duration_ms 0.048477983

    As you can see, abc gets printed in stdout, above the ExperimentalWarning.

  2. added
    test_runnerIssues and PRs related to the test runner subsystem.
    on May 10, 2022
  3. jim-king-2000 commented on May 10, 2022

    @jim-king-2000
    Author

    Weird. No output on my machine.

    import test from 'node:test';
    import assert from 'node:assert';
    
    test('synchronous passing test', (t) => {
      console.log('abc');
      assert.strictEqual(1, 1);
    });
    PS D:\WorkHouse\cache-hit\wx_server> node --test test/test.mjs
    TAP version 13
    ok 1 - D:\\WorkHouse\\cache-hit\\wx_server\\test\\test.mjs
      ---
      duration_ms: 0.0902871
      ...
    1..1
    # tests 1
    # pass 1
    # fail 0
    # skipped 0
    # todo 0
    # duration_ms 0.1434921
    
  4. aduh95 commented on May 10, 2022

    @aduh95
    Contributor

    So you are using the --test CLI flag, which makes Node.js spawn subprocess for each files, so the console.log will output to the stdout of the subprocess, not the top-level one. If you remove the --test flag, you should see the same output as me. Maybe we can improve documentation around the CLI flag, but other than that I think that's the expected behavior.

  5. jim-king-2000 commented on May 10, 2022

    @jim-king-2000
    Author

    OK. That's a good workaround. But I insist that --test should support console.log().

  6. aduh95 commented on May 10, 2022

    @aduh95
    Contributor

    --test is meant to be used to run your own test suite (it runs all files that have test in there name, more or less, see https://nodejs.org/api/test.html#test-runner-execution-model for the exact pattern), not for debugging. If one of that file failing, you can then run it individually to get the debug info you need. IMO it's reasonable to have --test not outputting all the debug info to stdout as it can get very noisy otherwise. That being said, the test runner is still very young, so if you think that adding an option to get the full output when running --test would be helpful, that can be looked into.

  7. jim-king-2000 commented on May 10, 2022

    @jim-king-2000
    Author

    Can't agree more. It would be better to have --verbose along with --test which would enable console.log(). By the way, test runner introduced in node v18 is so great that we don't have to import any other test framework. The next killer feature we think of node is the compiler which could turn node project (especially *.mjs) to an executable.

  8. aduh95 commented on May 10, 2022

    @aduh95
    Contributor

    If you are willing to contribute for that, that'd be awesome (opening a feature request issue would be ❤️ , opening a PR would be ❤️‍🔥 ).
    Regarding creating single file apps, that's already on the next-10 initiative list, see https://github.com/nodejs/node/blob/master/doc/contributing/technical-priorities.md#single-executable-applications.

  9. jim-king-2000 commented on May 10, 2022

    @jim-king-2000
    Author

    I'm not sure whether I could do this. But I'll try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    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