[wp-trac] [WordPress Trac] #47309: wp_script_is seems to fail in unit tests during plugin development

WordPress Trac noreply at wordpress.org
Fri May 17 18:15:49 UTC 2019


#47309: wp_script_is seems to fail in unit tests during plugin development
---------------------------+-----------------------------
 Reporter:  tedmaster      |      Owner:  (none)
     Type:  defect (bug)   |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  Script Loader  |    Version:
 Severity:  normal         |   Keywords:
  Focuses:                 |
---------------------------+-----------------------------
 **Summary**

 Enqueing a script in a plugin cannot be tested with wp_script_is() because
 it always returns false.

 Although enqueing scripts is default WordPress functionality and should
 not be tested in plugin tests, we have a case in which scripts are only
 enqueued in certain date ranges, and even then, depending on the date
 range, one script is enqueued rather than another. In other words, we're
 not interested in testing the default functionality of wp_enqueue_script
 but rather whether or not the plugin is enqueing the right script.

 **Preconditions**
 - install plugin scaffolding using
 {{{
 wp scaffold plugin
 }}}
  etc.

 **Steps to Reproduce**
 1. add the following to your main plugin file
 {{{#!php
 <?php
 add_action('wp_enqueue_scripts', function() {
         wp_enqueue_script( 'myhandle', plugin_dir_url(__FILE__) .
 '/js/myfile.js', ['jquery'] );
 });
 }}}
 2. Add a test to the tests folder. In the body of the test, add:
 {{{#!php
 <?php
         $this->assertTrue( wp_script_is( 'myhandle', 'enqueued' ) );
 }}}
 3. Run phpunit

 **Expected Result**
 - Test passes

 **Actual Result**
 - Test fails with "Failed asserting that false is true."

 **Workaround**
 - Possibly forcing WP_Scripts instance to enqueue all scripts but apparent
 the custom scripts aren't even registered either.

 **Reference**
 - https://stackoverflow.com/questions/35246470/wordpress-wp-script-is-
 done-not-working-on-custom-scripts
 - https://wordpress.stackexchange.com/questions/305500/integration-tests-
 test-script-enqueue-register-fails
 - https://make.wordpress.org/core/handbook/testing/automated-testing
 /writing-phpunit-tests/#the-wordpress-installation-and-bootstrap

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47309>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list