Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#47309 closed defect (bug) (invalid)

wp_script_is seems to fail in unit tests during plugin development

Reported by: tedmaster's profile tedmaster Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Script Loader Keywords:
Focuses: Cc:



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.


  • install plugin scaffolding using
    wp scaffold plugin

Steps to Reproduce

  1. add the following to your main plugin file
    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:
            $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."


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


Change History (4)

#1 @tedmaster
5 years ago

  • Keywords close added

Sorry… This can be resolved by running

do_action( 'wp_enqueue_scripts' );

at the start of the test.

Feel free to close this ticket…

#2 @swissspidy
5 years ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

I was just about to comment :-)

You are hooking your wp_enqueue_script call to the wp_enqueue_scripts action, which fires on page load. However, this action is never fired in your test. You would manually need to do that.

#3 @tedmaster
5 years ago

So, is there a general page_load action that I should be executing and that includes things like wp_enqueue_scripts? Asking because I often want to test what would be the final outcome of a normal page load.

#4 @swissspidy
5 years ago

Such a thing doesn't really exist. If you want to test the outcome of a normal page load I recommend you to look into integration or end-to-end testing instead.

Note: See TracTickets for help on using tickets.