Jest: handle stdout/stderr from the tests

Created on 9 Jun 2016  路  6Comments  路  Source: facebook/jest

right now whenever something is printed from within a test it's pretty random and breaks the output (esp if you're using json output format)

I think it'd be cool to wrap console.* methods and format the output. eg.

it('...', => {
  console.log('1234566');
  console.warn('111');
  console.error('222');
});
[log: myTest.js] 12334566
[warn: myTest.js] 111
[error: myTest.js] 222

Most helpful comment

@cpojer while it works, I don't think it's the most ideal solution, as you lose the callsite:

console.log node_modules/jest-mock/build/index.js:480
      resource "aws_route53_zone" "my_zone_com" {}

And you have to test for the parameters that were passed, which is not ideal:

let consoleLogSpy = jest.spyOn(console, 'log');

it('prints the file name', async () => {
  await AwsRoute53Zone.run([zoneId]);

  expect(consoleLogSpy).toHaveBeenCalledWith(
   "resource",
    expect.stringContaining('aws_route53_zone'),
    expect.stringContaining('route_my_zone_com'),
    JSON.stringify({})
  );
});

Would it be feasible to entertain the idea of having some form of expect.stdin & expect.stdout properties that can be used mid-test to test the input & output, seperate from the --verbose/silent flags?

(Personally I'm interested in the output, but figure it would make sense to have stdin too).

Effectively what I'm after is access to the underlying BufferedConsole.

All 6 comments

1435

done.

It seems this breaks modules that help test console output like https://www.npmjs.com/package/test-console or https://github.com/sindresorhus/hook-std

Any idea how to test console output from Jest?

RE: It seems that running with --verbose solved this for me

console.log = jest.fn();

and then treat it like any other mock.

Thanks @cpojer that worked like a charm!

@cpojer while it works, I don't think it's the most ideal solution, as you lose the callsite:

console.log node_modules/jest-mock/build/index.js:480
      resource "aws_route53_zone" "my_zone_com" {}

And you have to test for the parameters that were passed, which is not ideal:

let consoleLogSpy = jest.spyOn(console, 'log');

it('prints the file name', async () => {
  await AwsRoute53Zone.run([zoneId]);

  expect(consoleLogSpy).toHaveBeenCalledWith(
   "resource",
    expect.stringContaining('aws_route53_zone'),
    expect.stringContaining('route_my_zone_com'),
    JSON.stringify({})
  );
});

Would it be feasible to entertain the idea of having some form of expect.stdin & expect.stdout properties that can be used mid-test to test the input & output, seperate from the --verbose/silent flags?

(Personally I'm interested in the output, but figure it would make sense to have stdin too).

Effectively what I'm after is access to the underlying BufferedConsole.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rosiakr picture rosiakr  路  3Comments

withinboredom picture withinboredom  路  3Comments

ianp picture ianp  路  3Comments

jardakotesovec picture jardakotesovec  路  3Comments

gustavjf picture gustavjf  路  3Comments