ValentinH / jest-fail-on-console

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

Respect Jest flag `--noStackTrace` #30

Open isaiahshiner opened 1 year ago

isaiahshiner commented 1 year ago

Is it possible for this library to support the --noStackTrace Jest flag out of the box? That is, all console.x should still cause an error, and print expected not to call... but have no stack trace? It looks like this might be possible with a custom error message, but it might make sense for it to work automatically. I also might like to always have no stack traces for fail on console, but that's a stretch goal. I could also consider a PR if you could point me in the right direction.

If it should work and I have something wrong, let me post my exact behavior. Jest is run as a script through pnpm, so my actual command and output looks like this: pnpm jest -- TheTestToRun

TheTestToRun

Expected test not to call console.warn().

If the warn is expected, test for it explicitly by mocking it out using jest.spyOn(console, 'warn') and test that the warning occurs.

Unmatched GET to /logout/expire_if_idle
    at console.newMethod (/Users/.../node_modules/.pnpm/jest-fail-on-console@2.1.1/node_modules/jest-fail-on-console/index.js:20:25)
... continues for ~20 more lines

When using pnpm jest -- --silent --noStackTrace TheTestToRun, I get the exact same output.

ValentinH commented 1 year ago

The important thing for this to work automatically is to know if there is a way to detect that this flag was passed. I couldn't find any info about this topic.

Otherwise, not outputting the stack trace doesn't require much change. Here's the place where we output it: https://github.com/ValentinH/jest-fail-on-console/blob/main/index.js#L27

isaiahshiner commented 1 year ago

Thanks for the quick response! I see that it's not nearly as much code as I was picturing, I'll see if I can get a pull request together, maybe later in the month. I'll also see if I can find out if Jest makes the flag status available anywhere.