-
Notifications
You must be signed in to change notification settings - Fork 472
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(wait): wait will now also run your callback on DOM changes #415
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 a00cc96:
|
@@ -1,71 +1,21 @@ | |||
import { |
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.
It's easier to just look at the post-change version of this file: https://github.com/testing-library/dom-testing-library/blob/pr/wait-improvements/src/wait-for-element.js
@@ -1,85 +1,48 @@ | |||
import { |
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.
It's easier to just look at the post-change version of this file: https://github.com/testing-library/dom-testing-library/blob/pr/wait-improvements/src/wait-for-element-to-be-removed.js
b2619b6
to
2ceb1a0
Compare
That's weird. I think the codesandbox thing is a fluke. |
Hi Kent, I was gonna post a suggestion/PR, but since this one is about Can we expose await till(() => expect(MockRedirect).toHaveBeenCalledWith({to: '/'}, {})); I know we can alias our imports.. just thought the API would look nicer.. though it might not be worth introducing new API changes (at this time) Cheers, |
Hi @mustafawm, thanks for the feedback. I do prefer to avoid aliases when possible. We have an alias for I'm not sure there's enough justification to alias this function, but you certainly could do this in your own utils module. Good luck! |
Hey Ken, thanks for your quick reply. Agree. tbh, I wasn't feeling strongly about it .. just thought I put it out there and see :) Thanks for the utils module suggestion 🙏 |
Hey Kent, Thanks for pinging me here and letting me know - it will be bittersweet to see the installs of wait-for-expect plummet but it's for a good reason - the tiny helper is usable on it's own outside of dom-testing scope so we shouldn't be adding more complexity to it, and the dom-testing could use some improvements.. :) I also like the idea of simplifying the API. To be honest in a lot of projects I've seen and helped with, people didn't even bother to use waitForDomChange, wait "just worked". Optimizations (savings of maybe a few dozens of ms per test) were not worth the extra pause (and learning) to decide which of the wait* tools the programmer should use in a given situation. I'm glad that the initial design we've came up with lives on. It's so intuitive and clean. My only regret is the default long timeout - I was basing it off what I knew from browser-based testing (selenium, chimp, cypress, etc). But in more of a low-level environment the timeout hardly ever needs to go that high, with the downside that once things break the tests take forever to fail. (In browser-based testing the test would maybe take 8 seconds to fail instead of 4 seconds to pass, but in integration we can jump from 100 ms to 4500 ms which is much more annoying). It brakes the flow. It might be worth pointing this out more explicitly in the docs, that people might want to consider to change the default, if they run lightweight tests. But, at the same time, this family of libraries is all based on "smart defaults" :) Also - if you are going to do some breaking changes, this might be something to rethink, maybe? |
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.
Looks like a sensible change! Not only we remove a dependency, we also set the stage to simplify the exposed API in a future major version.
I've added a few comments here and there, but looks great overall 👍
// As the name implies, waitForElementToBeRemoved should check `present` --> `removed` | ||
if (isRemoved(callback())) { | ||
throw new Error( | ||
'The callback function which was passed did not return an element or non-empty array of elements. waitForElementToBeRemoved requires that the element(s) exist before waiting for removal.', |
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.
nitpicking ^^ sry about that
'The callback function which was passed did not return an element or non-empty array of elements. waitForElementToBeRemoved requires that the element(s) exist before waiting for removal.', | |
'The callback function which was passed did not return an element or non-empty array of elements. waitForElementToBeRemoved requires that the element(s) exist(s) before waiting for removal.', |
Maybe we should change the default timeout as part of this change as well. I think it makes more sense for it to be ~1 second. If someone's test is taking longer than that for a single What do folks think about that? |
+1 at setting a lower default value for the timeout – I've never found a case where a 4.5s timeout would make a (well-written and properly mocked) test pass while a lower one would make the same test fail. I'm ok with 1s. |
2ceb1a0
to
d7789a4
Compare
Rebased master and added two commits. If folks could take a look at the last two commits that would be super duper. Thanks! |
Codecov Report
@@ Coverage Diff @@
## beta #415 +/- ##
===================================
Coverage 100% 100%
===================================
Files 22 22
Lines 404 412 +8
Branches 95 99 +4
===================================
+ Hits 404 412 +8
Continue to review full report at Codecov.
|
asyncUtilTimeout: 4500, | ||
asyncUtilTimeout: 1000, |
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.
Just realized I amended the first of those two changes. So this is all that first change was.
// deprecated... TODO: remove this method. People should use wait instead | ||
// the reasoning is that waiting for just any DOM change is an implementation | ||
// detail. People should be waiting for a specific thing to change. |
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 we plan to remove this method in the next major release, should be add a warning so people know they are relying on a soon-to-be-gone method? Is it too invasive?
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'm kinda thinking that we should add the warning in the major version bump, and then remove it in the next one.
I'm starting to think that instead of deprecating we should just remove it entirely what do people think? To be clear, I'm talking about
Thoughts? |
Changed my mind. We'll just have a deprecation warning for this release. People will be less mad at us 😅 |
d7789a4
to
6e793e0
Compare
🎉 This PR is included in version 7.0.0-beta.2 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Closes #376 Closes #416 BREAKING CHANGE: `waitForElement` is deprecated in favor of `find*` queries or `wait`. BREAKING CHANGE: `waitForDomChange` is deprecated in favor of `wait` BREAKING CHANGE: default timeout for async utilities is now 1000ms rather than 4500ms. This can be configured: https://testing-library.com/docs/dom-testing-library/api-configuration
Closes #376 Closes #416 BREAKING CHANGE: `waitForElement` is deprecated in favor of `find*` queries or `wait`. BREAKING CHANGE: `waitForDomChange` is deprecated in favor of `wait` BREAKING CHANGE: default timeout for async utilities is now 1000ms rather than 4500ms. This can be configured: https://testing-library.com/docs/dom-testing-library/api-configuration
What: Make
wait
use a MutationObserver in addition to the previous behaviorWhy: This way we can get rid of
waitForDomChange
(in a future release) and we drastically simplify the other async utilities.wait
also gets a super-power of being able to re-run the callback faster than the interval when the DOM is changed, so this should speed up people's tests who are usingwait
today. Closes #376How: Remove
wait-for-expect
(cc @lgandecki) because it wont work nicely with this approach. I also refactored the other utilities to build on top ofwait
(which is something I've wanted to do for a long time now).Checklist:
I've prepared a PR for types targeting DefinitelyTyped(this should not be necessary. No APIs have changed)