question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Impossible to test stdout

See original GitHub issue

🐛 Bug Report

Mocking process.stdout.write doesn’t work like expected

To Reproduce

describe('logger', () => {
    beforeAll(() => {
        jest.spyOn(process.stdout, 'write').mockImplementation(function () { return true; });
    });

    it('should log single string argument', () => {
        console.log('message');
        expect(process.stdout.write).toHaveBeenLastCalledWith('message');
    });
});

Expected: “message” Received: " console.log message

  at Object.<anonymous> (logger.test.js:11:17)·

", [Function anonymous]

When I run jest against multiple files jest logger.test.js another.test.js (event if another.test.js) is blank I see another error that means mocking doesn’t work at all

FAIL logger.test.js ● Console

console.log
  message

  at Object.<anonymous> (logger.test.js:11:17)

● logger › should log single string argument

expect(jest.fn()).toHaveBeenLastCalledWith(...expected)

Expected: "message"

Number of calls: 0

  10 |     it('should log single string argument', () => {
  11 |         console.log('message');
> 12 |         expect(process.stdout.write).toHaveBeenLastCalledWith('message');
     |                                      ^
  13 |     });
  14 | });
  15 | 

  at Object.<anonymous> (logger.test.js:12:38)

Expected behavior

test pass

envinfo

  System:
    OS: macOS 10.15.4
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
  Binaries:
    Node: 12.13.1 - ~/.nvm/versions/node/v12.13.1/bin/node
    npm: 6.14.5 - ~/.nvm/versions/node/v12.13.1/bin/npm
  npmPackages:
    jest: ^26.0.0 => 26.0.0 

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
melnikaitecommented, May 6, 2020

I simplified example for this report, but in fact I use winston So I ended up with jest.spyOn(console._stdout, 'write')

0reactions
github-actions[bot]commented, May 11, 2021

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. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is this a proper way to test stdout with Python 3 unittest?
I would like to test the stdout to see if the student has properly written out the print statement. Based on what I've...
Read more >
Python | Testing Output to stdout - GeeksforGeeks
Given a program that has a method whose output goes to standard Output (sys.stdout). This almost always means that it emits text to...
Read more >
Java/Kotlin Unit Testing – Test Stdout/Stderr/Logs
This allows for example testing output to log statements (by sending logs to stdout / console appender). From the JavaDocs: 1. 2. 3....
Read more >
Test::Output - Utilities to test STDOUT and STDERR messages.
Test ::Output provides a simple interface for testing output sent to STDOUT or STDERR . A number of different utilities are included to...
Read more >
Unit Testing of System.out.println() with JUnit | Baeldung
Learn about approaches for testing System.out.println. ... over direct interaction with standard output, sometimes this isn't possible.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found