Skip to content
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

Improved logging #45

Merged
merged 6 commits into from
May 30, 2018
Merged

Conversation

npeterkamps
Copy link
Collaborator

What:

  • fix: don't crash when jsdom's document is passed to prettyDOM.
  • feat(logging): don't log DOM in Cypress, don't highlight in the browser.

Why:

How:

  • Use document.documentElement in prettyDOM to have the actual element with an outerHTML property.
  • Refactored debugDOM function to determine the environment and how/whether to log the DOM element.

Checklist:

  • Documentation
  • Tests
  • Ready to be merged
  • Added myself to contributors table

- added test to check whether Cypress doesn't log DOM
- ignore .idea folder (JetBrains IDE's)
- added myself to contributors
@aulisius
Copy link

Hey. Nice PR! Waiting on this currently. @kentcdodds yo, how soon can you merge this?

Thanks,
Faizaan.

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 great! Just one thing.

src/queries.js Outdated
if (inBrowser && !inNode) {
return `\n\n${prettyDOM(htmlElement, limit, { highlight: false })}`
}
return `\n\n${prettyDOM(htmlElement, limit)}`
Copy link
Member

@kentcdodds kentcdodds May 29, 2018

Choose a reason for hiding this comment

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

Rather than adding the \n\n here, let's do this in the usage:

throw new Error(['Unable to find... etc...', debugDOM(container)].filter(Boolean).join('\n\n'))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In Cypress, that would result in having two empty lines at the end of the error message. debugDOM seems to be a private function for query errors, so I thought this implementation would be preferable.

Copy link
Member

Choose a reason for hiding this comment

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

Updated my comment to avoid that issue

@kentcdodds
Copy link
Member

Looks like were missing some coverage. If it's really difficult to simulate a non-browser environment at the moment, then you can use /* istanbul ignore next */ comments. Open ./coverage/lcov-report/index.html to see what's missing. Thanks!

@npeterkamps
Copy link
Collaborator Author

npeterkamps commented May 29, 2018

Looks like were missing some coverage. If it's really difficult to simulate a non-browser environment...

I'm wondering, in what kind of situation would dom-testing-library be running in a pure browser environment? We've handled Cypress' case. Codesandbox runs in a mixed environment and I think it should have the same results as Node for consistency (as explain in this comment). So maybe we don't need to support a browser-only environment and just have an exception for Cypress?

I'll look into the coverage issue, don't know why line 59 isn't being covered by the tests. EDIT: jsdom/jsdom#2175 is fixed 😄

@npeterkamps
Copy link
Collaborator Author

@kentcdodds I've pushed an update.

The question remains whether we want to keep the browser specific code or whether dom-testing-library isn't going to be used in a browser-only environment (in which case, we might as well remove the code). I'm talking about the following lines:

if (inBrowser && !inNode) {
  return prettyDOM(htmlElement, limit, { highlight: false })
}

Let me know what you think!

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.

Some thoughts and ideas.

src/queries.js Outdated
return ''
}
/* istanbul ignore if */
if (inBrowser && !inNode) {
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we could probably change this to:

if (inCypress) {
  return ''
} else if (inNode) {
  return prettyDOM(htmlElement, limit)
} else {
  return prettyDOM(htmlElement, limit, {highlight: false})
}

src/queries.js Outdated
container,
)}`,
)
throw new Error([
Copy link
Member

Choose a reason for hiding this comment

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

I'm starting to think this may be a good case for something like:

throw getElementError(message, container)

where getElementError is just:

const getElementError => new Error([message, debugDOM(container)].filter(Boolean).join('\n\n'))

What do you think?

@npeterkamps
Copy link
Collaborator Author

Agreed. Changes pushed.

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.

Super duper! Thank you so much for iterating on this so well!

@kentcdodds kentcdodds merged commit fd0c18c into testing-library:master May 30, 2018
@kentcdodds
Copy link
Member

Thanks so much for your help! I've added you as a collaborator on the project. Please make sure that you review the other/MAINTAINING.md and CONTRIBUTING.md files (specifically the bit about the commit messages and the git hooks) and familiarize yourself with the code of conduct (we're using the contributor covenant). You might also want to watch the repo to be notified when someone files an issue/PR. Please continue to make PRs as you feel the need (you can make your branches directly on the repo rather than your fork if you want). Thanks! And welcome to the team :)

@kentcdodds
Copy link
Member

🎉 This PR is included in version 2.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

3 participants