#47309 closed defect (bug) (invalid)
wp_script_is seems to fail in unit tests during plugin development
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Script Loader | Keywords: | |
Focuses: | Cc: |
Description
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
- add the following to your main plugin file
<?php add_action('wp_enqueue_scripts', function() { wp_enqueue_script( 'myhandle', plugin_dir_url(__FILE__) . '/js/myfile.js', ['jquery'] ); });
- Add a test to the tests folder. In the body of the test, add:
<?php $this->assertTrue( wp_script_is( 'myhandle', 'enqueued' ) );
- 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
Change History (4)
#2
@
6 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.
Sorry… This can be resolved by running
at the start of the test.
Feel free to close this ticket…