SKILL.blade.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ---
  2. name: pest-testing
  3. description: "Use this skill for Pest PHP testing in Laravel projects only. Trigger whenever any test is being written, edited, fixed, or refactored — including fixing tests that broke after a code change, adding assertions, converting PHPUnit to Pest, adding datasets, and TDD workflows. Always activate when the user asks how to write something in Pest, mentions test files or directories (tests/Feature, tests/Unit) or architecture tests. Covers: test()/it()/expect() syntax, datasets, mocking, browser testing, arch(), Livewire component tests, RefreshDatabase, and all Pest 3 features. Do not use for editing factories, seeders, migrations, controllers, models, or non-test PHP code."
  4. license: MIT
  5. metadata:
  6. author: laravel
  7. ---
  8. @php
  9. /** @var \Laravel\Boost\Install\GuidelineAssist $assist */
  10. @endphp
  11. # Pest Testing 3
  12. ## Documentation
  13. Use `search-docs` for detailed Pest 3 patterns and documentation.
  14. ## Basic Usage
  15. ### Creating Tests
  16. All tests must be written using Pest. Use `{{ $assist->artisanCommand('make:test --pest {name}') }}`.
  17. The `{name}` argument should include only the path and test name, but should not include the test suite.
  18. - Incorrect: `{{ $assist->artisanCommand('make:test --pest Feature/SomeFeatureTest') }}` will generate `tests/Feature/Feature/SomeFeatureTest.php`
  19. - Correct: `{{ $assist->artisanCommand('make:test --pest SomeControllerTest') }}` will generate `tests/Feature/SomeControllerTest.php`
  20. - Incorrect: `{{ $assist->artisanCommand('make:test --pest --unit Unit/SomeServiceTest') }}` will generate `tests/Unit/Unit/SomeServiceTest.php`
  21. - Correct: `{{ $assist->artisanCommand('make:test --pest --unit SomeServiceTest') }}` will generate `tests/Unit/SomeServiceTest.php`
  22. ### Test Organization
  23. - Tests live in the `tests/Feature` and `tests/Unit` directories.
  24. - Do NOT remove tests without approval - these are core application code.
  25. - Test happy paths, failure paths, and edge cases.
  26. ### Basic Test Structure
  27. Pest supports both `test()` and `it()` functions. Before writing new tests, check existing test files in the same directory to match the project's convention. Use `test()` if existing tests use `test()`, or `it()` if they use `it()`.
  28. @boostsnippet("Basic Pest Test Example", "php")
  29. it('is true', function () {
  30. expect(true)->toBeTrue();
  31. });
  32. @endboostsnippet
  33. ### Running Tests
  34. - Run minimal tests with filter before finalizing: `{{ $assist->artisanCommand('test --compact --filter=testName') }}`.
  35. - Run all tests: `{{ $assist->artisanCommand('test --compact') }}`.
  36. - Run file: `{{ $assist->artisanCommand('test --compact tests/Feature/ExampleTest.php') }}`.
  37. ## Assertions
  38. Use specific assertions (`assertSuccessful()`, `assertNotFound()`) instead of `assertStatus()`:
  39. @boostsnippet("Pest Response Assertion", "php")
  40. it('returns all', function () {
  41. $this->postJson('/api/docs', [])->assertSuccessful();
  42. });
  43. @endboostsnippet
  44. | Use | Instead of |
  45. |-----|------------|
  46. | `assertSuccessful()` | `assertStatus(200)` |
  47. | `assertNotFound()` | `assertStatus(404)` |
  48. | `assertForbidden()` | `assertStatus(403)` |
  49. ## Mocking
  50. Import mock function before use: `use function Pest\Laravel\mock;`
  51. ## Datasets
  52. Use datasets for repetitive tests (validation rules, etc.):
  53. @boostsnippet("Pest Dataset Example", "php")
  54. it('has emails', function (string $email) {
  55. expect($email)->not->toBeEmpty();
  56. })->with([
  57. 'james' => 'james@laravel.com',
  58. 'taylor' => 'taylor@laravel.com',
  59. ]);
  60. @endboostsnippet
  61. ## Pest 3 Features
  62. ### Architecture Testing
  63. Pest 3 includes architecture testing to enforce code conventions:
  64. @boostsnippet("Architecture Test Example", "php")
  65. arch('controllers')
  66. ->expect('App\Http\Controllers')
  67. ->toExtendNothing()
  68. ->toHaveSuffix('Controller');
  69. arch('models')
  70. ->expect('App\Models')
  71. ->toExtend('Illuminate\Database\Eloquent\Model');
  72. arch('no debugging')
  73. ->expect(['dd', 'dump', 'ray'])
  74. ->not->toBeUsed();
  75. @endboostsnippet
  76. ### Type Coverage
  77. Pest 3 provides improved type coverage analysis. Run with `--type-coverage` flag.
  78. ## Common Pitfalls
  79. - Not importing `use function Pest\Laravel\mock;` before using mock
  80. - Using `assertStatus(200)` instead of `assertSuccessful()`
  81. - Forgetting datasets for repetitive validation tests
  82. - Deleting tests without approval
  83. - Prefixing `Feature/` or `Unit/` in `{name}` when using `make:test`