test-runner icon indicating copy to clipboard operation
test-runner copied to clipboard

[Question] Is there a way to fail a test on console.error?

Open AaronWatson2975 opened this issue 2 years ago • 3 comments

Description

I'm wondering if there is a way to fail tests when errors or warnings are written to the console. There are a number of scenarios where I believe this could be a huge benefit (for example, with React UI libraries, prop type validation could be a great thing to catch in these tests).

Things I've Tried

  • Ejected, implemented jest-fail-on-console, and hooked it up via setupFilesAfterEnv in the Jest config
  • Ejected, implemented global overrides for console to throw when error or warning are called via setupFilesAfterEnv

Neither seemed to work (I was able to confirm the code was running, but the tests were still passing), despite working as expected with a vanilla Jest implementation.

Example

Here is an example of how I attempted to implement jest-fail-on-console

test-runner-jest-config.js

const { getJestConfig } = require('@storybook/test-runner');

module.exports = {
  ...getJestConfig(),
  setupFilesAfterEnv: ['<rootDir>/test/setupStorybookTests.js'],
};

test/setupStorybookTests.js

import failOnConsole from 'jest-fail-on-console'

failOnConsole()

Additional context

I just wanted to say thanks for creating this wonderful library, out of the box, being able to check all your stories to ensure they render without throwing errors with almost no setup, is a huge benefit. IMO having a way to fail on console errors and warnings could make it an even better value prop.

AaronWatson2975 avatar Feb 22 '23 20:02 AaronWatson2975

I'm looking through the code and think it would be easy to implement. This as currently the console-log-output is already being captured, to be displayed on an actual Storybook-story-error.

work933k avatar Jun 20 '23 11:06 work933k

It looks like there's now a --failOnConsole option. Should this be closed?

IanVS avatar Jun 07 '24 14:06 IanVS