0

This question is related to this SO post

I'm testing a plugin, and in it I have

public function enqueue_scripts() {
    $main_style = 'public/scripts/application.js';

    wp_register_script( 'plugin-scripts', plugin_dir_url( __DIR__ ) . $main_script, array() );

    wp_enqueue_script( 'plugin-scripts' );
}

The plugin is using composer and npm, so it needs to be built to work.

And it is built (locally where I'm testing it, it works in a local instance I have on VVV). The test I wrote looks like this

public function test_enqueued_scripts() {
    $this->admin->enqueue_scripts();
    $this->assertTrue( wp_script_is( 'plugin-scripts' ) );
}

$this->admin is just an instance of the class where my enqueue method is in the setUp() method.

The test fails (Failed asserting that false is true), and I wanted to see what's wrong so I added

public function enqueue_scripts() {
    $main_style = 'public/scripts/application.js';
    error_log( print_r( plugin_dir_url( __DIR__ ), true ) );
    error_log( print_r( __DIR__, true ) );
    wp_register_script( 'plugin-scripts', plugin_dir_url( __DIR__ ) . $main_script, array() );

    wp_enqueue_script( 'plugin-scripts' );
}

And ran the unit test, I saw

http://example.org/wp-content/plugins/Users/dingo_d/vagrant-local/www/projects/project/public_html/wp-content/plugins/my-plugin/

For the plugin_dir_url( __DIR__ ), and for the __DIR__ I have

/vagrant-local/www/projects/project/public_html/wp-content/plugins/my-plugin/

The test WordPress instance is located in a temporary folder that looks something like

/var/folders/xj/14b3w8w51ll6rw09q72p45cr0000gn/T/wordpress/

But my plugins are not installed there. I'm guessing the tests are only using that as a source for the core functionality. Also the bootstrap.php file is referencing the wordpress-develop that is located in my VVV folder.

What I don't understand is why do I get that example.org url? And how can I successfully test the enqueueing/registering of the script?

1 Answer 1

1

You need to run do_action('wp_enqueue_scripts') prior to executing your assertions. This article has more details and an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.