- How does PestStan improve type safety in Laravel Pest tests?
- PestStan adds PHPStan support for Pest’s `expect()` function, enabling generic typing (e.g., `Expectation<string>`) and type narrowing after assertions like `toBeString()` or `toBeInstanceOf()`. This ensures static analysis catches type mismatches early, reducing runtime errors in Laravel’s test suites.
- Will PestStan work with Laravel’s TestCase or custom test classes?
- Yes, PestStan integrates out-of-the-box with Laravel’s `TestCase` and custom test classes. It auto-detects Pest’s configuration and enforces type safety across all test methods, including those extending Laravel’s base test classes.
- What Laravel/Pest versions does PestStan support?
- PestStan supports Pest 3.0–5.0 and Laravel projects using Pest as their testing framework. It requires PHP 8.2+ and PHPStan 2.0+, so ensure your Laravel app meets these baseline requirements before installation.
- Do I need to modify existing Pest tests to use PestStan?
- No modifications are needed—PestStan works passively by enhancing PHPStan’s static analysis. Your existing test logic remains unchanged, but you’ll gain type safety for `expect()` calls, `$this` binding, and Pest function return types automatically.
- How do I configure PestStan if I don’t use phpstan/extension-installer?
- Add this to your `phpstan.neon` file: `includes: - vendor/mrpunyapal/peststan/extension.neon`. If PestStan doesn’t detect your test classes, explicitly set `testCaseClass` in the extension’s NEON config to match your Laravel test case class.
- Will PestStan slow down my CI/CD pipeline?
- No, PestStan runs only during static analysis (not at runtime) and adds minimal overhead. PHPStan’s incremental analysis ensures reprocessing large Laravel test suites remains efficient, even with hundreds of test files.
- How do I handle false positives in type narrowing (e.g., dynamic factory calls)?
- Use `@var` annotations for complex cases, like `@var User|string $value`. For persistent issues, suppress rules with `@phpstan-ignore pest.test.staticClosure` or adjust PHPStan’s baseline to exclude problematic tests temporarily.
- Can PestStan enforce stricter rules like `pest.expectation.impossible` in CI?
- Yes, enable stricter rules in your CI’s `phpstan.neon` by adding `level: 8` (or higher) and including PestStan’s extension. Start with `level: 5` locally to avoid disrupting development workflows.
- Are there alternatives to PestStan for type-safe Pest tests?
- For PHPStan integration, PestStan is the only dedicated extension. Alternatives include using Psalm with Pest (via `vimeo/psalm`), but PestStan provides deeper Pest-specific type support, like `$this` binding and `and()` chaining, tailored for Laravel’s testing ecosystem.
- How do I test PestStan’s effectiveness in a Laravel project?
- Run `phpstan analyse` and check for new type errors in Pest tests. Focus on `expect()` calls and assertion chains—if PHPStan flags inconsistencies, they’re likely genuine issues. Use the `--error-format=json` flag to parse results programmatically for CI integration.