Skip to content

Commit 5c80d79

Browse files
committed
Tests: Add doing_it_wrong() test for missing script module dependencies
1 parent 29772af commit 5c80d79

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tests/phpunit/tests/script-modules/wpScriptModules.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,9 @@ public function test_script_module_printing_and_dependency_ordering( bool $use_g
19651965
global $wp_version;
19661966
$wp_version = '99.9.9';
19671967

1968+
if ( $use_global_function && $only_enqueue ) {
1969+
$this->setExpectedIncorrectUsage( 'WP_Script_Modules::sort_item_dependencies' );
1970+
}
19681971
$register = static function ( ...$args ) use ( $use_global_function ) {
19691972
if ( $use_global_function ) {
19701973
wp_register_script_module( ...$args );
@@ -2312,4 +2315,31 @@ public function test_static_import_dependency_with_dynamic_imports_depending_on_
23122315
"Expected script modules to match snapshot:\n$script_modules"
23132316
);
23142317
}
2318+
2319+
/**
2320+
* Tests that a missing script module dependency triggers a _doing_it_wrong() notice.
2321+
*
2322+
* @ticket 64229
2323+
* @covers WP_Script_Modules::sort_item_dependencies
2324+
*/
2325+
public function test_missing_script_module_dependency_triggers_incorrect_usage() {
2326+
$this->setExpectedIncorrectUsage( 'WP_Script_Modules::sort_item_dependencies' );
2327+
2328+
$this->script_modules->register( 'main-module', '/main-module.js', array( 'missing-mod-dep' ) );
2329+
$this->script_modules->enqueue( 'main-module' );
2330+
2331+
get_echo( array( $this->script_modules, 'print_enqueued_script_modules' ) );
2332+
2333+
$this->assertArrayHasKey(
2334+
'WP_Script_Modules::sort_item_dependencies',
2335+
$this->caught_doing_it_wrong,
2336+
'Expected WP_Script_Modules::sort_item_dependencies to be reported via doing_it_wrong().'
2337+
);
2338+
2339+
// Assert the message mentions the missing dependency handle.
2340+
$this->assertStringContainsString(
2341+
'The script module main-module was enqueued with dependencies that are not registered: missing-mod-dep',
2342+
$this->caught_doing_it_wrong['WP_Script_Modules::sort_item_dependencies']
2343+
);
2344+
}
23152345
}

0 commit comments

Comments
 (0)