Make WordPress Core


Ignore:
Timestamp:
11/30/2025 12:54:26 AM (9 months ago)
Author:
westonruter
Message:

Script Loader: Emit notices when enqueueing a script, style, or script module with missing dependencies.

Developed in https://github.com/WordPress/wordpress-develop/pull/10545

Follow-up to [60999].

Props deepakprajapati, westonruter.
See #63486.
Fixes #64229.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r61223 r61323  
    40944094                $this->assertStringNotContainsStringIgnoringCase( 'sourceURL=', $translations_script_data );
    40954095        }
     4096
     4097        /**
     4098         * Tests that WP_Scripts emits a _doing_it_wrong() notice for missing dependencies.
     4099         *
     4100         * @ticket 64229
     4101         * @covers WP_Dependencies::all_deps
     4102         */
     4103        public function test_wp_scripts_doing_it_wrong_for_missing_dependencies() {
     4104                $expected_incorrect_usage = 'WP_Scripts::add';
     4105                $this->setExpectedIncorrectUsage( $expected_incorrect_usage );
     4106
     4107                wp_register_script( 'registered-dep', '/registered-dep.js' );
     4108                wp_enqueue_script( 'main', '/main.js', array( 'registered-dep', 'missing-dep' ) );
     4109
     4110                $markup = get_echo( 'wp_print_scripts' );
     4111                $this->assertStringNotContainsString( 'main.js', $markup, 'Expected script to be absent.' );
     4112
     4113                $this->assertArrayHasKey(
     4114                        $expected_incorrect_usage,
     4115                        $this->caught_doing_it_wrong,
     4116                        "Expected $expected_incorrect_usage to trigger a _doing_it_wrong() notice for missing dependency."
     4117                );
     4118
     4119                $this->assertStringContainsString(
     4120                        'The script with the handle "main" was enqueued with dependencies that are not registered: missing-dep',
     4121                        $this->caught_doing_it_wrong[ $expected_incorrect_usage ],
     4122                        'Expected _doing_it_wrong() notice to indicate missing dependencies for enqueued script.'
     4123                );
     4124        }
    40964125}
Note: See TracChangeset for help on using the changeset viewer.