question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Make console.error (and warn?) log a stack trace and codeframe like thrown errors do

See original GitHub issue

šŸš€ Feature Proposal

99% of the time, if console.error and console.warn are called during tests (and theyā€™re not mocked), itā€™s something that needs to be fixed.

Motivation

In particular, with Reactā€™s new act warning, people are seeing output like this:

 console.error node_modules/react-dom/cjs/react-dom.development.js:545
    Warning: An update to User inside a test was not wrapped in act(...).
    
    When testing, code that causes React state updates should be wrapped into act(...):
    
    act(() => {
      /* fire events that update state */
    });
    /* assert on the output */
    
    This ensures that you're testing the behavior the user would see in the browser. Learn more at https://fb.me/react-wrap-tests-with-act
        in User

Itā€™s not entirely helpful. It does show that this is happening in the User component, but there could be any number of places within that component that could have triggered that error to be logged.

Example

Hereā€™s a proof of concept of what Iā€™m suggesting (definitely wouldnā€™t work like this if built-in):

const originalError = console.error
beforeAll(() => {
  console.error = (...args) => {
    console.log(new Error().stack)
    originalError(...args)
  }
})

afterAll(() => {
  console.error = originalError
})

This would look like:

console.log src/__tests__/tmp.js:31
    Error: 
        at CustomConsole.console.error (/Users/kentcdodds/code/react-testing-library/src/__tests__/tmp.js:31:17)
        at warningWithoutStack (/Users/kentcdodds/code/react-testing-library/node_modules/react-dom/cjs/react-dom.development.js:545:32)
        at warnIfNotCurrentlyActingUpdatesInDEV (/Users/kentcdodds/code/react-testing-library/node_modules/react-dom/cjs/react-dom.development.js:23281:7)
        at dispatchAction (/Users/kentcdodds/code/react-testing-library/node_modules/react-dom/cjs/react-dom.development.js:15813:9)
        at setUser (/Users/kentcdodds/code/react-testing-library/src/__tests__/tmp.js:8:5)
        at processTicksAndRejections (internal/process/task_queues.js:89:5)

  console.error src/__tests__/tmp.js:32
    Warning: An update to User inside a test was not wrapped in act(...).
    
    When testing, code that causes React state updates should be wrapped into act(...):
    
    act(() => {
      /* fire events that update state */
    });
    /* assert on the output */
    
    This ensures that you're testing the behavior the user would see in the browser. Learn more at https://fb.me/react-wrap-tests-with-act
        in User

And visually:

image

And it could be potentially improved within Jest so itā€™s even more helpful (with color coding to highlight the part of the code that triggered the error). Similar to when an error is thrown.

    some error in React

       6 |   async function fetchUserData(id) {
       7 |     const response = await fetch(`/${id}`)
    >  8 |     setUser(await response.json())
         |     ^
       9 |   }
      10 |   React.useEffect(() => {
      11 |     fetchUserData(props.id)

      at dispatchAction (node_modules/react-dom/cjs/react-dom.development.js:15809:11)
      at setUser (src/__tests__/tmp.js:8:5)

And visually:

image

To be clear, what I mean is that console.error could print out a stack trace and codeframe where the part of the stack trace which is not in user code could be dimmed to reduce confusion and the codeframe would show the part of the userā€™s code which ultimately triggered the error to be logged.

Pitch

I think this would help more than just act and it seems like the correct place for this to happen. It would help people get rid of error (and potentially warn) logs quicker, and move on to building their apps faster (which I believe is the goal of Jest).

Reference: https://github.com/testing-library/react-testing-library/issues/434

Iā€™d love to hear what you all think!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:59
  • Comments:39 (15 by maintainers)

github_iconTop GitHub Comments

9reactions
gaearoncommented, Mar 20, 2020

Doing this only for console.error and console.warn matches how browsers show expandable arrows with JS stack next to them. The implementation itself is just new Error().stack. Sure, itā€™s not asynchronous ā€” but thatā€™s a question to VM implementors. Eventually it would be (or maybe it already is in recent Nodes). This sounds like a simple enough proposal and likely doable in 30 minutes. Let me tweet this.

7reactions
SimenBcommented, Apr 19, 2020

Released in 25.4.0, please try it out šŸ™‚

Read more comments on GitHub >

github_iconTop Results From Across the Web

Š“эŠ½ on Twitter: "Anyone wants to add JS stack to console.error ...
Make console.error (and warn?) log a stack trace and codeframe like thrown errors do Ā· Issue #8819... Feature Proposal 99% of the time,Ā ......
Read more >
How can I get a JavaScript stack trace when I throw an ...
In all modern browsers you can simply call: console.trace(); (MDN ... an exception throw new Error(); } catch(e) { console.log(e.stack); }.
Read more >
console.trace() - Web APIs - MDN Web Docs
The console.trace() method outputs a stack trace to the Web console.
Read more >
Improve test error messages of your abstractions
How to manipulate stack traces to get beautiful error messages with Jest and your test helper functions.
Read more >
Diagnostics and Logging - Parcel
This can be anything from a verbose message to a warning or error. ... Errors that are thrown are sent to Reporter plugins,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found