ValentinH / jest-fail-on-console

Utility to make jest tests fail when console.error() or any other methods are used
MIT License
141 stars 20 forks source link

console shows only up on no other failtures #45

Closed daacrx closed 1 month ago

daacrx commented 6 months ago

This is the setup:

failOnConsole({ shouldFailOnError: true, shouldFailOnWarn: true, });

I recognized that the console will only print, if there is no other error or failed jest expect exception. So basically the console statement is not immediately printed when the code execution reaches the code, but later, eventually.

See example below:

test('will neither fail nor print due to console, but shows failing expect only', () => {
      console.error('show me please');
      expect(true).toBe(false);
    });

Is this expected? It would be nice to have a configuration to change this behavior and always and immediately show the console statement.

This is a workaround:

failOnConsole({
  shouldFailOnError: true,
  shouldFailOnWarn: true,
  silenceMessage: (errorMessage, methodName, context) => {
    /**
     * on local machine we want to display all logs. It must be console.log,
     * otherwise it produces infinite loops
     */
    if (process.env.CI && process.env.CI !== 'false') {
      console.log(errorMessage);
    }
    return false;
  },
});
ValentinH commented 6 months ago

Thank you for this feature request. I understand the need, would you like to create a PR to implement this as a new option? It should not be too complex.

ValentinH commented 1 month ago

Implemented in https://github.com/ValentinH/jest-fail-on-console/releases/tag/v3.3.0