storybookjs / test-runner

🚕 Turn stories into executable tests
https://storybook.js.org/docs/writing-tests/interaction-testing
MIT License
231 stars 72 forks source link

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

Open AaronWatson2975 opened 1 year ago

AaronWatson2975 commented 1 year ago

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

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.

work933k commented 1 year ago

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.

IanVS commented 4 months ago

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

work933k commented 1 week ago

I think this issue might be closed. The "failOnConsole" option seems to work. Although it only works with Angular with Storybook when build NOT in production-mode. In NX this is controlled with the option enableProdMode=false.