Repository navigation
console.log() does not work in the test runner #43034
Description
Activity
Run the code above and notice that string
abcis 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,
abcgets printed in stdout, above theExperimentalWarning.- addedtest_runnerIssues and PRs related to the test runner subsystem.Issues and PRs related to the test runner subsystem.
on May 10, 2022 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.1434921So you are using the
--testCLI flag, which makes Node.js spawn subprocess for each files, so theconsole.logwill output to the stdout of the subprocess, not the top-level one. If you remove the--testflag, 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.Reacted by Jim Jin and StevenOK. That's a good workaround. But I insist that
--testshould supportconsole.log().Reacted by SeungBin Kim and 夜雨_归0--testis meant to be used to run your own test suite (it runs all files that havetestin 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--testnot 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--testwould be helpful, that can be looked into.Reacted by Chris Calo, OuttaPencil, Alexey Elizarov, javier-rlcore and mob-aaronCan't agree more. It would be better to have
--verbosealong with--testwhich would enableconsole.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.Reacted by javier-rlcoreReacted by Chris CaloIf 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.I'm not sure whether I could do this. But I'll try it.
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.
Run the code above and notice that string
abcis 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.