Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Pest Plugin Faker Laravel Package

pestphp/pest-plugin-faker

Pest Plugin Faker adds seamless Faker support to Pest tests, letting you quickly generate realistic fake data in your test suite. Ideal for speeding up data-driven tests with minimal setup.

View on GitHub
Deep Wiki
Context7

Getting Started

Install the package via Composer:

composer require --dev pestphp/pest-plugin-faker

No manual setup is required—once installed, Faker helpers like fake() and Faker::name() become available in your Pest tests. Start by using it in a basic test:

it('generates a random name', function () {
    expect(fake()->name())->toBeString();
});

Check the official docs and Faker docs to understand available generators (name(), email(), uuid(), etc.). Your first use case will likely be seeding fake data into database factory-like test scenarios.

Implementation Patterns

  • Inline generation: Use fake() anywhere in your test to access the Faker instance:
    it('creates a user with fake data', function () {
        $user = User::create([
            'name' => fake()->name(),
            'email' => fake()->unique()->email(),
        ]);
        expect($user->name)->not->toBeEmpty();
    });
    
  • Predefined fake datasets: Define reusable fake payloads in test helpers or Pest.php:
    function fakeUser(array $overrides = [])
    {
        return array_merge([
            'name' => fake()->name(),
            'email' => fake()->unique()->email(),
            'password' => bcrypt('password'),
        ], $overrides);
    }
    
    it('validates user registration', function () {
        $response = $this->post('/register', fakeUser());
        $response->assertRedirect();
    });
    
  • Integration with factories: Extend Laravel Eloquent factories using Faker for realistic seeds, especially when tests require multiple entities:
    User::factory()
        ->count(5)
        ->state(fn () => ['name' => fake()->name()])
        ->create();
    

Gotchas and Tips

  • Locale & Provider awareness: fake() uses the default locale (usually en_US). Override with fake('fr_FR') or register custom Faker providers in Pest.php:
    use Faker\Generator;
    use Illuminate\Foundation\Testing\TestCase;
    
    TestCase::macro('faker', fn ($locale = 'en_US') => app(Generator::class)->locale($locale));
    
  • Uniqueness constraints fail silently: fake()->unique()->email() may throw exceptions if uniqueness cannot be enforced (e.g., over 10,000 attempts). Guard against this in loops or use fake()->optional() for non-critical fields.
  • Avoid in production logic: The plugin is designed for testing only. Double-check that fake() calls are not used outside tests/.
  • Static method fallback: If fake() isn’t working, ensure no helper name collisions—fall back to Faker::create()->name() explicitly if needed.
  • Performance tip: For tests needing many fake records, consider seeding a fixed set or using deterministic generators (e.g., faker()->seed(1234)) to avoid flaky assertions.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation
uri-template/tests