Skip to content
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

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

Closed
jim-king-2000 opened this issue May 10, 2022 · 8 comments
Closed

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

jim-king-2000 opened this issue May 10, 2022 · 8 comments
Labels
test_runner Issues and PRs related to the test runner subsystem.

Comments

@jim-king-2000
Copy link

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.

@aduh95
Copy link
Contributor

aduh95 commented May 10, 2022

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.

@aduh95 aduh95 added the test_runner Issues and PRs related to the test runner subsystem. label May 10, 2022
@jim-king-2000
Copy link
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

@aduh95
Copy link
Contributor

aduh95 commented May 10, 2022

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.

@jim-king-2000
Copy link
Author

jim-king-2000 commented May 10, 2022

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

@aduh95
Copy link
Contributor

aduh95 commented May 10, 2022

--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.

@jim-king-2000
Copy link
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.

@aduh95
Copy link
Contributor

aduh95 commented May 10, 2022

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.

@jim-king-2000
Copy link
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
Labels
test_runner Issues and PRs related to the test runner subsystem.
Projects
None yet
Development

No branches or pull requests

2 participants