-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Use @wordpress/warning during block registration instead of console.error and console.warn
#63610
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
Use @wordpress/warning during block registration instead of console.error and console.warn
#63610
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
There are 4 failing unit tests reported that needs to be still fixed and this will be good to go. I really like this refactoring 💯 |
| it( 'should reject blocks without a namespace', () => { | ||
| const block = registerBlockType( 'doing-it-wrong' ); | ||
| expect( console ).toHaveErroredWith( | ||
| expect( console ).toHaveWarnedWith( | ||
| 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block' | ||
| ); | ||
| expect( block ).toBeUndefined(); | ||
| } ); | ||
|
|
||
| it( 'should reject blocks with too many namespaces', () => { | ||
| const block = registerBlockType( 'doing/it/wrong' ); | ||
| expect( console ).toHaveErroredWith( | ||
| expect( console ).toHaveWarnedWith( | ||
| 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block' | ||
| ); | ||
| expect( block ).toBeUndefined(); | ||
| } ); |
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 seems that the @wordpress/warning package only triggers one message, which is causing these tests to fail: link. I assume that's expected, right? Any idea how to solve 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.
OK, so the same message is printed only once. You would have to reset it through:
https://github.com/WordPress/gutenberg/blob/release/14.2/packages/warning/src/utils.js
There are multiple ways in Jest to do it, you can import logged and call a method on this set, for example.
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.
import logged from 'path/to/warning/src/utils.js';
logged.clear();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.
gutenberg/packages/warning/src/test/index.js
Lines 10 to 13 in 4171f27
| afterEach( () => { | |
| process.env.NODE_ENV = initialNodeEnv; | |
| logged.clear(); | |
| } ); |
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.
Okay, I made the changes in this commit and tests are passing (at least locally).
However, importing from '../../../../warning/src/utils' feels pretty weird. What happens to other projects wanting to use this package and add tests like ours but don't have the package in the same repo? Should we export it somehow?
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 wouldn’t worry about it. You could also force Jest to reload the package with some of the utils they provide:
|
Size Change: -615 B (-0.04%) Total Size: 1.75 MB
ℹ️ View Unchanged
|
gziolo
left a comment
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 looks great. Thank you for a swift follow-up.
…e.error` and `console.warn` (WordPress#63610) * Use `@wordpress/warning` in block registration * Clear logged set Co-authored-by: SantosGuillamot <[email protected]> Co-authored-by: gziolo <[email protected]>


What?
As suggested here by @gziolo , in this pull request I explore the possibility of using
@wordpress/warningduring block registration instead of the currentconsole.errorandconsole.warncalls.This means that all the previous errors are now warnings, which I am not sure if it's fine:
Before

After

Why?
To unify how these use cases are handled and to benefit from the functionalities the package provides by default.
How?
Just change the console calls with the
warningfunction and adapt the tests.Testing Instructions
Automated tests should pass.