-
Notifications
You must be signed in to change notification settings - Fork 467
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
feat: Print link to the Testing-Library playground when getBy* queries fail #852
base: main
Are you sure you want to change the base?
feat: Print link to the Testing-Library playground when getBy* queries fail #852
Conversation
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 07aa3a1:
|
Codecov Report
@@ Coverage Diff @@
## main #852 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 26 27 +1
Lines 966 977 +11
Branches 293 303 +10
=========================================
+ Hits 966 977 +11
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
It appears that Prettier might have formatted the inline snapshots differently, I'm not sure why 🤔 |
34c9548
to
f62b3a9
Compare
Before I will call this Ready to be merged, I have a few questions:
|
f62b3a9
to
1f556b1
Compare
f2371c3
to
b567f10
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for taking so long.
src/playground-helper.ts
Outdated
} | ||
|
||
function getPlaygroundUrl(element: Element | null, logErrors = true) { | ||
if (!element || !('innerHTML' in element)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!element || !('innerHTML' in element)) { | |
if (element === null) { |
Should be sufficient looking at the type definitions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original if-statement in logTestingPlaygroundURL
in screen.js
look like this:
if (!element || !('innerHTML' in element))
So changing it will cause a behaviour change for that function. Are we okay with that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering we now have the type-safety I'm ok with it. Unless the types are inaccurate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm.. 🤔
In screen.d.ts
the type for the element parameter is Element | HTMLDocument
. However HTMLDocument
does not have an innerHTML
property.
It's not really clear to me why logTestingPlaygroundURL
accepts a HTMLDocument
if it requires innerHTML
at the same time.
Should I keep Element | null
or is Element | HTMLDocument | null
the correct type?
Perhaps the type definition in screen.d.ts
needs to be updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another question: do we even want the | null
? From the type definition it sounds like null
is a valid value, when in fact it's an erroneous value to give as parameter.
If we do not have | null
though, we will have to remove the first check or shut up ESLint with:
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!element) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update:
- I have decided to remove the
| null
, so we're not "advertising" thatnull
can be used as a valid value. - I don't think
HTMLDocument
is a valid type to use here, so to me it looks like that was an error in the old type definition. - I have removed a test that tried using
{}
as an argument and expectingThe element you're providing isn't a valid DOM element
. That error will no longer be logged if you're passing{}
, because I removed the!('innerHTML' in element)
check, and rely on static type checking instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That error will no longer be logged if you're passing {}, because I removed the !('innerHTML' in element) check, and rely on static type checking instead.
We generally don't relly on static type checking for users of our library. We may change that in the future but for now additional runtime errors should not be removed.
But these errors should happen in public methods. If getPlaygroundUrl
is internal only then we can rely on static type checking and error should be thrown in the public methods using getPlaygroundUrl
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation, I think I get it now! I have added the runtime check again in logTestingPlaygroundURL
.
No worries! This is hardly an urgent issue, so thanks for taking the time to review :-) |
e938cd9
to
bb23329
Compare
4f71920
to
1c4f22b
Compare
cabd517
to
0b3d36e
Compare
Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
src/playground-helper.ts
Outdated
function getPlaygroundUrl(element: Element) { | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (!element) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function getPlaygroundUrl(element: Element) { | |
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | |
if (!element) { | |
function getPlaygroundUrl(element: Element | null) { | |
if (element === null) { |
Let's match runtime and static types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I touched upon this a little bit: #852 (comment)
Do you still think we should match the types, given that null
is an erroneous type/value to pass in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #852 (comment)
internal methods: static type checking
public methods: static type checking + extraneous runtime type checking for users not using static type checking
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the runtime checks has been moved to logTestingPlaygroundURL
I was able to remove the if (element === null)
from here :-)
@eps1lon I think I finally understood the difference between error handling in internal and public facing functions, so the PR should be ready again now :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eps1lon I think I finally understood the difference between error handling in internal and public facing functions, so the PR should be ready again now :-)
Mostly because I was side-lining this PR and didn't put much time into explaining what I think this PR should look like. Thank your for sticking with it 👍
I'll do some git hygene with the snapshot diffs, ensure compatibility with the alpha and then try it out in Material-UI to check if the playground link is not too noisy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought: Are we sure this doesn't affect findBy*
perf? getMissingError
for Byrole
has a special branch when called in findBy*
. We need to make sure performance of findBy
isn't affected by this change.
What's the status of this issue? I want something like this implemented and would love to help. |
What:
Adds a link to the Testing-Library playground, when
getBy*
queries fail, as requested in #837.Why:
It adds awareness of the playground, which might help in making a better query.
How:
Changes the default
getElementError
function.Checklist:
docs site, a PR has been added: Add documentation for printPlaygroundLink configuration option testing-library-docs#726