Skip to content

Commit 278bc79

Browse files
peterwilsonccaslamdoctor
authored andcommitted
External Libraries: Test registered script versions match package.json.
Expands tests to ensure the version number of packages updated via NPM matches the version number used for registering the script in the script loader. This adds tests for (by their registered name in WordPress): * backbone * clipboard * hoverIntent * hoverintent-js * imagesloaded * jquery-color * jquery-core * jquery-form * masonry * react-jsx-runtime * underscore * wp-polyfill-dom-rect * wp-polyfill-element-closest * wp-polyfill-fetch * wp-polyfill-formdata * wp-polyfill-inert * wp-polyfill-node-contains * wp-polyfill-object-fit * wp-polyfill-url This expands on the earlier tests introduced for: * lodash * moment * react * react-dom * regenerator-runtime An additional test is added to ensure that the data provider for these tests is maintained as libraries are added via package.json. `@wordpress/*` scripts are excluded from these tests as wp-scripts generates a version number automatically based on the file's contents. Additionally, the version of element-closest listed in package.json is updated to use a fixed version rather than a range. This reflects the current practice of WordPress to define the specific version in core. For the avoidance of doubt, this does not affect the version shipped in WordPress. Follow up to [57185]. Props peterwilsoncc, jorbin. Fixes #61855. git-svn-id: https://develop.svn.wordpress.org/trunk@59071 602fd350-edb4-49c9-b593-d223f7449a82
1 parent c8095ce commit 278bc79

3 files changed

Lines changed: 103 additions & 12 deletions

File tree

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
"backbone": "1.6.0",
148148
"clipboard": "2.0.11",
149149
"core-js-url-browser": "3.6.4",
150-
"element-closest": "^3.0.2",
150+
"element-closest": "3.0.2",
151151
"formdata-polyfill": "4.0.10",
152152
"hoverintent": "2.2.1",
153153
"imagesloaded": "5.0.0",

tests/phpunit/tests/dependencies/scripts.php

Lines changed: 101 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3386,29 +3386,120 @@ public function data_provider_script_move_to_footer() {
33863386
}
33873387

33883388
/**
3389+
* Tests default scripts are registered with the correct versions.
3390+
*
3391+
* Ensures that vendor scripts registered in wp_default_scripts() and
3392+
* wp_default_packages_vendor() are registered with the correct version
3393+
* number from package.json.
3394+
*
3395+
* @ticket 61855
33893396
* @ticket 60048
33903397
*
3398+
* @covers ::wp_default_scripts
33913399
* @covers ::wp_default_packages_vendor
33923400
*
3393-
* @dataProvider data_wp_default_packages_vendor
3401+
* @dataProvider data_vendor_script_versions_registered_manually
3402+
*
3403+
* @param string $script Script name as defined in package.json.
3404+
* @param string $handle Optional. Handle to check for. Defaults to the script name.
33943405
*/
3395-
public function test_wp_default_packages_vendor( $script ) {
3406+
public function test_vendor_script_versions_registered_manually( $script, $handle = null ) {
33963407
global $wp_scripts;
3408+
wp_default_packages_vendor( $wp_scripts );
3409+
wp_default_scripts( $wp_scripts );
3410+
33973411
$package_json = $this->_scripts_from_package_json();
3412+
if ( ! $handle ) {
3413+
$handle = $script;
3414+
}
33983415

3399-
wp_default_packages_vendor( $wp_scripts );
3416+
$script_query = $wp_scripts->query( $handle, 'registered' );
34003417

3401-
$this->assertSame( $package_json[ $script ], $wp_scripts->query( $script, 'registered' )->ver );
3418+
$this->assertNotFalse( $script_query, "The script '{$handle}' should be registered." );
3419+
$this->assertArrayHasKey( $script, $package_json, "The dependency '{$script}' should be included in package.json." );
3420+
$this->assertSame( $package_json[ $script ], $wp_scripts->query( $handle, 'registered' )->ver, "The script '{$handle}' should be registered with version {$package_json[ $script ]}." );
34023421
}
34033422

3404-
public function data_wp_default_packages_vendor() {
3423+
/**
3424+
* Data provider for test_vendor_script_versions_registered_manually.
3425+
*
3426+
* @return array[]
3427+
*/
3428+
public function data_vendor_script_versions_registered_manually() {
34053429
return array(
3406-
array( 'script' => 'lodash' ),
3407-
array( 'script' => 'moment' ),
3408-
array( 'script' => 'react' ),
3409-
array( 'script' => 'react-dom' ),
3410-
array( 'script' => 'regenerator-runtime' ),
3430+
'backbone' => array( 'backbone' ),
3431+
'clipboard' => array( 'clipboard' ),
3432+
'core-js-url-browser' => array( 'core-js-url-browser', 'wp-polyfill-url' ),
3433+
'element-closest' => array( 'element-closest', 'wp-polyfill-element-closest' ),
3434+
'formdata-polyfill' => array( 'formdata-polyfill', 'wp-polyfill-formdata' ),
3435+
'imagesloaded' => array( 'imagesloaded' ),
3436+
'jquery-color' => array( 'jquery-color' ),
3437+
'jquery-core' => array( 'jquery', 'jquery-core' ),
3438+
'jquery-form' => array( 'jquery-form' ),
3439+
'jquery-hoverintent' => array( 'jquery-hoverintent', 'hoverIntent' ),
3440+
'lodash' => array( 'lodash' ),
3441+
'masonry' => array( 'masonry-layout', 'masonry' ),
3442+
'moment' => array( 'moment' ),
3443+
'objectFitPolyfill' => array( 'objectFitPolyfill', 'wp-polyfill-object-fit' ),
3444+
'polyfill-library (dom rect)' => array( 'polyfill-library', 'wp-polyfill-dom-rect' ),
3445+
'polyfill-library (node contains)' => array( 'polyfill-library', 'wp-polyfill-node-contains' ),
3446+
'react (jsx-runtime)' => array( 'react', 'react-jsx-runtime' ),
3447+
'react (React)' => array( 'react' ),
3448+
'react-dom' => array( 'react-dom' ),
3449+
'regenerator-runtime' => array( 'regenerator-runtime' ),
3450+
'underscore' => array( 'underscore' ),
3451+
'vanilla-js-hoverintent' => array( 'hoverintent', 'hoverintent-js' ),
3452+
'whatwg-fetch' => array( 'whatwg-fetch', 'wp-polyfill-fetch' ),
3453+
'wicg-inert' => array( 'wicg-inert', 'wp-polyfill-inert' ),
3454+
);
3455+
}
3456+
3457+
/**
3458+
* Ensures that all the scripts in the package.json are included in the data provider.
3459+
*
3460+
* This is a test the tests to ensure the data provider includes all the scripts in package.json.
3461+
*
3462+
* @ticket 61855
3463+
*/
3464+
public function test_vendor_script_data_provider_includes_all_packages() {
3465+
$package_json_dependencies = array_keys( $this->_scripts_from_package_json() );
3466+
$data_provider_dependencies = $this->data_vendor_script_versions_registered_manually();
3467+
3468+
/*
3469+
* Exclude `@wordpress/*` packages from the packages in package.json.
3470+
*
3471+
* The version numbers for these packages is generated by the build
3472+
* process based on a hash of the file contents.
3473+
*/
3474+
$package_json_dependencies = array_filter(
3475+
$package_json_dependencies,
3476+
static function ( $dependency ) {
3477+
return 0 !== strpos( $dependency, '@wordpress/' );
3478+
}
34113479
);
3480+
3481+
// Get the script names from the data provider.
3482+
$data_provider_dependencies = array_map(
3483+
static function ( $dependency ) {
3484+
return $dependency[0];
3485+
},
3486+
$data_provider_dependencies
3487+
);
3488+
3489+
// Exclude packages that are not registered in WordPress.
3490+
$exclude = array( 'react-is', 'json2php' );
3491+
$package_json_dependencies = array_diff( $package_json_dependencies, $exclude );
3492+
3493+
/*
3494+
* Ensure the arrays are unique.
3495+
*
3496+
* This is for the react package as it is included in the data provider
3497+
* as both `react` and `react-jsx-runtime`.
3498+
*/
3499+
$package_json_dependencies = array_unique( $package_json_dependencies );
3500+
$data_provider_dependencies = array_unique( $data_provider_dependencies );
3501+
3502+
$this->assertSameSets( $package_json_dependencies, $data_provider_dependencies );
34123503
}
34133504

34143505
/**

0 commit comments

Comments
 (0)