Skip to content

Pr/440 show line in debug #733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

victorandcode
Copy link
Contributor

@victorandcode victorandcode commented Aug 8, 2020

What:
Add the call frame to the output of screen.debug before the prettified DOM

Why:
For easier debugging.
Resolves #440

How:

  • Create an empty Error and filter it's stack frames to keep only the one we care about
  • Coloring the message with chalk

Before
Screenshot from 2020-08-08 21-00-53

After
Screenshot from 2020-08-08 20-53-46

Here's the code for the test
Screenshot from 2020-08-08 20-54-56

Checklist:

  • Documentation added to the
    docs site
  • Tests
  • Typescript definitions updated
  • Ready to be merged

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 8, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 6c6df90:

Sandbox Source
kentcdodds/react-testing-library-examples Configuration

@codecov
Copy link

codecov bot commented Aug 8, 2020

Codecov Report

Merging #733 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #733   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           24        25    +1     
  Lines          675       702   +27     
  Branches       181       183    +2     
=========================================
+ Hits           675       702   +27     
Impacted Files Coverage Δ
src/get-user-code-frame.js 100.00% <100.00%> (ø)
src/pretty-dom.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 19901e1...6c6df90. Read the comment docs.

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks mostly ok to me (what are the chances we could create an actual codeframe? https://babeljs.io/docs/en/next/babel-code-frame.html)

@victorandcode victorandcode force-pushed the pr/440-show-line-in-debug branch from 2f5ee1d to 74ca2c2 Compare August 15, 2020 13:43
@victorandcode
Copy link
Contributor Author

victorandcode commented Aug 15, 2020

After doing some research based on the feedback, I believe we have 2 options to implement this feature 🧑‍🏭

If we only want to display the frame like location/to/my/file.js:10:2

This can only be achieved in node since we are counting on ignoring external packages inside node_modules. Also, we need the environment (jest), to calculate correctly the line of the error with source mapping. We can do this by using the inNode function and only printing the frame when appropriate.

If we want to display the code frame as suggested by @kentcdodds

This feature can be achieved via @babel/code-frame + node's fs module. I don't see any alternative to do this on the browser, therefore, it would be node exclusive like the previous option.

In order to achieve this, we would need to do some tweaking in the build config to mock those 2 modules I mentioned so these don't cause any trouble in the browser. This is still pending on the PR until we reach a decision.

Let me know if you feel like any of those 2 options is worth pursuing :)

@kentcdodds
Copy link
Member

I like option 2. I don't think we'd need anything special in config. We could do something like this: https://github.com/testing-library/react-testing-library/blob/master/src/flush-microtasks.js#L28

@victorandcode victorandcode force-pushed the pr/440-show-line-in-debug branch from 74ca2c2 to 18fb7dd Compare August 15, 2020 20:20
Comment on lines 6 to 12
const nodeRequire = module && module.require
readFileSync = nodeRequire.call(module, 'fs').readFileSync
codeFrameColumns = nodeRequire.call(module, '@babel/code-frame')
.codeFrameColumns
} catch {
// We're in a browser environment
/* istanbul ignore next */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that these lines run while the module is loaded, I don't know of a practical way to mock them for a single test to force them to throw. That's why I'm ignoring coverage for now. If you know of a way to do this, I'd appreciate the guidance 🙏

@victorandcode
Copy link
Contributor Author

Option 2 is now implemented and code frames are now displayed 🎉 Please take a look when you have some time. I also tested the UMD build in chrome and it's working fine.

Here's an example:
Screenshot from 2020-08-16 11-18-02
Screenshot from 2020-08-16 11-18-24

@victorandcode victorandcode force-pushed the pr/440-show-line-in-debug branch 2 times, most recently from e36f525 to 439a4d7 Compare August 16, 2020 11:57
@kentcdodds
Copy link
Member

This is fantastic! I'll give the code a deeper look later, but one thing: could we put the codeframe at the bottom?

@kentcdodds
Copy link
Member

Oh and the line above the codeframe appears to be listing the wrong file, it should show the one that is shown in the codeframe right?

@victorandcode
Copy link
Contributor Author

victorandcode commented Aug 16, 2020

Oh and the line above the codeframe appears to be listing the wrong file, it should show the one that is shown in the codeframe right?

So actually that line comes from jest, it's where console.log is actually called. I have looked into it but I haven't found a way to prevent it :(

@victorandcode
Copy link
Contributor Author

Oh and the line above the codeframe appears to be listing the wrong file, it should show the one that is shown in the codeframe right?

This has now been fixed. Here's the new version:
Screenshot from 2020-08-16 15-50-31

@kentcdodds
Copy link
Member

Super! Let's add another line between the debug output and the line listing where the file is, and let's also make that line muted like the console.log line. Thanks!

@victorandcode victorandcode force-pushed the pr/440-show-line-in-debug branch from 08a4c55 to 32aa265 Compare August 16, 2020 18:46
@victorandcode
Copy link
Contributor Author

Super! Let's add another line between the debug output and the line listing where the file is, and let's also make that line muted like the console.log line. Thanks!

This is now fixed and looks like this 👀

I still need to take a look at the CI error, it shouldn't be failing since locally it doesn't with the same script 🤔

Screenshot from 2020-08-16 20-48-25

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Thank you. Just a few things.


test('it returns only client code frame when code frames from node_modules are first', () => {
const stack = `Error: Kaboom
at Object.<anonymous> (/home/john/projects/projects/sample-error/node_modules/@es2050/console/build/index.js:4:10)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This says john/projects/projects, but the snapshot just says john/projects/ 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On this particular test, that frame should actually be ignored since it's inside node_modules. The one we care about is one line after ${userStackFrame}.
However, the frame that is ignored does have a typo (/projects/projects). I've corrected this and simplified the paths used for all tests to avoid confusion. They all now start with /sample-error instead of /home/john/<etc>

@victorandcode victorandcode force-pushed the pr/440-show-line-in-debug branch 2 times, most recently from 5b91123 to 742dbbd Compare August 20, 2020 19:30
@victorandcode victorandcode force-pushed the pr/440-show-line-in-debug branch 2 times, most recently from be70a1f to e04c63d Compare August 30, 2020 08:16
@victorandcode victorandcode force-pushed the pr/440-show-line-in-debug branch from e04c63d to 19901e1 Compare August 30, 2020 08:31
@victorandcode victorandcode reopened this Aug 30, 2020
@victorandcode victorandcode force-pushed the pr/440-show-line-in-debug branch 2 times, most recently from e04c63d to d641ec1 Compare August 30, 2020 08:56
@victorandcode victorandcode force-pushed the pr/440-show-line-in-debug branch from d641ec1 to 6c6df90 Compare August 30, 2020 09:01
@victorandcode
Copy link
Contributor Author

Hey @kentcdodds, please let me know if the latest changes look ok.

All comments have been addressed but the travis error still puzzles me. It fails in a seemingly unrelated file (role.js) but passes locally.

I also tried temporarily rolling back my changes, creating a very simple commit but the error still happened.

I was wondering if this had happened before

@kentcdodds
Copy link
Member

Yeah, that's odd. I'm not sure what's up with.

Anyone else want to investigate?

@kentcdodds
Copy link
Member

Tried it locally myself and everything worked fine. So I'm going to just hope this is an issue with CI for this PR and we'll go ahead and merge it. I'll deal with it if it persists beyond this PR CI issue.

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is solid. Thanks!

@kentcdodds
Copy link
Member

Looks like there was a weird issue in CI. Cleared cache and ran again and everything's fine now.

@kentcdodds kentcdodds merged commit dfb4aa1 into testing-library:master Sep 2, 2020
@testing-library-bot
Copy link

🎉 This PR is included in version 7.24.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@kentcdodds
Copy link
Member

@all-contributors please add @victorandcode for code and tests

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @victorandcode! 🎉

@victorandcode
Copy link
Contributor Author

Looks like there was a weird issue in CI. Cleared cache and ran again and everything's fine now.

Thanks for taking a look :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

show line in debug()
4 participants