Make WordPress Core


Ignore:
Timestamp:
11/30/2025 12:54:26 AM (8 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/script-modules/wpScriptModules.php

    r61073 r61323  
    20602060                );
    20612061
    2062                 $deregister( array( 'b', 'c ' ) );
     2062                $deregister( array( 'b', 'c' ) );
    20632063
    20642064                // Test that registered dependency in footer doesn't place dependant in footer.
     
    23132313                );
    23142314        }
     2315
     2316        /**
     2317         * Tests that a missing script module dependency triggers a _doing_it_wrong() notice.
     2318         *
     2319         * @ticket 64229
     2320         * @covers WP_Script_Modules::sort_item_dependencies
     2321         */
     2322        public function test_missing_script_module_dependency_triggers_incorrect_usage() {
     2323                $expected_incorrect_usage = 'WP_Script_Modules::register';
     2324                $this->setExpectedIncorrectUsage( $expected_incorrect_usage );
     2325
     2326                $this->script_modules->enqueue( 'main-module', '/main-module.js', array( 'missing-mod-dep' ) );
     2327
     2328                $markup = get_echo( array( $this->script_modules, 'print_enqueued_script_modules' ) );
     2329                $this->assertStringNotContainsString( 'main-module.js', $markup, 'Expected script module to be absent.' );
     2330
     2331                $this->assertArrayHasKey(
     2332                        $expected_incorrect_usage,
     2333                        $this->caught_doing_it_wrong,
     2334                        'Expected WP_Script_Modules::register to be reported via doing_it_wrong().'
     2335                );
     2336
     2337                // Assert the message mentions the missing dependency handle.
     2338                $this->assertStringContainsString(
     2339                        'The script module with the ID "main-module" was enqueued with dependencies that are not registered: missing-mod-dep',
     2340                        $this->caught_doing_it_wrong[ $expected_incorrect_usage ]
     2341                );
     2342        }
    23152343}
Note: See TracChangeset for help on using the changeset viewer.