This repo contains custom expectations to be used in a Pest test suite.
It also contains various helpers to make testing with Pest easier. Imagine, you only want to run a test on GitHub Actions. You can use the whenGitHubActions
helper to do so.
it('can only run well on github actions', function () {
// your test
})->whenGitHubActions();
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
You can install the package via composer:
composer require spatie/pest-expectations
Once installed, you can use the custom expectations and helpers provided by this package.
This expectation can be used to test if an invokable validation rule works correctly.
In this example, the $value
will be given to YourValidationRule
. The expectation will pass if your rule passed for the given value.
expect(new YourValidationRule())->toPassWith($value);
You can expect the your validation not to pass for the given value, by using Pest's not()
.
expect(new YourValidationRule()->not()->toPassWith($value);
This expectation can be used to test if an invokable validation rule did not pass for a given value.
In this example, the $value
will be given to YourValidationRule
. The expectation will pass if your rule did not pass for the given value.
expect(new YourValidationRule())->toFailWith($value);
Optionally, you can also pass a message as the second argument. The expectation will pass is the validation rule return the given $message
.
expect(new YourValidationRule())->toFailWith($value, 'This value is not valid.');
Expect that a value is a model an equal to the passed model.
expect($model)->toBeModel($anotherModel);
The expectation will only pass if both models are Eloquent models of the same class, with the same key.
Expect that a value is an array of the specified value.
expect(User::all())->toBeArrayOf(User::class);
The specified value may be a class name or a class instance, or one of the following string values:
'string'
'int'
'float'
'bool'
'scalar'
'array'
'object'
'null'
expect([1, 2])->toBeArrayOf('int');
expect([true, false])->toBeArrayOf('bool');
expect(['foo', 1, false])->toBeArrayOf('scalar');
Expect that a value is a scheduled job, command or invokable class. The timezone
parameter, if passed, will also check that the specified timezone was defined.
expect(MyJob::class)->toBeScheduled('0 * * * *', timezone: 'Europe/Paris');
Optionally, you may pass a callback that accepts an Illuminate\Console\Scheduling\Event
or Illuminate\Console\Scheduling\CallbackEvent
instance, so you can run any assertion needed:
expect(MyArtisanCommand::class)->toBeScheduled(function (Event $event) {
expect($event->getExpression())->toBe('0 0 * * *');
expect($event->getSummaryForDisplay())->toBe('Foo');
});
Expect that a response has JSON:API pagination. Have a look at our package spatie/laravel-json-api-paginate to see how to add JSON:API pagination to your Laravel app.
$response = $this->get('/');
expect($response)->toHaveJsonApiPagination();
This package offers various helpers that you can tack on any test. Here's an example of the whenGitHubActions
helper. When tacked on to a test, the test will be skipped unless you're running it on GitHub Actions.
it('can only run well on github actions', function () {
// your test
})->whenGitHubActions();
To use the helpers, you should call registerSpatiePestHelpers()
in your Pest.php
file.
These helpers are provided by this package:
whenConfig($configKey)
: only run the test when the given Laravel config key is setwhenEnvVar($envVarName)
: only run the test when the given environment variable is setwhenWindows
: the test will be skipped unless running on WindowswhenMac
: the test will be skipped unless running on macOSwhenLinux
: the test will be skipped unless running on LinuxwhenGitHubActions()
: the test will be skipped unless running on GitHub ActionsskipOnGitHubActions()
: the test will be skipped when running on GitHub ActionswhenPhpVersion($version)
: the test will be skipped unless running on the given PHP version, or higher. You can pass a version like 8
or 8.1
.This package also provides a set of custom assertions that you can use in your tests.
In your TestCase
class, you can use the CustomAssertions
trait and call the registerCustomAssertions
method in the setUp
method.
class TestCase extends \Illuminate\Foundation\Testing\TestCase
{
use CustomAssertions;
protected function setUp(): void
{
parent::setUp();
$this->registerCustomAssertions();
}
Assert that a response has JSON:API pagination. Have a look at our package spatie/laravel-json-api-paginate to see how to add JSON:API pagination to your Laravel app.
$this
->get('/')
->assertHasJsonApiPagination();
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.