test-runner
test-runner copied to clipboard
[Question] Is there a way to fail a test on console.error?
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 viasetupFilesAfterEnvin the Jest config - Ejected, implemented global overrides for console to throw when
errororwarningare called viasetupFilesAfterEnv
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.
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.
It looks like there's now a --failOnConsole option. Should this be closed?