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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow console.error/warn/log to be overwritten in tests #3239

Closed
paularmstrong opened this issue Mar 31, 2017 · 4 comments
Closed

Allow console.error/warn/log to be overwritten in tests #3239

paularmstrong opened this issue Mar 31, 2017 · 4 comments

Comments

@paularmstrong
Copy link

Do you want to request a feature or report a bug?
feature

What is the current behavior?
Due to the BufferedConsole, it's impossible to overwrite console.error, console.log, console.warn and console.info.

Use Case: We'd like to be able to have console.error and console.warn throw errors and fail tests.

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 and yarn test.

What is the expected behavior?
I expect that if I add console.error = (message) => { throw new Error(message); }; (globally, in a test, or in setupFiles/setupFramework), that any time console.error is called in a test it would throw an error and fail a test.

In reality, nothing happens.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

N/A

@thymikee
Copy link
Collaborator

thymikee commented Apr 1, 2017

Use mocking function jest.fn():

console.warn = jest.fn(warn => {
  throw new Error(warn);
});

@thymikee thymikee closed this as completed Apr 1, 2017
@paularmstrong
Copy link
Author

paularmstrong commented Apr 3, 2017

@thymikee This isn't working as expected either, using a global setup file:

in "setupTestFrameworkScriptFile": "<rootDir>/tools/jest/setup-framework.js"

beforeEach(() => {
  console.error = jest.fn((error) => {
    throw new Error(error);
  });
});

In test:

const somePromiseThatLogsConsoleError = () => {
  return new Promise((resolve, reject) => {
    console.error('foobar');
    resolve();
  });
});
  
it('test that has console.error', () => {
  return somePromiseThatLogsConsoleError().then(() => {
    expect(false).toBeTrue();
  });
});

@aaronabramov
Copy link
Contributor

i just tried it and it worked:

// test file
test('lol', () => {
  return new Promise((resolve) => {
    console.log('lol');
    resolve();
  });
});
// setup file
global.console.log = () => {
  throw new Error('hey!');
};

result:

 __tests__/test-test.js
  ✕ lol (3ms)

  ● lol

    hey!

      at CustomConsole.Object.<anonymous>.global.console.log (setup.js:2:9)
      at resolve (__tests__/test-test.js:3:13)
      at Object.<anonymous>.test (__tests__/test-test.js:2:10)
      at process._tickCallback (internal/process/next_tick.js:109:7)

Test Summary
 › Ran all tests.
 › 1 test failed, 0 tests passed (1 total in 1 test suite, run time 0.661s)

@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 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants