Skip to content

Impossible to test stdout #9984

@melnikaite

Description

@melnikaite

🐛 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 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions