Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running unit test shows an error #25

Closed
Ratko-Solaja opened this issue Jan 20, 2023 · 5 comments
Closed

Running unit test shows an error #25

Ratko-Solaja opened this issue Jan 20, 2023 · 5 comments
Assignees
Labels
bug Something isn't working upstream issue Issue isn't coming from this repo, but from an upstream dependency

Comments

@Ratko-Solaja
Copy link

Ratko-Solaja commented Jan 20, 2023

Describe your bug

After doing a clean install of wp-pest and running the command vendor/bin/pest --group=unit - the following error appears:
Screenshot 2023-01-20 at 1 48 17 PM

Steps to Reproduce

  1. Install wp-pest: composer require dingo-d/wp-pest-integration-test-setup --dev
  2. Setup wp-pest: vendor/bin/wp-pest setup theme
  3. Run the unit test: vendor/bin/pest --group=unit

Expected behavior

It should do the tests.

Environment info

  • macOS Ventura 13.1
  • PHP 7.4 or 8.0

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

Yes

@Ratko-Solaja Ratko-Solaja added the bug Something isn't working label Jan 20, 2023
@dingo-d dingo-d added the upstream issue Issue isn't coming from this repo, but from an upstream dependency label Jan 21, 2023
@dingo-d
Copy link
Owner

dingo-d commented Jan 21, 2023

@Ratko-Solaja for now it looks to be an upstream issue. I'm not sure if this will be fixed, or if this was intended to be done in this way, but based on what I'll get as an answer I might have to limit the functionality of this package to integration tests only :/

As an intermediate step, you could try to see if modifying the bootstrap.php file could help. The uses() for the unit tests should stay as is (so that the unit tests are using the BrainMonkey base test class), but still load WordPress, even though you wouldn't use it. It would be a bit of an overhead (loading WordPress for unit tests), but I think it could work (will try to test it out).

The above suggestion won't work. In that case even the unit tests will use the integration test base test class :/

@dingo-d
Copy link
Owner

dingo-d commented Jan 26, 2023

@Ratko-Solaja I think I found a dirty workaround that should do the trick.

In your integration tests, before the uses() statement add

if (!empty($GLOBALS['argv']) && $GLOBALS['argv'][1] === '--group=unit') {
	return;
}

That way, you'll bail out of the integration test file during unit tests.

EDIT:

You can also create a helper in the Pest.php file

function isUnitTest() {
	return !empty($GLOBALS['argv']) && $GLOBALS['argv'][1] === '--group=unit';
}

And then just use in your integration test:

<?php

use Yoast\WPTestUtils\WPIntegration\TestCase;

if (isUnitTest()) {
	return;
}

uses(TestCase::class);

// Rest of the tests.

Looks a bit cleaner 😄

@dingo-d dingo-d closed this as completed Jan 30, 2023
@Dartui
Copy link

Dartui commented Jun 14, 2023

I have stumbled upon this error today and after many attempts I think that I found what causes that error.

By default phpunit.xml.dist has set bootstrap attribute to tests/bootstrap.php and that's fine. However Pest loads tests/Pest.php before bootstraper. That's why using Yoast TestCase or anything else from WordPress testing repository in Pest.php file throws errors, because it's not yet loaded.

Switching attribute to bootstrap="vendor/autoload.php" and requiring tests/bootstrap.php on top of tests/Pest.php file solved that issue for me.

@dingo-d
Copy link
Owner

dingo-d commented Jun 14, 2023

I will try to test this and if this works update the lib 👍🏼

Thanks!

@dingo-d
Copy link
Owner

dingo-d commented Sep 27, 2023

@Dartui it looks like this works, so I will try to update the readme with the instructions. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working upstream issue Issue isn't coming from this repo, but from an upstream dependency
Projects
None yet
Development

No branches or pull requests

3 participants