Navigation Menu

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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impossible to test stdout #9984

Closed
melnikaite opened this issue May 6, 2020 · 7 comments
Closed

Impossible to test stdout #9984

melnikaite opened this issue May 6, 2020 · 7 comments

Comments

@melnikaite
Copy link

馃悰 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 
@jeysal
Copy link
Contributor

jeysal commented May 6, 2020

This is because of our custom Console implementation. @SimenB ideas on how to help with this use case?

@SimenB
Copy link
Member

SimenB commented May 6, 2020

I'd recommend just mocking console.log instead, and not rely on the implementation detail which is that node's implementation just call stdout.write(string + linefeed)

@jeysal
Copy link
Contributor

jeysal commented May 6, 2020

Yeah I thought of that as well, implementers of Console objects have quite a lot of freedom. There can be an argument that we try to match Node's behavior as much as possible with things like assertions, require, ... but in this case the UX our console provides is definitely worth it IMO, and I can't think of a better workaround than Simen for this particular use case.

@jeysal jeysal closed this as completed May 6, 2020
@melnikaite
Copy link
Author

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

@jeysal
Copy link
Contributor

jeysal commented May 6, 2020

In that case mocking winston itself would be best I'd say

@SimenB
Copy link
Member

SimenB commented May 6, 2020

I'm very much for making console call through to stdout at some point, PR welcome 馃槂 But we'd want to mock stdout first, I really like that feature of Jest

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants