dingo-d / wp-pest

A package that will add WordPress integration test suite with Pest
MIT License
121 stars 5 forks source link

Integration tests aren't isolated #36

Open jkudish opened 5 months ago

jkudish commented 5 months ago

Describe your bug

When running consecutive integration tests, much of the WordPress lifecycle seem to persist between the tests. I would expect each request to run independently.

Steps to Reproduce

Here's two example tests that demonstrate the issue:

test('action demo 1', function(){
    add_action('wp', function(){
        fputs(STDOUT, "wp action fired");
    });

    wp();
});

test('action demo 2', function(){
    // this will still have the action from the previous test
    // and emit the dump
    wp();
});

Expected behavior

The action was defined in the first test, and I would expect it to only fire in that test.

Screenshots, screen recording, code snippet

No response

Environment info

PHP 8.1 WordPress 6.4.2

Please confirm that you have searched existing issues in this repo.

Yes

dingo-d commented 5 months ago

Pest v1 doesn't have process isolation, like regular phpunit does, so I kinda think that would be the 'culprit' here.

jkudish commented 5 months ago

Pest v1 doesn't have process isolation, like regular phpunit does, so I kinda think that would be the 'culprit' here.

Gotcha, makes sense - any chance of working towards an upgrade to pest v2?

dingo-d commented 5 months ago

Until WP core updates their unit test suites to be compatible with PHPUnit 10 there's not much I can do.

I've opened up a trac ticket about it, but that one will probably be closed in favor of one with much more detailed steps necessary to move it forward.

jkudish commented 5 months ago

Hey @dingo-d nice thanks for the update. I'll follow along and hopefully we'll get PHPUnit 10 compatibility in WP soon 😄

In the mean time, in case anyone else encounters this, note that I worked around my specific issue by doing something like this with my tests (this is a simplified example).

beforeEach(function (){
    global $wp_actions;
    unset($wp_actions['my_action']);
});

test('my action fires', function (){
    do_action('my_action');
    expect(did_action('my_action'))->toBeTrue();
});

test('my action does not fire', function (){
    expect(did_action('my_action'))->toBeFalse();
});