-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Lodash: Remove a bunch of usages from tests #41751
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
Conversation
|
Size Change: +1.33 kB (0%) Total Size: 1.24 MB
ℹ️ View Unchanged
|
|
Would be really cool to merge this sooner, as it touches a bunch of files. It has the least impact because it touches only test files, but it's a nice little win. |
| * External dependencies | ||
| */ | ||
| import { uniqueId } from 'lodash'; | ||
| export const uniqueId = () => |
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 don't know if the replacement function is needed - the usage of uniqueId in these tests could just be replaced with hard-coded id values.
lodash uniqueId is just a counter that counts up from 1 rather than a random number:

So I'd expect every test run to produced the same ids, the first call to uniqueId() would return '1', the second '2' etc.
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.
Good point. Happy to simplify 👍 Done in 8c4d486
| instanceId={ uniqueId() } | ||
| label={ 'Padding' } | ||
| /> | ||
| <DimensionControl instanceId={ uuid() } label={ 'Padding' } /> |
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 also think using a generated id is probably overkill here too. If there's only one dimension control instance in each tree, the instanceId can just be the same value in each test.
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.
Good call, updated in 8c4d486 to use the same ID.
| const keyBy = ( arr, key ) => | ||
| ( arr || [] ).reduce( | ||
| ( r, x ) => ( { ...r, [ key ? x[ key ] : x ]: x } ), | ||
| {} | ||
| ); | ||
| const omit = ( obj, keys ) => | ||
| Object.fromEntries( | ||
| Object.entries( obj ).filter( ( [ key ] ) => ! keys.includes( key ) ) | ||
| ); |
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.
This is the kind of situation where I personally wouldn't be in favor of replacing lodash usage, because these functions are mostly simple to understand in terms of input and output, but the implementation is quite complex and they're only unit tested by proxy.
I do understand the overall goal here. I just wonder whether there's a better way.
The first one is also not meeting the coding standards, where shortened names like r and x shouldn't be used in favour of a more descriptive alternative. That's at least easily fixable.
edit: this isn't intended to act as a blocker though, just sharing some feedback.
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 all the candid feedback!
While this is in tests and has little impact, I think what you brought up is a valid concern to bring up and I'm happy to do another pass and simplify it. There's actually some opportunity for that here and I'm happy to grasp it.
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 pushed a couple of new commits, and I'd love to hear what you think:
- I completely got rid of
omit()in favor of a simpler declaration of the fixtures. It's unnecessary to declare a complex object and then pull it in pieces when we can just declare simpler objects in the first place. - I refactored
keyBya bit to make it simpler in the current context, and more readable.
Let me know what you think, and thanks again!
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.
Oh, and I hope this new version helps you reconsider this statement:
This is the kind of situation where I personally wouldn't be in favor of replacing lodash usage
😉
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.
Definitely better, thanks for addressing the feedback.
I still feel like some of these functions (e.g. isPlainObject) should be library code rather than having the implementation duplicated across the codebase.
Have there been any alternative explorations to this approach?
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 still feel like some of these functions (e.g. isPlainObject) should be library code rather than having the implementation duplicated across the codebase.
We can, and definitely will abstract some of those into library code as the need comes. isPlainObject in particular might end up being one of those.
I do agree with you, and I am definitely not planning to clutter the codebase with redundant snippet repetitions. If you take #41806 for example, I'm doing the opposite there - using the opportunity to abstract a bit of repeated code and make it better and well tested while removing Lodash from it at the same time.
However, I think it should be fine to take these into small steps, do the improvements incrementally, and not try to overengineer it from the beginning. It's a marathon, not a sprint, after all.
What do you think?
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.
Sounds good, thanks for replying to my concerns. Will give this one the thumbs up.
|
|
||
| for ( const variantField of fields ) { | ||
| for ( const constantField of fields.filter( | ||
| ( f ) => ! f === variantField |
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 think there is a typo on this condition, right now it is always false. I submitted a PR at #42280.
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 @jorgefilipecosta 🙌
What?
There's plenty of usage of Lodash in our tests. This PR aims to remove it in one sweep, because of the little impact all these usages have.
Why?
Lodash is known to unnecessarily inflate the bundle size of packages, and in most cases, it can be replaced with native language functionality. See these for more information and rationale:
@wordpress/api-fetchpackage haslodashas a dependency #39495How?
We're updating a bunch of methods, but the usages are mostly straightforward, and we only need the tests to be passing to ensure that the migration went well.
Testing Instructions