-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
stdout not being flushed before Jest's test result summary is written #4977
Comments
I'd love to see an option for displaying the current contents of the console buffer before the tests have finished. Use-case: I find Jest is super convenient for writing quick TDD blocks for HTTP servers and other long-running servers, that I want to augment with manual testing. Using a combo of Granted, this is probably an edge case (and a bastardisation of Jest), but a simple |
I'm also experiencing this sometimes. Is there any workaround? Can I completely disable jest's console wrapper? |
I'm using the following workaround to wait for the console to be flushed afterAll(async (done) => {
if (!process.stdout.write('')) {
process.stdout.once('drain', () => { done(); });
}
});
Probably not an ideal solution, but works for now. |
In my case, the widely suggested stdout flush technique, such as writing an empty string to stdout, did not work - because kernel buffering isn't the problem. I still have completed process.stdout.write() calls that haven't printed by the time jest calls process.stderr.write(). And since stderr doesn't buffer, the final test results wind up somewhere in the middle of chatty test output. I ultimately folded and went with this hack at the top of my test code:
I've tried reaching into the stdout._handle and turning off buffering via And I couldn't get this to reproduce with a simple test. Update: I tracked down the reason for my log output coming after Jest stderr write. See darrylwest/simple-node-logger#69 |
I solved mine with Update: the async isn't needed so my workaround is: |
Using |
Don't take this the wrong way, but I'm glad this is a Jest issue. I really thought I had a race condition on the library I'm working on. But firstly, I am aware Jest complains if logs happen after the test is done with a warning about async functions that are not awaited, but I get no such error, and secondly our library is being used in production-ready apps and I'm certain whenever I await the logs they always show up in the order I expect them to and without allowing following operations to run before the logs are printed to console. I can confirm both workarounds that @glenne and @pedroteixeira shared work. I won't go down the route of using For now I will add the const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
//...
afterAll(async () => { await sleep(100); }); If you'd like a reproducible case you can download the simplyhexagonal/logger repo and use this commit of ours and simply remove the aforementioned simplyhexagonal/logger@eb3ae2e Cheers |
@jeanlescure You can also use your sleep function inside an individual test to work around this by putting
at the end of an async test. My version works because afterAll has an overload where it passes a 'done' callback func to the user supplied function to signal completion. |
I think this is better tracked in #7900 |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Do you want to request a feature or report a bug?
I want to report a bug.
What is the current behavior?
The last
process.stdout.write
invocation in anasync
test is not flushed before Jest's test result summary is written.If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can
yarn install
andyarn test
.https://repl.it/repls/DeepQuickwittedMite
What is the expected behavior?
I expect the console messages from the tests to all appear before Jest's test result summary:
But instead the last output message is flushed after Jest's test result summary:
Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.
jest 21.2.1
node 6.12.0
npm 3.10.10
Windows 10.0
The text was updated successfully, but these errors were encountered: